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

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

The newest version!
/*
 * #%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%
 */
package org.nuiton.validator;

import java.io.Serializable;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * Abstract provider of validator.
 *
 * @author Tony Chemit - [email protected]
 * @since 2.0
 */
public abstract class AbstractNuitonValidatorProvider implements NuitonValidatorProvider {

    protected final String name;
    protected Map, NuitonValidatorModel> models;

    protected static class ModelEntry implements Serializable {

        private static final long serialVersionUID = 1L;

        protected final Class type;

        protected final String context;

        protected final NuitonValidatorScope[] scopes;

        public static  ModelEntry createModelEntry(Class type,
                                                         String context,
                                                         NuitonValidatorScope... scopes) {
            return new ModelEntry<>(type, context, scopes);
        }

        protected ModelEntry(Class type,
                             String context,
                             NuitonValidatorScope... scopes) {
            this.type = type;
            this.context = context;
            this.scopes = scopes == null ? NuitonValidatorScope.values() : scopes;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (!(o instanceof ModelEntry)) return false;

            ModelEntry that = (ModelEntry) o;

            if (context != null ? !context.equals(that.context) : that.context != null)
                return false;
            if (!Arrays.equals(scopes, that.scopes)) return false;
            return type.equals(that.type);

        }

        @Override
        public int hashCode() {
            int result = type.hashCode();
            result = 31 * result + (context != null ? context.hashCode() : 0);
            result = 31 * result + Arrays.hashCode(scopes);
            return result;
        }
    }

    public AbstractNuitonValidatorProvider(String name) {
        this.name = name;
    }

    @Override
    public  NuitonValidatorModel getModel(Class type, String context, NuitonValidatorScope... scopes) {
        ModelEntry key = ModelEntry.createModelEntry(type, context, scopes);

        @SuppressWarnings({"unchecked"})
        NuitonValidatorModel model = (NuitonValidatorModel) getModels().get(key);

        if (model == null) {
            model = newModel(type, context, scopes);
        }
        return model;
    }

    protected Map, NuitonValidatorModel> getModels() {
        if (models == null) {
            models = new HashMap<>();
        }
        return models;
    }

    @Override
    public String getName() {
        return name;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy