Symfony (kind of) validators: required check for null value in postvalidator
authorGustavo Martin Morcuende <gu.martinm@gmail.com>
Sun, 19 Apr 2015 04:33:21 +0000 (06:33 +0200)
committerGustavo Martin Morcuende <gu.martinm@gmail.com>
Sun, 19 Apr 2015 04:33:21 +0000 (06:33 +0200)
Without this check I could use lambda expression.
In normal cases I shouln't need to check for null value in postvalidator because
validations where performed by the validators classes.

Allgemeines/SymfonyKindOf/validators/src/de/example/symfony/validator/PostValidatorBase.java
Allgemeines/SymfonyKindOf/validators/src/de/example/symfony/validator/PostValidatorBaseCriteria.java [new file with mode: 0644]
Allgemeines/SymfonyKindOf/validators/src/de/example/symfony/validator/ValidatorBase.java
Allgemeines/SymfonyKindOf/validators/src/de/example/symfony/validator/main/ValidatorMainExample.java
Allgemeines/SymfonyKindOf/validators/src/de/example/symfony/validator/service/ValidatorService.java
Allgemeines/SymfonyKindOf/validators/src/de/example/symfony/validator/service/impl/ValidatorServiceImpl.java

index bab2496..788ebe1 100644 (file)
@@ -1,7 +1,11 @@
 package de.example.symfony.validator;
 
 
-public interface PostValidatorBase {
+public abstract class PostValidatorBase {
 
-       void doPostValidate(String value);
+       public void doPostValidate(String value) {
+               this.postValidate(value);
+       }
+       
+       protected abstract void postValidate(String value);
 }
diff --git a/Allgemeines/SymfonyKindOf/validators/src/de/example/symfony/validator/PostValidatorBaseCriteria.java b/Allgemeines/SymfonyKindOf/validators/src/de/example/symfony/validator/PostValidatorBaseCriteria.java
new file mode 100644 (file)
index 0000000..eaaf273
--- /dev/null
@@ -0,0 +1,39 @@
+package de.example.symfony.validator;
+
+
+public abstract class PostValidatorBaseCriteria extends PostValidatorBase {
+
+       public void doPostValidate(String value) {
+               if (!isBlank(value)) {
+                       this.postValidate(value);
+               }
+       }
+       
+    /**
+     * <p>Checks if a String is whitespace, empty ("") or null.</p>
+     *
+     * <pre>
+     * StringUtils.isBlank(null)      = true
+     * StringUtils.isBlank("")        = true
+     * StringUtils.isBlank(" ")       = true
+     * StringUtils.isBlank("bob")     = false
+     * StringUtils.isBlank("  bob  ") = false
+     * </pre>
+     *
+     * @param str  the String to check, may be null
+     * @return <code>true</code> if the String is null, empty or whitespace
+     * @since 2.0
+     */
+    private static boolean isBlank(String str) {
+        int strLen;
+        if (str == null || (strLen = str.length()) == 0) {
+            return true;
+        }
+        for (int i = 0; i < strLen; i++) {
+            if ((Character.isWhitespace(str.charAt(i)) == false)) {
+                return false;
+            }
+        }
+        return true;
+    }
+}
index d08703c..df7a694 100644 (file)
@@ -55,7 +55,7 @@ public abstract class ValidatorBase {
      * @return <code>true</code> if the String is null, empty or whitespace
      * @since 2.0
      */
-    public static boolean isBlank(String str) {
+    private static boolean isBlank(String str) {
         int strLen;
         if (str == null || (strLen = str.length()) == 0) {
             return true;
index bb46501..cf50415 100644 (file)
@@ -7,17 +7,15 @@ import de.example.symfony.validator.service.impl.ValidatorServiceImpl;
 
 
 public class ValidatorMainExample {
-       private static final String FROMID = "fromId";
-       private static final String TOID = "toId";
-       private static final String DESCRIPTION = "description";
 
        public static void main(String[] args) {
                Map<String, String> params = new HashMap<>();
-               params.put(FROMID, "50");
-               params.put(TOID, "75");
-               params.put(DESCRIPTION, "Snake Eyes");
+               params.put(ValidatorServiceImpl.FROMID, "50");
+               params.put(ValidatorServiceImpl.TOID, "75");
+               params.put(ValidatorServiceImpl.DESCRIPTION, "Snake Eyes");
                
                ValidatorServiceImpl validatorService = new ValidatorServiceImpl(params);
+               
                validatorService.doValidate();
                
                validatorService.doPostValidate();
index c252ed1..2250ae4 100644 (file)
@@ -14,13 +14,11 @@ public abstract class ValidatorService {
        
        
        public ValidatorService(Map<String, String> params) {
-           this.params = params;
-           
-           this.configureValidators();
-           this.configurePostValidators();
+           this.params = params; 
     }
 
        public void doValidate() {
+               this.configureValidators();
                
                for (Map.Entry<String, ValidatorBase> validator : validators.entrySet()) {
                        validator.getValue().doValidate(params.get(validator.getKey()));
@@ -29,6 +27,7 @@ public abstract class ValidatorService {
        }
        
        public void doPostValidate() {
+               this.configurePostValidators();
                
                for (Map.Entry<String, PostValidatorBase> postValidator : postValidators.entrySet()) {
                        postValidator.getValue().doPostValidate(params.get(postValidator.getKey()));
index 09b35be..6abcf88 100644 (file)
@@ -2,18 +2,19 @@ package de.example.symfony.validator.service.impl;
 
 import java.util.Map;
 
+import de.example.symfony.validator.PostValidatorBaseCriteria;
 import de.example.symfony.validator.ValidatorNumber;
 import de.example.symfony.validator.ValidatorString;
 import de.example.symfony.validator.service.ValidatorService;
 
 
 public class ValidatorServiceImpl extends ValidatorService {
-       private static final String FROMID = "fromId";
-       private static final String TOID = "toId";
-       private static final String DESCRIPTION = "description";
+       public static final String FROMID = "fromId";
+       public static final String TOID = "toId";
+       public static final String DESCRIPTION = "description";
        
-       private String fromId;
-       private String toId;
+       private Long fromId;
+       private Long toId;
        private String description;
 
        public ValidatorServiceImpl(Map<String, String> params) {
@@ -29,26 +30,36 @@ public class ValidatorServiceImpl extends ValidatorService {
 
        @Override
     protected void configurePostValidators() {
-               
-               postValidators.put(FROMID, value ->
-               {
-                       fromId = value;
-               System.out.println("fromId: " + fromId); 
+       
+               postValidators.put(FROMID, new PostValidatorBaseCriteria() {
+
+                       @Override
+            protected void postValidate(String value) {
+                               fromId = Long.valueOf(value);
+                       System.out.println("fromId: " + fromId); 
+            }
+                       
                });
                
-               postValidators.put(TOID, value ->
-               {
-                       toId = value;
-               System.out.println("toId: " + toId);
+               postValidators.put(TOID, new PostValidatorBaseCriteria() {
 
+                       @Override
+            protected void postValidate(String value) {
+                               toId = Long.valueOf(value);
+                       System.out.println("toId: " + toId); 
+            }
+                       
                });
                
-               postValidators.put(DESCRIPTION, value ->
-               {
-                       description = value;
-               System.out.println("description: " + description);
+               postValidators.put(DESCRIPTION, new PostValidatorBaseCriteria() {
 
-               });  
+                       @Override
+            protected void postValidate(String value) {
+                               description = value;
+                       System.out.println("description: " + description); 
+            }
+                       
+               });
     }
 
 }