From 012d8dfad581ecdbd9a6633635bc22e6595d398a Mon Sep 17 00:00:00 2001 From: Gustavo Martin Morcuende Date: Sun, 19 Apr 2015 06:33:21 +0200 Subject: [PATCH] Symfony (kind of) validators: required check for null value in postvalidator 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. --- .../symfony/validator/PostValidatorBase.java | 8 +++- .../validator/PostValidatorBaseCriteria.java | 39 +++++++++++++++++ .../example/symfony/validator/ValidatorBase.java | 2 +- .../validator/main/ValidatorMainExample.java | 10 ++--- .../validator/service/ValidatorService.java | 7 ++-- .../service/impl/ValidatorServiceImpl.java | 49 +++++++++++++--------- 6 files changed, 83 insertions(+), 32 deletions(-) create mode 100644 Allgemeines/SymfonyKindOf/validators/src/de/example/symfony/validator/PostValidatorBaseCriteria.java diff --git a/Allgemeines/SymfonyKindOf/validators/src/de/example/symfony/validator/PostValidatorBase.java b/Allgemeines/SymfonyKindOf/validators/src/de/example/symfony/validator/PostValidatorBase.java index bab2496..788ebe1 100644 --- a/Allgemeines/SymfonyKindOf/validators/src/de/example/symfony/validator/PostValidatorBase.java +++ b/Allgemeines/SymfonyKindOf/validators/src/de/example/symfony/validator/PostValidatorBase.java @@ -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 index 0000000..eaaf273 --- /dev/null +++ b/Allgemeines/SymfonyKindOf/validators/src/de/example/symfony/validator/PostValidatorBaseCriteria.java @@ -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); + } + } + + /** + *

Checks if a String is whitespace, empty ("") or null.

+ * + *
+     * StringUtils.isBlank(null)      = true
+     * StringUtils.isBlank("")        = true
+     * StringUtils.isBlank(" ")       = true
+     * StringUtils.isBlank("bob")     = false
+     * StringUtils.isBlank("  bob  ") = false
+     * 
+ * + * @param str the String to check, may be null + * @return true 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; + } +} diff --git a/Allgemeines/SymfonyKindOf/validators/src/de/example/symfony/validator/ValidatorBase.java b/Allgemeines/SymfonyKindOf/validators/src/de/example/symfony/validator/ValidatorBase.java index d08703c..df7a694 100644 --- a/Allgemeines/SymfonyKindOf/validators/src/de/example/symfony/validator/ValidatorBase.java +++ b/Allgemeines/SymfonyKindOf/validators/src/de/example/symfony/validator/ValidatorBase.java @@ -55,7 +55,7 @@ public abstract class ValidatorBase { * @return true 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; diff --git a/Allgemeines/SymfonyKindOf/validators/src/de/example/symfony/validator/main/ValidatorMainExample.java b/Allgemeines/SymfonyKindOf/validators/src/de/example/symfony/validator/main/ValidatorMainExample.java index bb46501..cf50415 100644 --- a/Allgemeines/SymfonyKindOf/validators/src/de/example/symfony/validator/main/ValidatorMainExample.java +++ b/Allgemeines/SymfonyKindOf/validators/src/de/example/symfony/validator/main/ValidatorMainExample.java @@ -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 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(); diff --git a/Allgemeines/SymfonyKindOf/validators/src/de/example/symfony/validator/service/ValidatorService.java b/Allgemeines/SymfonyKindOf/validators/src/de/example/symfony/validator/service/ValidatorService.java index c252ed1..2250ae4 100644 --- a/Allgemeines/SymfonyKindOf/validators/src/de/example/symfony/validator/service/ValidatorService.java +++ b/Allgemeines/SymfonyKindOf/validators/src/de/example/symfony/validator/service/ValidatorService.java @@ -14,13 +14,11 @@ public abstract class ValidatorService { public ValidatorService(Map params) { - this.params = params; - - this.configureValidators(); - this.configurePostValidators(); + this.params = params; } public void doValidate() { + this.configureValidators(); for (Map.Entry 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 postValidator : postValidators.entrySet()) { postValidator.getValue().doPostValidate(params.get(postValidator.getKey())); diff --git a/Allgemeines/SymfonyKindOf/validators/src/de/example/symfony/validator/service/impl/ValidatorServiceImpl.java b/Allgemeines/SymfonyKindOf/validators/src/de/example/symfony/validator/service/impl/ValidatorServiceImpl.java index 09b35be..6abcf88 100644 --- a/Allgemeines/SymfonyKindOf/validators/src/de/example/symfony/validator/service/impl/ValidatorServiceImpl.java +++ b/Allgemeines/SymfonyKindOf/validators/src/de/example/symfony/validator/service/impl/ValidatorServiceImpl.java @@ -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 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); + } + + }); } } -- 2.1.4