diff --git a/src/main/java/strman/Strman.java b/src/main/java/strman/Strman.java index 32a477b..91dd892 100644 --- a/src/main/java/strman/Strman.java +++ b/src/main/java/strman/Strman.java @@ -118,12 +118,6 @@ public abstract class Strman { } - public static void validate(String value, Predicate predicate, final Supplier supplier) { - if (predicate.test(value)) { - throw new IllegalArgumentException(supplier.get()); - } - } - /** * Verifies that the needle is contained in the value. The search is case insensitive * @@ -547,7 +541,21 @@ public abstract class Strman { return append(Stream.generate(() -> pad).limit(length - value.length()).collect(joining()), value); } - public static String decode(final String value, int digits, int radix) { + /** + * Checks whether Object is String + * + * @param value The input String + * @return true if Object is a String false otherwise + */ + public static boolean isString(Object value) { + if (Objects.isNull(value)) { + throw new IllegalArgumentException("value can't be null"); + } + return value instanceof String; + } + + + public static String decode(final String value, final int digits, final int radix) { validate(value, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER); return Arrays .stream(value.split("(?<=\\G.{" + digits + "})")) @@ -555,11 +563,17 @@ public abstract class Strman { .collect(joining()); } - public static String encode(String value, int digits, int radix) { + public static String encode(final String value, final int digits, final int radix) { validate(value, NULL_STRING_PREDICATE, NULL_STRING_MSG_SUPPLIER); return value.chars().mapToObj(ch -> leftPad(Integer.toString(ch, radix), "0", digits)).collect(joining()); } + private static void validate(String value, Predicate predicate, final Supplier supplier) { + if (predicate.test(value)) { + throw new IllegalArgumentException(supplier.get()); + } + } + private static long countSubstr(String value, String subStr, boolean allowOverlapping, long count) { int position = value.indexOf(subStr); if (position == -1) { diff --git a/src/test/java/strman/StrmanTest.java b/src/test/java/strman/StrmanTest.java index ccd6b43..6a68c88 100644 --- a/src/test/java/strman/StrmanTest.java +++ b/src/test/java/strman/StrmanTest.java @@ -438,6 +438,19 @@ public class StrmanTest { assertThat(leftPad("001", "0", 5), equalTo("00001")); assertThat(leftPad("0001", "0", 5), equalTo("00001")); assertThat(leftPad("00001", "0", 5), equalTo("00001")); + } + @Test + public void isString_shouldBeFalseWhenValueIsNotString() throws Exception { + assertFalse(isString(1)); + assertFalse(isString(false)); + assertFalse(isString(1.2)); + assertFalse(isString(new String[]{})); + } + + @Test + public void isString_shouldBeTrueWhenValueIsString() throws Exception { + assertTrue(isString("string")); + assertTrue(isString("")); } } \ No newline at end of file