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

org.faktorips.runtime.ValidationContext Maven / Gradle / Ivy

Go to download

Runtime library for Faktor-IPS. When using the JAXB support use either faktorips-runtime-jakarta-xml or faktorips-runtime-javax-xml as dependency. When using CSV to read tables, add the optional dependencies to opencsv, commons-lang3 and commons-text. If you want to run Faktor-IPS tests as JUnit tests, you need to provide either junit (JUnit 4) or junit-jupiter-api (JUnit 5).

There is a newer version: 25.1.0.a20241030-01
Show newest version
/*******************************************************************************
 * Copyright (c) Faktor Zehn GmbH - faktorzehn.org
 * 
 * This source code is available under the terms of the AGPL Affero General Public License version
 * 3.
 * 
 * Please see LICENSE.txt for full license terms, including the additional permissions and
 * restrictions as well as the possibility of alternative license terms.
 *******************************************************************************/

package org.faktorips.runtime;

import static java.util.Objects.requireNonNull;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import org.faktorips.runtime.validation.DefaultGenericAttributeValidationConfiguration;
import org.faktorips.runtime.validation.IGenericAttributeValidationConfiguration;

/**
 * Default implementation of {@link IValidationContext}.
 */
public class ValidationContext implements IValidationContext {

    private final Locale locale;

    private final Map propertyValues = new HashMap<>();

    private final ClassLoader resourceClassLoader;

    private final IGenericAttributeValidationConfiguration genericAttributeValidationConfiguration;

    /**
     * Creates a new validation context with the specified locale, resource {@link ClassLoader} and
     * {@link IGenericAttributeValidationConfiguration}.
     * 
     * @param locale Setting the locale of this context
     * @param resourceClassLoader setting the {@link ClassLoader} used to load resources
     * 
     * @throws NullPointerException if one of the specified parameters is {@code null}
     */
    public ValidationContext(Locale locale, ClassLoader resourceClassLoader,
            IGenericAttributeValidationConfiguration genericAttributeValidationConfiguration) {
        this.locale = requireNonNull(locale, "The parameter locale cannot be null.");
        this.resourceClassLoader = requireNonNull(resourceClassLoader,
                "The parameter resourceClassLoader cannot be null.");
        this.genericAttributeValidationConfiguration = requireNonNull(genericAttributeValidationConfiguration,
                "The parameter genericAttributeValidationConfiguration cannot be null.");
    }

    /**
     * Creates a new validation context with the specified locale, resource {@link ClassLoader} and
     * a {@link DefaultGenericAttributeValidationConfiguration}.
     * 
     * @param locale Setting the locale of this context
     * @param resourceClassLoader setting the {@link ClassLoader} used to load resources
     * 
     * @throws NullPointerException if one of the specified parameters is {@code null}
     */
    public ValidationContext(Locale locale, ClassLoader resourceClassLoader) {
        this(locale, resourceClassLoader, new DefaultGenericAttributeValidationConfiguration(locale));
    }

    /**
     * Creates a new validation context with the specified locale and a
     * {@link DefaultGenericAttributeValidationConfiguration}.
     * 
     * @throws NullPointerException if the specified locale is {@code null}
     */
    public ValidationContext(Locale locale) {
        this(locale, ValidationContext.class.getClassLoader());
    }

    /**
     * Creates a new validation context with the default locale ({@link java.util.Locale}
     * .getDefault()) and a {@link DefaultGenericAttributeValidationConfiguration}.
     */
    public ValidationContext() {
        this(Locale.getDefault());
    }

    @Override
    public Locale getLocale() {
        return locale;
    }

    @Override
    public Object getValue(String propertyName) {
        return propertyValues.get(propertyName);
    }

    /**
     * Sets the value of the specified property.
     * 
     * @param propertyName the name of the property
     * @param value the value of the property
     */
    public void setValue(String propertyName, Object value) {
        propertyValues.put(propertyName, value);
    }

    /**
     * @return Returns the resourceClassLoader.
     */
    public ClassLoader getResourceClassLoader() {
        return resourceClassLoader;
    }

    /**
     * {@inheritDoc}
     *
     * @implNote This implementation returns the configuration passed to the
     *               {@link #ValidationContext(Locale, ClassLoader, IGenericAttributeValidationConfiguration)
     *               constructor}.
     */
    @Override
    public IGenericAttributeValidationConfiguration getGenericAttributeValidationConfiguration() {
        return genericAttributeValidationConfiguration;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy