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

org.hibernate.validator.internal.util.CollectionHelper Maven / Gradle / Ivy

/*
* JBoss, Home of Professional Open Source
* Copyright 2010, 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.validator.internal.util;

import java.util.ArrayList;
import java.util.Arrays;
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;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Provides some methods for simplified collection instantiation.
 *
 * @author Gunnar Morling
 * @author Kevin Pollet  (C) 2011 SERLI
 * @author Hardy Ferentschik
 */
public final class CollectionHelper {

	private CollectionHelper() {
	}

	public static  HashMap newHashMap() {
		return new HashMap();
	}

	public static  HashMap newHashMap(int size) {
		return new HashMap( size );
	}

	public static  HashMap newHashMap(Map map) {
		return new HashMap( map );
	}

	public static  ConcurrentHashMap newConcurrentHashMap() {
		return new ConcurrentHashMap();
	}

	public static  HashSet newHashSet() {
		return new HashSet();
	}

	public static  HashSet newHashSet(int size) {
		return new HashSet( size );
	}

	public static  HashSet newHashSet(Collection c) {
		return new HashSet( c );
	}

	public static  HashSet newHashSet(Collection s1, Collection s2) {
		HashSet set = newHashSet( s1 );
		set.addAll( s2 );
		return set;
	}

	public static  HashSet newHashSet(Iterable iterable) {
		HashSet set = newHashSet();
		for ( T t : iterable ) {
			set.add( t );
		}
		return set;
	}

	public static  ArrayList newArrayList() {
		return new ArrayList();
	}

	public static  ArrayList newArrayList(int size) {
		return new ArrayList( size );
	}

	public static  ArrayList newArrayList(Iterable... iterables) {
		ArrayList resultList = newArrayList();
		for ( Iterable oneIterable : iterables ) {
			for ( T oneElement : oneIterable ) {
				resultList.add( oneElement );
			}
		}
		return resultList;
	}

	public static  Set asSet(T... ts) {
		return new HashSet( Arrays.asList( ts ) );
	}

	/**
	 * Creates a map containing the given list's values partitioned by the given
	 * partitioner.
	 *
	 * @param  The key type of the resulting map.
	 * @param  The element type of the list to be partitioned.
	 * @param list The list to be partitioned.
	 * @param partitioner The partitioner to be used for determining the partitions.
	 *
	 * @return A map containing the given list's values partitioned by the given
	 *         partitioner.
	 */
	public static  Map> partition(List list, Partitioner partitioner) {
		if ( list == null ) {
			return Collections.emptyMap();
		}

		Map> theValue = newHashMap();

		for ( V v : list ) {
			K key = partitioner.getPartition( v );

			List partition = theValue.get( key );
			if ( partition == null ) {
				partition = newArrayList();
				theValue.put( key, partition );
			}

			partition.add( v );
		}

		return theValue;
	}

	/**
	 * Creates a map containing the given set's values partitioned by the given
	 * partitioner.
	 *
	 * @param  The key type of the resulting map.
	 * @param  The element type of the set to be partitioned.
	 * @param set The set to be partitioned.
	 * @param partitioner The partitioner to be used for determining the partitions.
	 *
	 * @return A map containing the given set's values partitioned by the given
	 *         partitioner.
	 */
	public static  Map> partition(Set set, Partitioner partitioner) {
		if ( set == null ) {
			return Collections.emptyMap();
		}

		Map> theValue = newHashMap();

		for ( V v : set ) {
			K key = partitioner.getPartition( v );

			Set partition = theValue.get( key );
			if ( partition == null ) {
				partition = newHashSet();
				theValue.put( key, partition );
			}

			partition.add( v );
		}

		return theValue;
	}

	public interface Partitioner {
		K getPartition(V v);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy