BAEL-838 Changed method names

This commit is contained in:
Predrag Maric 2017-05-14 00:13:18 +02:00
parent 85255bc03b
commit e497266a82
3 changed files with 32 additions and 34 deletions

2
.gitignore vendored
View File

@ -33,5 +33,3 @@ spring-openid/src/main/resources/application.properties
spring-security-openid/src/main/resources/application.properties
spring-all/*.log
core-java/src/main/java/com/baeldung/string/RemoveLastChar.java
core-java/src/test/java/com/baeldung/string/RemoveLastCharTest.java

View File

@ -3,22 +3,22 @@ package com.baeldung.string;
import java.util.Optional;
public class StringHelper {
public static String withoutLastChar(String s) {
public static String removeLastChar(String s) {
return (s == null || s.length() == 0) ? s : (s.substring(0, s.length() - 1));
}
public static String removeLastChar(String s) {
public static String removeLastCharRegex(String s) {
return (s == null) ? s : s.replaceAll(".$", "");
}
public static String withoutLastCharacter(String s) {
public static String removeLastCharOptional(String s) {
return Optional.ofNullable(s)
.filter(str -> str.length() != 0)
.map(str -> str.substring(0, str.length() - 1))
.orElse(s);
}
public static String removeLastCharacter(String s) {
public static String removeLastCharRegexOptional(String s) {
return Optional.ofNullable(s)
.map(str -> str.replaceAll(".$", ""))
.orElse(null);

View File

@ -17,76 +17,76 @@ public class StringHelperTest {
@Test
public void givenTestString_whenSubstring_thenGetStingWithoutLastChar() {
assertEquals("abcde", StringHelper.withoutLastChar(TEST_STRING));
assertEquals("abcde", StringHelper.removeLastChar(TEST_STRING));
assertEquals("abcde", StringUtils.substring(TEST_STRING, 0, TEST_STRING.length() - 1));
assertEquals("abcde", StringUtils.chop(TEST_STRING));
assertEquals("abcde", TEST_STRING.replaceAll(".$", ""));
assertEquals("abcde", StringHelper.removeLastChar(TEST_STRING));
assertEquals("abcde", StringHelper.withoutLastCharacter(TEST_STRING));
assertEquals("abcde", StringHelper.removeLastCharacter(TEST_STRING));
assertEquals("abcde", StringHelper.removeLastCharRegex(TEST_STRING));
assertEquals("abcde", StringHelper.removeLastCharOptional(TEST_STRING));
assertEquals("abcde", StringHelper.removeLastCharRegexOptional(TEST_STRING));
}
@Test
public void givenNullString_whenSubstring_thenGetNullString() {
assertEquals(NULL_STRING, StringHelper.withoutLastChar(NULL_STRING));
assertEquals(NULL_STRING, StringUtils.chop(NULL_STRING));
assertEquals(NULL_STRING, StringHelper.removeLastChar(NULL_STRING));
assertEquals(NULL_STRING, StringHelper.withoutLastCharacter(NULL_STRING));
assertEquals(NULL_STRING, StringHelper.removeLastCharacter(NULL_STRING));
assertEquals(NULL_STRING, StringUtils.chop(NULL_STRING));
assertEquals(NULL_STRING, StringHelper.removeLastCharRegex(NULL_STRING));
assertEquals(NULL_STRING, StringHelper.removeLastCharOptional(NULL_STRING));
assertEquals(NULL_STRING, StringHelper.removeLastCharRegexOptional(NULL_STRING));
}
@Test
public void givenEmptyString_whenSubstring_thenGetEmptyString() {
assertEquals(EMPTY_STRING, StringHelper.withoutLastChar(EMPTY_STRING));
assertEquals(EMPTY_STRING, StringHelper.removeLastChar(EMPTY_STRING));
assertEquals(EMPTY_STRING, StringUtils.substring(EMPTY_STRING, 0, EMPTY_STRING.length() - 1));
assertEquals(EMPTY_STRING, StringUtils.chop(EMPTY_STRING));
assertEquals(EMPTY_STRING, EMPTY_STRING.replaceAll(".$", ""));
assertEquals(EMPTY_STRING, StringHelper.removeLastChar(EMPTY_STRING));
assertEquals(EMPTY_STRING, StringHelper.withoutLastCharacter(EMPTY_STRING));
assertEquals(EMPTY_STRING, StringHelper.removeLastCharacter(EMPTY_STRING));
assertEquals(EMPTY_STRING, StringHelper.removeLastCharRegex(EMPTY_STRING));
assertEquals(EMPTY_STRING, StringHelper.removeLastCharOptional(EMPTY_STRING));
assertEquals(EMPTY_STRING, StringHelper.removeLastCharRegexOptional(EMPTY_STRING));
}
@Test
public void givenOneCharString_whenSubstring_thenGetEmptyString() {
assertEquals(EMPTY_STRING, StringHelper.withoutLastChar(ONE_CHAR_STRING));
assertEquals(EMPTY_STRING, StringHelper.removeLastChar(ONE_CHAR_STRING));
assertEquals(EMPTY_STRING, StringUtils.substring(ONE_CHAR_STRING, 0, ONE_CHAR_STRING.length() - 1));
assertEquals(EMPTY_STRING, StringUtils.chop(ONE_CHAR_STRING));
assertEquals(EMPTY_STRING, ONE_CHAR_STRING.replaceAll(".$", ""));
assertEquals(EMPTY_STRING, StringHelper.removeLastChar(ONE_CHAR_STRING));
assertEquals(EMPTY_STRING, StringHelper.withoutLastCharacter(ONE_CHAR_STRING));
assertEquals(EMPTY_STRING, StringHelper.removeLastCharacter(ONE_CHAR_STRING));
assertEquals(EMPTY_STRING, StringHelper.removeLastCharRegex(ONE_CHAR_STRING));
assertEquals(EMPTY_STRING, StringHelper.removeLastCharOptional(ONE_CHAR_STRING));
assertEquals(EMPTY_STRING, StringHelper.removeLastCharRegexOptional(ONE_CHAR_STRING));
}
@Test
public void givenStringWithWhiteSpaceAtTheEnd_whenSubstring_thenGetStringWithoutWhiteSpaceAtTheEnd() {
assertEquals("abc", StringHelper.withoutLastChar(WHITE_SPACE_AT_THE_END_STRING));
assertEquals("abc", StringHelper.removeLastChar(WHITE_SPACE_AT_THE_END_STRING));
assertEquals("abc", StringUtils.substring(WHITE_SPACE_AT_THE_END_STRING, 0, WHITE_SPACE_AT_THE_END_STRING.length() - 1));
assertEquals("abc", StringUtils.chop(WHITE_SPACE_AT_THE_END_STRING));
assertEquals("abc", WHITE_SPACE_AT_THE_END_STRING.replaceAll(".$", ""));
assertEquals("abc", StringHelper.removeLastChar(WHITE_SPACE_AT_THE_END_STRING));
assertEquals("abc", StringHelper.withoutLastCharacter(WHITE_SPACE_AT_THE_END_STRING));
assertEquals("abc", StringHelper.removeLastCharacter(WHITE_SPACE_AT_THE_END_STRING));
assertEquals("abc", StringHelper.removeLastCharRegex(WHITE_SPACE_AT_THE_END_STRING));
assertEquals("abc", StringHelper.removeLastCharOptional(WHITE_SPACE_AT_THE_END_STRING));
assertEquals("abc", StringHelper.removeLastCharRegexOptional(WHITE_SPACE_AT_THE_END_STRING));
}
@Test
public void givenStringWithNewLineAtTheEnd_whenSubstring_thenGetStringWithoutNewLine() {
assertEquals("abc", StringHelper.withoutLastChar(NEW_LINE_AT_THE_END_STRING));
assertEquals("abc", StringHelper.removeLastChar(NEW_LINE_AT_THE_END_STRING));
assertEquals("abc", StringUtils.substring(NEW_LINE_AT_THE_END_STRING, 0, NEW_LINE_AT_THE_END_STRING.length() - 1));
assertEquals("abc", StringUtils.chop(NEW_LINE_AT_THE_END_STRING));
assertNotEquals("abc", NEW_LINE_AT_THE_END_STRING.replaceAll(".$", ""));
assertNotEquals("abc", StringHelper.removeLastChar(NEW_LINE_AT_THE_END_STRING));
assertEquals("abc", StringHelper.withoutLastCharacter(NEW_LINE_AT_THE_END_STRING));
assertNotEquals("abc", StringHelper.removeLastCharacter(NEW_LINE_AT_THE_END_STRING));
assertNotEquals("abc", StringHelper.removeLastCharRegex(NEW_LINE_AT_THE_END_STRING));
assertEquals("abc", StringHelper.removeLastCharOptional(NEW_LINE_AT_THE_END_STRING));
assertNotEquals("abc", StringHelper.removeLastCharRegexOptional(NEW_LINE_AT_THE_END_STRING));
}
@Test
public void givenMultiLineString_whenSubstring_thenGetStringWithoutNewLine() {
assertEquals("abc\nde", StringHelper.withoutLastChar(MULTIPLE_LINES_STRING));
assertEquals("abc\nde", StringHelper.removeLastChar(MULTIPLE_LINES_STRING));
assertEquals("abc\nde", StringUtils.substring(MULTIPLE_LINES_STRING, 0, MULTIPLE_LINES_STRING.length() - 1));
assertEquals("abc\nde", StringUtils.chop(MULTIPLE_LINES_STRING));
assertEquals("abc\nde", MULTIPLE_LINES_STRING.replaceAll(".$", ""));
assertEquals("abc\nde", StringHelper.removeLastChar(MULTIPLE_LINES_STRING));
assertEquals("abc\nde", StringHelper.withoutLastCharacter(MULTIPLE_LINES_STRING));
assertEquals("abc\nde", StringHelper.removeLastCharacter(MULTIPLE_LINES_STRING));
assertEquals("abc\nde", StringHelper.removeLastCharRegex(MULTIPLE_LINES_STRING));
assertEquals("abc\nde", StringHelper.removeLastCharOptional(MULTIPLE_LINES_STRING));
assertEquals("abc\nde", StringHelper.removeLastCharRegexOptional(MULTIPLE_LINES_STRING));
}
}