All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cn.allbs.hj212.validator.clazz.T212MapFieldValidator Maven / Gradle / Ivy

There is a newer version: 2.0.0
Show newest version
package cn.allbs.hj212.validator.clazz;


import cn.allbs.hj212.model.verify.T212Map;

import javax.validation.ConstraintValidator;
import java.lang.annotation.Annotation;
import java.util.Optional;
import java.util.function.Predicate;
import java.util.regex.Pattern;

/**
 * @author ChenQi
 */
public abstract class T212MapFieldValidator
        extends FieldValidator, AF, String> {

    private Predicate predicate;

    public T212MapFieldValidator(ConstraintValidator constraintValidator) {
        super(constraintValidator);
    }

    @Override
    public void initialize(A a) {
        super.initialize(a);
        if (isFieldRegex(a)) {
            predicate = Pattern.compile(field).asPredicate();
        }
    }

    public abstract boolean isFieldRegex(A a);

    @Override
    public String getFieldValue(T212Map value, String field) {
        if (predicate != null) {
            Optional optional = value.keySet()
                    .stream()
                    .filter(key -> predicate.test(key))
                    .findFirst();
            if (optional.isPresent()) {
                return (String) value.get(optional.get());
            }
        }
        if (!value.containsKey(field)) {
            return null;
        }
        return (String) value.get(field);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy