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

org.hibernate.validator.cfg.ConstraintMapping Maven / Gradle / Ivy

// $Id$
/*
 * JBoss, Home of Professional Open Source
 * Copyright 2010, Red Hat, Inc. and/or its affiliates, and individual contributors
 * by the @authors tag. See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.hibernate.validator.cfg;

import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Top level class for constraints configured via the programmatic API.
 *
 * @author Hardy Ferentschik
 */
public class ConstraintMapping {
	private final Map, List>> constraintConfig;
	private final Map, List> cascadeConfig;
	private final Set> configuredClasses;
	private final Map, List>> defaultGroupSequences;

	public ConstraintMapping() {
		this.constraintConfig = new HashMap, List>>();
		this.cascadeConfig = new HashMap, List>();
		this.configuredClasses = new HashSet>();
		this.defaultGroupSequences = new HashMap, List>>();
	}

	/**
	 * Starts defining constraints on the specified bean class.
	 *
	 * @param beanClass The bean class on which to define constraints. All constraints defined after calling this method
	 * are added to the bean of the type {@code beanClass} until the next call of {@code type}.
	 *
	 * @return Instance allowing for defining constraints on the specified class.
	 */
	public final ConstraintsForType type(Class beanClass) {
		return new ConstraintsForType( beanClass, this );
	}

	public final  Map, List>> getConstraintConfig() {
		Map, List>> newDefinitions = new HashMap, List>>();
		for ( Map.Entry, List>> entry : constraintConfig.entrySet() ) {

			List> newList = new ArrayList>();
			for ( ConstraintDef definition : entry.getValue() ) {
				Class beanClass = definition.beanType;
				@SuppressWarnings("unchecked")
				ConstraintDefWrapper defAccessor = new ConstraintDefWrapper(
						beanClass,
						( Class ) definition.constraintType,
						definition.property,
						definition.elementType,
						definition.parameters,
						this
				);
				newList.add( defAccessor );
			}
			newDefinitions.put( entry.getKey(), newList );
		}
		return newDefinitions;
	}

	public final Map, List> getCascadeConfig() {
		return cascadeConfig;
	}

	public final Collection> getConfiguredClasses() {
		return configuredClasses;
	}

	public final List> getDefaultSequence(Class beanType) {
		if ( defaultGroupSequences.containsKey( beanType ) ) {
			return defaultGroupSequences.get( beanType );
		}
		else {
			return Collections.emptyList();
		}
	}

	@Override
	public String toString() {
		final StringBuilder sb = new StringBuilder();
		sb.append( "ConstraintMapping" );
		sb.append( "{cascadeConfig=" ).append( cascadeConfig );
		sb.append( ", constraintConfig=" ).append( constraintConfig );
		sb.append( ", configuredClasses=" ).append( configuredClasses );
		sb.append( ", defaultGroupSequences=" ).append( defaultGroupSequences );
		sb.append( '}' );
		return sb.toString();
	}

	protected final void addCascadeConfig(CascadeDef cascade) {
		Class beanClass = cascade.getBeanType();
		configuredClasses.add( beanClass );
		if ( cascadeConfig.containsKey( beanClass ) ) {
			cascadeConfig.get( beanClass ).add( cascade );
		}
		else {
			List cascadeList = new ArrayList();
			cascadeList.add( cascade );
			cascadeConfig.put( beanClass, cascadeList );
		}
	}

	protected final void addDefaultGroupSequence(Class beanClass, List> defaultGroupSequence) {
		defaultGroupSequences.put( beanClass, defaultGroupSequence );
	}

	protected final void addConstraintConfig(ConstraintDef definition) {
		Class beanClass = definition.beanType;
		configuredClasses.add( beanClass );
		if ( constraintConfig.containsKey( beanClass ) ) {
			constraintConfig.get( beanClass ).add( definition );
		}
		else {
			List> definitionList = new ArrayList>();
			definitionList.add( definition );
			constraintConfig.put( beanClass, definitionList );
		}
	}
}






© 2015 - 2025 Weber Informatics LLC | Privacy Policy