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

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

Go to download

JSR 380's RI, Hibernate Validator version ${hibernate-validator.version} and its dependencies repackaged as OSGi bundle

There is a newer version: 5.1.0
Show newest version
/*
 * Hibernate Validator, declare and validate application constraints
 *
 * License: Apache License, Version 2.0
 * See the license.txt file in the root directory or .
 */
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.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Provides some methods for simplified collection instantiation.
 *
 * @author Gunnar Morling
 * @author Kevin Pollet <[email protected]> (C) 2011 SERLI
 * @author Hardy Ferentschik
 * @author Guillaume Smet
 */
public final class CollectionHelper {

	private CollectionHelper() {
	}

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

	public static  HashMap newHashMap(int size) {
		return new HashMap( getInitialCapacityFromExpectedSize( 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( getInitialCapacityFromExpectedSize( size ) );
	}

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

	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 );
	}

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

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

	public static  List toImmutableList(List list) {
		switch ( list.size() ) {
			case 0:
				return Collections.emptyList();
			case 1:
				return Collections.singletonList( list.get( 0 ) );
			default:
				return Collections.unmodifiableList( list );
		}
	}

	public static  Set toImmutableSet(Set set) {
		switch ( set.size() ) {
			case 0:
				return Collections.emptySet();
			case 1:
				return Collections.singleton( set.iterator().next() );
			default:
				return Collections.unmodifiableSet( set );
		}
	}

	public static  Map toImmutableMap(Map map) {
		switch ( map.size() ) {
			case 0:
				return Collections.emptyMap();
			case 1:
				Entry entry = map.entrySet().iterator().next();
				return Collections.singletonMap( entry.getKey(), entry.getValue() );
			default:
				return Collections.unmodifiableMap( map );
		}
	}

	/**
	 * As the default loadFactor is of 0.75, we need to calculate the initial capacity from the expected size to avoid
	 * resizing the collection when we populate the collection with all the initial elements. We use a calculation
	 * similar to what is done in {@link HashMap#putAll(Map)}.
	 *
	 * @param expectedSize the expected size of the collection
	 * @return the initial capacity of the collection
	 */
	private static int getInitialCapacityFromExpectedSize(int expectedSize) {
		if ( expectedSize < 3 ) {
			return expectedSize + 1;
		}
		return (int) ( (float) expectedSize / 0.75f + 1.0f );
	}

	/**
	 * Builds an {@link Iterator} for a given array. It is (un)necessarily ugly because we have to deal with array of primitives.
	 *
	 * @param object a given array
	 * @return an {@code Iterator} iterating over the array
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" }) // Reflection is used to ensure the correct types are used
	public static Iterator iteratorFromArray(Object object) {
		return new ArrayIterator( accessorFromArray( object ), object );
	}

	/**
	 * Builds an {@link Iterable} for a given array. It is (un)necessarily ugly because we have to deal with array of primitives.
	 *
	 * @param object a given array
	 * @return an {@code Iterable} providing iterators over the array
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" }) // Reflection is used to ensure the correct types are used
	public static Iterable iterableFromArray(Object object) {
		return new ArrayIterable( accessorFromArray( object ), object );
	}

	private static ArrayAccessor accessorFromArray(Object object) {
		ArrayAccessor accessor;
		if ( Object.class.isAssignableFrom( object.getClass().getComponentType() ) ) {
			accessor = ArrayAccessor.OBJECT;
		}
		else if ( object.getClass() == boolean[].class ) {
			accessor = ArrayAccessor.BOOLEAN;
		}
		else if ( object.getClass() == int[].class ) {
			accessor = ArrayAccessor.INT;
		}
		else if ( object.getClass() == long[].class ) {
			accessor = ArrayAccessor.LONG;
		}
		else if ( object.getClass() == double[].class ) {
			accessor = ArrayAccessor.DOUBLE;
		}
		else if ( object.getClass() == float[].class ) {
			accessor = ArrayAccessor.FLOAT;
		}
		else if ( object.getClass() == byte[].class ) {
			accessor = ArrayAccessor.BYTE;
		}
		else if ( object.getClass() == short[].class ) {
			accessor = ArrayAccessor.SHORT;
		}
		else if ( object.getClass() == char[].class ) {
			accessor = ArrayAccessor.CHAR;
		}
		else {
			throw new IllegalArgumentException( "Provided object " + object + " is not a supported array type" );
		}
		return accessor;
	}

	private static class ArrayIterable implements Iterable {
		private final ArrayAccessor accessor;
		private final A values;

		public ArrayIterable(ArrayAccessor accessor, A values) {
			this.accessor = accessor;
			this.values = values;
		}

		@Override
		public final Iterator iterator() {
			return new ArrayIterator<>( accessor, values );
		}
	}

	private static class ArrayIterator implements Iterator {
		private final ArrayAccessor accessor;
		private final A values;
		private int current = 0;

		public ArrayIterator(ArrayAccessor accessor, A values) {
			this.accessor = accessor;
			this.values = values;
		}

		@Override
		public boolean hasNext() {
			return current < accessor.size( values );
		}

		@Override
		public T next() {
			T result = accessor.get( values, current );
			current++;
			return result;
		}
	}

	private interface ArrayAccessor {

		int size(A array);

		T get(A array, int index);

		ArrayAccessor OBJECT = new ArrayAccessor() {
			@Override
			public int size(Object[] array) {
				return array.length;
			}

			@Override
			public Object get(Object[] array, int index) {
				return array[index];
			}
		};

		ArrayAccessor BOOLEAN = new ArrayAccessor() {
			@Override
			public int size(boolean[] array) {
				return array.length;
			}

			@Override
			public Boolean get(boolean[] array, int index) {
				return array[index];
			}
		};

		ArrayAccessor INT = new ArrayAccessor() {
			@Override
			public int size(int[] array) {
				return array.length;
			}

			@Override
			public Integer get(int[] array, int index) {
				return array[index];
			}
		};

		ArrayAccessor LONG = new ArrayAccessor() {
			@Override
			public int size(long[] array) {
				return array.length;
			}

			@Override
			public Long get(long[] array, int index) {
				return array[index];
			}
		};

		ArrayAccessor DOUBLE = new ArrayAccessor() {
			@Override
			public int size(double[] array) {
				return array.length;
			}

			@Override
			public Double get(double[] array, int index) {
				return array[index];
			}
		};

		ArrayAccessor FLOAT = new ArrayAccessor() {
			@Override
			public int size(float[] array) {
				return array.length;
			}

			@Override
			public Float get(float[] array, int index) {
				return array[index];
			}
		};

		ArrayAccessor BYTE = new ArrayAccessor() {
			@Override
			public int size(byte[] array) {
				return array.length;
			}

			@Override
			public Byte get(byte[] array, int index) {
				return array[index];
			}
		};

		ArrayAccessor SHORT = new ArrayAccessor() {
			@Override
			public int size(short[] array) {
				return array.length;
			}

			@Override
			public Short get(short[] array, int index) {
				return array[index];
			}
		};

		ArrayAccessor CHAR = new ArrayAccessor() {
			@Override
			public int size(char[] array) {
				return array.length;
			}

			@Override
			public Character get(char[] array, int index) {
				return array[index];
			}
		};
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy