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

org.meeuw.i18n.regions.validation.RegionValidatorService Maven / Gradle / Ivy

Go to download

Provides the service loader for Regions, and some utilities to deal with regions generally

There is a newer version: 2.1.0
Show newest version
package org.meeuw.i18n.regions.validation;

import java.util.Collections;
import java.util.Set;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.logging.Logger;

import javax.validation.*;

import org.checkerframework.checker.nullness.qual.NonNull;
import org.meeuw.i18n.regions.RegionService;

/**
 * Makes validation settings (via annotation) available as a {@link Predicate}.
 * @author Michiel Meeuwissen
 * @since 0.1
 */
public class RegionValidatorService {
    private static final Logger logger = Logger.getLogger(RegionValidatorService.class.getName());



    private final static Validator VALIDATOR;

    static {
        Validator proposal;
        try {
            ValidatorFactory factory = Validation.byDefaultProvider()
                .configure()
                .buildValidatorFactory();

            proposal = factory.getValidator();
        } catch (Exception e) {
            logger.warning(e.getMessage());
            proposal = null;
        }
        VALIDATOR = proposal;
    }

    private static final RegionValidatorService INSTANCE = new RegionValidatorService();


    public static RegionValidatorService getInstance() {
        return INSTANCE;
    }

    private RegionValidatorService() {
    }

    /**
     * Returns the validation info for the given property of the name as a {@link Predicate}, which can e.g. be used to {@link java.util.stream.Stream#filter(Predicate)} the results of {@link RegionService#values()}
     * @param clazz
     * @param propertyName  The property which is annotation with javax.validation annotation's like {@link ValidRegion}.
     * @param groups
     * @return
     */
    public Predicate fromProperty(
        @NonNull Class clazz,
        @NonNull String propertyName,
        @NonNull Class... groups) {
        return (o) -> VALIDATOR.validateValue(clazz, propertyName, o, groups).isEmpty();
    }

      /**
     * Returns the validation info for the given property of the name as a {@link Predicate}, which can e.g. be used to {@link java.util.stream.Stream#filter(Predicate)} the results of {@link RegionService#values()}
     * @param clazz
     * @param propertyName  The property which is annotation with javax.validation annotation's like {@link ValidRegion}.
     * @param groups
     * @return
     */
    public Predicate fromListProperty(
        @NonNull Class clazz,
        @NonNull String propertyName,
        @NonNull Class... groups) {
        return fromWrappedProperty(clazz, propertyName, Collections::singletonList, groups);
    }

     public Predicate fromSetProperty(
        @NonNull Class clazz,
        @NonNull String propertyName,
        @NonNull Class... groups) {
        return fromWrappedProperty(clazz, propertyName, Collections::singleton, groups);
    }

    /**
     * Returns the validation info for the given property of the name as a {@link Predicate}, which can e.g. be used to {@link java.util.stream.Stream#filter(Predicate)} the results of {@link RegionService#values()}
     * @param clazz
     * @param propertyName  The property which is annotation with javax.validation annotation's like {@link ValidRegion}.
     * @param groups
     * @return
     */
    public @NonNull Predicate fromWrappedProperty(
        @NonNull Class clazz,
        @NonNull String propertyName,
        @NonNull Function wrapper,
        @NonNull Class... groups
    ) {
        return o -> functionFromWrappedProperty(clazz, propertyName, wrapper, groups).apply(o).isEmpty();
    }

    public @NonNull Function>> functionFromWrappedProperty(
        @NonNull Class clazz,
        @NonNull String propertyName,
        @NonNull Function wrapper,
        @NonNull Class... groups
    ) {
        return o -> {
            Object apply = wrapper.apply(o);
            return VALIDATOR.validateValue(clazz, propertyName, apply, groups);
        };
    }

    public Validator getValidator() {
        return VALIDATOR;
    }

}