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

org.hibernate.validation.engine.groups.GroupChain Maven / Gradle / Ivy

// $Id: GroupChain.java 17107 2009-07-16 14:00:06Z hardy.ferentschik $
/*
* JBoss, Home of Professional Open Source
* Copyright 2008, Red Hat Middleware LLC, 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.validation.engine.groups;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.validation.GroupDefinitionException;
import javax.validation.groups.Default;

/**
 * An instance of {@code GroupChain} defines the group order during one full validation call.
 *
 * @author Hardy Ferentschik
 */
public class GroupChain {

	/**
	 * The list of single groups to be used this validation.
	 */
	private List groupList = new ArrayList();

	/**
	 * The different sequences for this validation. The map contains the list of groups mapped to their sequence
	 * name.
	 */
	private Map, List> sequenceMap = new HashMap, List>();

	public Iterator getGroupIterator() {
		return groupList.iterator();
	}

	public Iterator> getSequenceIterator() {
		return sequenceMap.values().iterator();
	}

	public Iterator getAllGroupsUnordered() {
		Set allGroups = new HashSet();
		allGroups.addAll( groupList );
		for ( List sequenceList : sequenceMap.values() ) {
			allGroups.addAll( sequenceList );
		}
		return allGroups.iterator();
	}

	public boolean containsSequence(Class groupSequence) {
		return sequenceMap.containsKey( groupSequence );
	}

	void insertGroup(Group group) {
		if ( !groupList.contains( group ) ) {
			groupList.add( group );
		}
	}

	void insertSequence(List groups) {
		if ( groups == null || groups.size() == 0 ) {
			return;
		}

		if ( !sequenceMap.containsValue( groups ) ) {
			sequenceMap.put( groups.get( 0 ).getSequence(), groups );
		}
	}

	@Override
	public String toString() {
		return "GroupChain{" +
				"groupList=" + groupList +
				", sequenceMap=" + sequenceMap +
				'}';
	}

	public void assertDefaulGroupSequenceIsExpandable(List> defaultGroupSequence) {
		for ( Map.Entry, List> entry : sequenceMap.entrySet() ) {
			Class sequence = entry.getKey();
			List groupList = entry.getValue();
			List defaultGroupList = buildTempGroupList( defaultGroupSequence, sequence );
			int defaultGroupIndex = containsDefaultGroupAtIndex( sequence, groupList );
			if ( defaultGroupIndex != -1 ) {
				ensureDefaultGroupSequenceIsExpandable( groupList, defaultGroupList, defaultGroupIndex );
			}
		}
	}

	private void ensureDefaultGroupSequenceIsExpandable(List groupList, List defaultGroupList, int defaultGroupIndex) {
		for ( int i = 0; i < defaultGroupList.size(); i++ ) {
			Group group = defaultGroupList.get( i );
			if ( group.getGroup().equals( Default.class ) ) {
				continue; // we don't have to consider the default group since it is the one we want to replace
			}
			int index = groupList.indexOf( group ); // check whether the sequence contains group of the default group sequence
			if ( index == -1 ) {
				continue; // if the group is not in the sequence we can continue
			}

			if ( ( i == 0 && index == defaultGroupIndex - 1 ) || ( i == defaultGroupList.size() - 1 && index == defaultGroupIndex + 1 ) ) {
				// if we are at the beginning or end of he defaultGroupSequence and the matches are either directly before resp after we can continue as well,
				// since we basically have two groups
				continue;
			}
			throw new GroupDefinitionException( "Unable to expand default group list" + defaultGroupList + " into sequence " + groupList );
		}
	}

	private int containsDefaultGroupAtIndex(Class sequence, List groupList) {
		Group defaultGroup = new Group( Default.class, sequence );
		return groupList.indexOf( defaultGroup );
	}

	private List buildTempGroupList(List> defaultGroupSequence, Class sequence) {
		List groupList = new ArrayList();
		for ( Class clazz : defaultGroupSequence ) {
			Group g = new Group( clazz, sequence );
			groupList.add( g );
		}
		return groupList;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy