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.
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);
}
--- /dev/null
+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;
+ }
+}
* @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;
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();
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()));
}
public void doPostValidate() {
+ this.configurePostValidators();
for (Map.Entry<String, PostValidatorBase> postValidator : postValidators.entrySet()) {
postValidator.getValue().doPostValidate(params.get(postValidator.getKey()));
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) {
@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);
+ }
+
+ });
}
}