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

org.nuiton.validator.AbstractNuitonValidator Maven / Gradle / Ivy

The newest version!
package org.nuiton.validator;

/*-
 * #%L
 * Validation :: API
 * %%
 * Copyright (C) 2021 - 2024 Ultreia.io
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program.  If not, see
 * .
 * #L%
 */

import java.util.Arrays;
import java.util.Collections;
import java.util.EnumMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * Created on 26/01/2024.
 *
 * @author Tony Chemit - [email protected]
 * @since 2.0.0
 */
public abstract class AbstractNuitonValidator implements NuitonValidator {

    /**
     * Bean validation model.
     */
    private final NuitonValidatorModel model;

    /**
     * Bean scope validators.
     */
    private final Map> validators;


    protected abstract NuitonScopeValidator createScopeValidator(String context, NuitonValidatorScope scope, Class type, Set fields);

    protected AbstractNuitonValidator(NuitonValidatorModel model) {

        this.model = Objects.requireNonNull(model);

        Map> validators = new EnumMap<>(NuitonValidatorScope.class);

        Class type = model.getType();
        String context = model.getContext();

        Map fieldsMap = model.getFields();

        for (Map.Entry entry : fieldsMap.entrySet()) {

            NuitonValidatorScope scope = entry.getKey();

            Set fields = new HashSet<>(Arrays.asList(entry.getValue()));
            NuitonScopeValidator newValidator = createScopeValidator(context, scope, type, fields);

            validators.put(scope, newValidator);
        }
        this.validators = Collections.unmodifiableMap(validators);
    }

    @Override
    public NuitonValidatorModel getModel() {
        return model;
    }

    @Override
    public NuitonValidatorResult validate(O object, NuitonValidationContext validationContext) throws NullPointerException {

        Objects.requireNonNull(object);

        NuitonValidatorResult result = new NuitonValidatorResult();

        for (Map.Entry> entry : getValidators().entrySet()) {
            NuitonValidatorScope scope = entry.getKey();
            NuitonScopeValidator validator = entry.getValue();
            Map> newMessages = validator.validate(object, validationContext);
            result.addMessagesForScope(scope, newMessages);
        }
        return result;
    }

    @Override
    public Set getEffectiveScopes() {
        return getValidators().keySet();
    }

    @Override
    public Set getEffectiveFields() {
        return getValidators().values().stream().flatMap(s -> s.getFieldNames().stream()).collect(Collectors.toSet());
    }

    @Override
    public Set getEffectiveFields(NuitonValidatorScope scope) {
        Set result = new HashSet<>();
        NuitonScopeValidator scopeValidator = getValidators().get(scope);
        if (scopeValidator != null) {
            result.addAll(scopeValidator.getFieldNames());
        }
        return result;
    }

    protected Map> getValidators() {
        return validators;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy