From 18776342340e2617b28bce4fba5716993d0f2e67 Mon Sep 17 00:00:00 2001 From: Andy Deese Date: Fri, 19 May 2017 09:18:15 -0400 Subject: [PATCH 1/5] added isBlank method, along with junit test and readme update --- .classpath | 8 ++++++++ .gitignore | 1 + .project | 23 ++++++++++++++++++++++ .settings/org.eclipse.buildship.core.prefs | 3 +++ README.md | 13 ++++++++++++ src/main/java/strman/Strman.java | 14 +++++++++++++ src/test/java/strman/StrmanTest.java | 9 +++++++-- 7 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 .classpath create mode 100644 .project create mode 100644 .settings/org.eclipse.buildship.core.prefs diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..9f084aa --- /dev/null +++ b/.classpath @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/.gitignore b/.gitignore index 48267e9..d6a4cf2 100644 --- a/.gitignore +++ b/.gitignore @@ -71,3 +71,4 @@ hs_err_pid* !gradle/wrapper/gradle-wrapper.jar +/bin/ diff --git a/.project b/.project new file mode 100644 index 0000000..0a4059d --- /dev/null +++ b/.project @@ -0,0 +1,23 @@ + + + strman + Project strman created by Buildship. + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.buildship.core.gradleprojectbuilder + + + + + + org.eclipse.buildship.core.gradleprojectnature + org.eclipse.jdt.core.javanature + + diff --git a/.settings/org.eclipse.buildship.core.prefs b/.settings/org.eclipse.buildship.core.prefs new file mode 100644 index 0000000..03931c0 --- /dev/null +++ b/.settings/org.eclipse.buildship.core.prefs @@ -0,0 +1,3 @@ +connection.gradle.distribution=GRADLE_DISTRIBUTION(WRAPPER) +connection.project.dir= +eclipse.preferences.version=1 diff --git a/README.md b/README.md index a671ca8..9bca612 100644 --- a/README.md +++ b/README.md @@ -618,6 +618,19 @@ join(new String[]{"hello","world","123"}, ";") // result => "hello;world;123") ``` +## isBlank + +isEmpty returns true if a string is blank or null and false otherwise. + +```java +isBlank("") +// result => true) +isBlank(null) +// result => true) +isBlank("test") +// result => false) +``` + ## Inspiration This library is inspired by [dleitee/strman](https://github.com/dleitee/strman). diff --git a/src/main/java/strman/Strman.java b/src/main/java/strman/Strman.java index 08ddd2b..70dc783 100644 --- a/src/main/java/strman/Strman.java +++ b/src/main/java/strman/Strman.java @@ -38,6 +38,9 @@ import static java.util.function.Function.identity; import static java.util.stream.Collectors.counting; import static java.util.stream.Collectors.groupingBy; import static java.util.stream.Collectors.joining; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static strman.Strman.isBlank; /** * A String manipulation library without any dependencies @@ -1305,6 +1308,16 @@ public abstract class Strman { .mapToObj(c -> (char) c) .collect(groupingBy(identity(), counting())); } + + /** + * Checks if string is empty. This is a null safe check and will return true when string is null. + * + * @param input The input string + * @return true if string is null or empty + */ + public static boolean isBlank(String input) { + return input == null || input.isEmpty(); + } private static void validate(String value, Predicate predicate, final Supplier supplier) { if (predicate.test(value)) { @@ -1325,5 +1338,6 @@ public abstract class Strman { } return countSubstr(value.substring(offset), subStr, allowOverlapping, ++count); } + } diff --git a/src/test/java/strman/StrmanTest.java b/src/test/java/strman/StrmanTest.java index 84f18f6..a4fcc8b 100644 --- a/src/test/java/strman/StrmanTest.java +++ b/src/test/java/strman/StrmanTest.java @@ -37,8 +37,6 @@ import static org.hamcrest.collection.IsArrayContainingInOrder.arrayContaining; import static org.hamcrest.collection.IsArrayWithSize.emptyArray; import static org.junit.Assert.*; import static strman.Strman.*; -import static strman.Strman.endsWith; -import static strman.Strman.format; public class StrmanTest { @@ -1027,4 +1025,11 @@ public class StrmanTest { assertThat(charsCount("-----abbcccCCCBBA-----"), equalTo(expectedOutput)); } + + @Test + public void testIsBlank() { + assertTrue(isBlank("")); + assertTrue(isBlank(null)); + assertFalse(isBlank("ac")); + } } \ No newline at end of file From fad14c5f40c7021d71e90839d56624204c8f88f3 Mon Sep 17 00:00:00 2001 From: Andy Deese Date: Fri, 19 May 2017 09:24:56 -0400 Subject: [PATCH 2/5] removing local files --- .classpath | 8 --- .gitignore | 74 ---------------------- .project | 23 ------- .settings/org.eclipse.buildship.core.prefs | 3 - 4 files changed, 108 deletions(-) delete mode 100644 .classpath delete mode 100644 .gitignore delete mode 100644 .project delete mode 100644 .settings/org.eclipse.buildship.core.prefs diff --git a/.classpath b/.classpath deleted file mode 100644 index 9f084aa..0000000 --- a/.classpath +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - diff --git a/.gitignore b/.gitignore deleted file mode 100644 index d6a4cf2..0000000 --- a/.gitignore +++ /dev/null @@ -1,74 +0,0 @@ -# Created by .ignore support plugin (hsz.mobi) -### JetBrains template -# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio - -*.iml - -## Directory-based project format: -.idea/ -# if you remove the above rule, at least ignore the following: - -# User-specific stuff: -# .idea/workspace.xml -# .idea/tasks.xml -# .idea/dictionaries - -# Sensitive or high-churn files: -# .idea/dataSources.ids -# .idea/dataSources.xml -# .idea/sqlDataSources.xml -# .idea/dynamic.xml -# .idea/uiDesigner.xml - -# Gradle: -# .idea/gradle.xml -# .idea/libraries - -# Mongo Explorer plugin: -# .idea/mongoSettings.xml - -## File-based project format: -*.ipr -*.iws - -## Plugin-specific files: - -# IntelliJ -/out/ - -# mpeltonen/sbt-idea plugin -.idea_modules/ - -# JIRA plugin -atlassian-ide-plugin.xml - -# Crashlytics plugin (for Android Studio and IntelliJ) -com_crashlytics_export_strings.xml -crashlytics.properties -crashlytics-build.properties -### Gradle template -.gradle -build/ - -# Ignore Gradle GUI config -gradle-app.setting - -# Avoid ignoring Gradle wrapper jar file (.jar files are usually ignored) -!gradle-wrapper.jar -### Java template -*.class - -# Mobile Tools for Java (J2ME) -.mtj.tmp/ - -# Package Files # -*.jar -*.war -*.ear - -# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml -hs_err_pid* - -!gradle/wrapper/gradle-wrapper.jar - -/bin/ diff --git a/.project b/.project deleted file mode 100644 index 0a4059d..0000000 --- a/.project +++ /dev/null @@ -1,23 +0,0 @@ - - - strman - Project strman created by Buildship. - - - - - org.eclipse.jdt.core.javabuilder - - - - - org.eclipse.buildship.core.gradleprojectbuilder - - - - - - org.eclipse.buildship.core.gradleprojectnature - org.eclipse.jdt.core.javanature - - diff --git a/.settings/org.eclipse.buildship.core.prefs b/.settings/org.eclipse.buildship.core.prefs deleted file mode 100644 index 03931c0..0000000 --- a/.settings/org.eclipse.buildship.core.prefs +++ /dev/null @@ -1,3 +0,0 @@ -connection.gradle.distribution=GRADLE_DISTRIBUTION(WRAPPER) -connection.project.dir= -eclipse.preferences.version=1 From f1e8e83cc05a8bb8b393130a58fec4a5316aa3fa Mon Sep 17 00:00:00 2001 From: Andy Deese Date: Fri, 19 May 2017 09:28:47 -0400 Subject: [PATCH 3/5] adding gitignore with Eclipse specific files ignored --- .gitignore | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6c9b373 --- /dev/null +++ b/.gitignore @@ -0,0 +1,78 @@ +# Created by .ignore support plugin (hsz.mobi) +### JetBrains template +# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio + +*.iml + +## Directory-based project format: +.idea/ +# if you remove the above rule, at least ignore the following: + +# User-specific stuff: +# .idea/workspace.xml +# .idea/tasks.xml +# .idea/dictionaries + +# Sensitive or high-churn files: +# .idea/dataSources.ids +# .idea/dataSources.xml +# .idea/sqlDataSources.xml +# .idea/dynamic.xml +# .idea/uiDesigner.xml + +# Gradle: +# .idea/gradle.xml +# .idea/libraries + +# Mongo Explorer plugin: +# .idea/mongoSettings.xml + +## File-based project format: +*.ipr +*.iws + +## Plugin-specific files: + +# IntelliJ +/out/ + +# mpeltonen/sbt-idea plugin +.idea_modules/ + +# JIRA plugin +atlassian-ide-plugin.xml + +# Crashlytics plugin (for Android Studio and IntelliJ) +com_crashlytics_export_strings.xml +crashlytics.properties +crashlytics-build.properties +### Gradle template +.gradle +build/ + +# Ignore Gradle GUI config +gradle-app.setting + +# Avoid ignoring Gradle wrapper jar file (.jar files are usually ignored) +!gradle-wrapper.jar +### Java template +*.class + +# Mobile Tools for Java (J2ME) +.mtj.tmp/ + +# Package Files # +*.jar +*.war +*.ear + +# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* + +!gradle/wrapper/gradle-wrapper.jar + +/bin/ + +.classpath +.project +.settings From 04b9f8a7b4d0d022c6a3e4ff36f958a04d5d2b13 Mon Sep 17 00:00:00 2001 From: Andy Deese Date: Fri, 19 May 2017 09:49:13 -0400 Subject: [PATCH 4/5] removed unused imports from Strman.java and split tests out to match current test convention --- src/main/java/strman/Strman.java | 3 --- src/test/java/strman/StrmanTest.java | 12 ++++++++++-- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/main/java/strman/Strman.java b/src/main/java/strman/Strman.java index 70dc783..8f2eaa1 100644 --- a/src/main/java/strman/Strman.java +++ b/src/main/java/strman/Strman.java @@ -38,9 +38,6 @@ import static java.util.function.Function.identity; import static java.util.stream.Collectors.counting; import static java.util.stream.Collectors.groupingBy; import static java.util.stream.Collectors.joining; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; -import static strman.Strman.isBlank; /** * A String manipulation library without any dependencies diff --git a/src/test/java/strman/StrmanTest.java b/src/test/java/strman/StrmanTest.java index a4fcc8b..328b962 100644 --- a/src/test/java/strman/StrmanTest.java +++ b/src/test/java/strman/StrmanTest.java @@ -1027,9 +1027,17 @@ public class StrmanTest { } @Test - public void testIsBlank() { - assertTrue(isBlank("")); + public void isBlank_shouldReturnTrueIfNull() { assertTrue(isBlank(null)); + } + + @Test + public void isBlank_shouldReturnTrueIfEmpty() { + assertTrue(isBlank("")); + } + + @Test + public void isBlank_shouldReturnFalseIfNotEmpty() { assertFalse(isBlank("ac")); } } \ No newline at end of file From 6bbcb5313842bb28c49041a28ed2ff594848ef9d Mon Sep 17 00:00:00 2001 From: Andy Deese Date: Sat, 20 May 2017 12:01:13 -0400 Subject: [PATCH 5/5] Added underscored method with tests and documentation --- README.md | 10 ++++++++++ src/main/java/strman/Strman.java | 28 +++++++++++++++++++++++++++- src/test/java/strman/StrmanTest.java | 16 ++++++++++++++++ 3 files changed, 53 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 9bca612..ba9bd6d 100644 --- a/README.md +++ b/README.md @@ -631,6 +631,16 @@ isBlank("test") // result => false) ``` +## underscored + +underscored returns a string that represents the passed in string in all lowercase and underscores +between words. + +```java +underscored("MozTransform") +// result => "moz_transform") +``` + ## Inspiration This library is inspired by [dleitee/strman](https://github.com/dleitee/strman). diff --git a/src/main/java/strman/Strman.java b/src/main/java/strman/Strman.java index 8f2eaa1..65ea031 100644 --- a/src/main/java/strman/Strman.java +++ b/src/main/java/strman/Strman.java @@ -1310,12 +1310,38 @@ public abstract class Strman { * Checks if string is empty. This is a null safe check and will return true when string is null. * * @param input The input string - * @return true if string is null or empty + * @return true if input string is null or empty */ public static boolean isBlank(String input) { return input == null || input.isEmpty(); } + /** + * Changes passed in string to all lower case and adds underscore between words. + * + * @param input The input string + * @return the input string in all lower case with underscores between words + */ + public static String underscored(String input) { + String result = null; + if (input != null) { + StringBuffer resultBuffer = new StringBuffer(); + char[] inputArray = input.toCharArray(); + for(int i = 0; i < inputArray.length; i++) { + char nextChar = inputArray[i]; + if (Character.isUpperCase(nextChar)) { + //start new word + if (resultBuffer.length() != 0) { + resultBuffer.append("_"); + } + } + resultBuffer.append(nextChar); + } + result = resultBuffer.toString().toLowerCase(); + } + return result; + } + private static void validate(String value, Predicate predicate, final Supplier supplier) { if (predicate.test(value)) { throw new IllegalArgumentException(supplier.get()); diff --git a/src/test/java/strman/StrmanTest.java b/src/test/java/strman/StrmanTest.java index 328b962..9e640c2 100644 --- a/src/test/java/strman/StrmanTest.java +++ b/src/test/java/strman/StrmanTest.java @@ -1040,4 +1040,20 @@ public class StrmanTest { public void isBlank_shouldReturnFalseIfNotEmpty() { assertFalse(isBlank("ac")); } + + @Test + public void underscored_shouldReturnUnderscoredString() { + assertThat(underscored("MozTransform"), equalTo("moz_transform")); + } + + @Test + public void underscored_shouldReturnEmptyStringIfEmptyStringPassedIn() { + assertThat(underscored(""), equalTo("")); + } + + @Test + public void underscored_shouldReturnNullIfNullPassedIn() { + assertThat(underscored(null), equalTo(null)); + } + } \ No newline at end of file