Commits

Илья Казначеев committed 4011df3

Cf.String.is(Not)BlankF? along with tests.

Comments (0)

Files changed (2)

src/main/java/ru/yandex/bolts/type/StringType.java

         };
     }
 
+    public boolean isBlank(String s) {
+        if (s == null || s.isEmpty()) {
+            return true;
+        }
+
+        int len = s.length();
+        for (int i = 0; i < len; i++) {
+            if (!Character.isWhitespace(s.charAt(i))) {
+                return false;
+            }
+        }
+
+        return true;
+    }
+
+    public boolean isNotBlank(String s) {
+        return !isBlank(s);
+    }
+
+    public Function1B<String> isBlankF() {
+        return new Function1B<String>() {
+            public boolean apply(String s) {
+                return isBlank(s);
+            }
+        };
+    }
+
+    public Function1B<String> isNotBlankF() {
+        return new Function1B<String>() {
+            public boolean apply(String s) {
+                return isNotBlank(s);
+            }
+        };
+    }
+
     public Function2<String, String, String> joinF(String sep) {
         return plus3F().bind2(sep);
     }

src/main/java/ru/yandex/bolts/type/StringTypeTest.java

+package ru.yandex.bolts.type;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import ru.yandex.bolts.collection.Cf;
+import ru.yandex.bolts.collection.ListF;
+
+/**
+ * @author ilyak
+ */
+public class StringTypeTest {
+
+    @Test
+    public void blank() {
+        Assert.assertTrue(Cf.String.isBlank(null));
+        Assert.assertTrue(Cf.String.isBlank(""));
+        Assert.assertTrue(Cf.String.isBlank(" "));
+        Assert.assertTrue(Cf.String.isBlank("\t \n"));
+        Assert.assertFalse(Cf.String.isBlank("\t w\n"));
+        Assert.assertFalse(Cf.String.isBlank(" |\n"));
+        Assert.assertFalse(Cf.String.isBlank("1234"));
+
+        Assert.assertEquals(Cf.list("w"),
+                Cf.list("", " ", "w", "\n").filter(Cf.String.isNotBlankF()));
+        Assert.assertEquals(Cf.list("", " ", "\n"),
+                Cf.list("", " ", "w", "\n").filter(Cf.String.isBlankF()));
+    }
+
+} //~