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

org.hibernate.search.util.impl.CollectionHelper Maven / Gradle / Ivy

There is a newer version: 5.11.12.Final
Show newest version
/*
 * Hibernate Search, full-text search for your domain model
 *
 * License: GNU Lesser General Public License (LGPL), version 2.1 or later
 * See the lgpl.txt file in the root directory or .
 */

package org.hibernate.search.util.impl;

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.Set;
import java.util.SortedMap;
import java.util.TreeMap;

/**
 * Provides some methods for simplified collection instantiation.
 *
 * @author Gunnar Morling
 * @author Hardy Ferentschik
 */
public final class CollectionHelper {

	private CollectionHelper() {
		// not allowed
	}

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

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

	public static  SortedMap newSortedMap() {
		return new TreeMap();
	}

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

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

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

	public static  Set asSet(T... ts) {
		HashSet set = new HashSet( ts.length );
		Collections.addAll( set, ts );
		return set;
	}

	public static  List toImmutableList(final Collection c) {
		if ( c.isEmpty() ) {
			return Collections.emptyList();
		}
		else {
			return Collections.unmodifiableList( new ArrayList( c ) );
		}
	}

	public static Set asImmutableSet(String[] names) {
		//The intention here is to save some memory by picking the simplest safe representation,
		// as we usually require immutable sets for long living metadata:
		if ( names == null || names.length == 0 ) {
			return Collections.emptySet();
		}
		else if ( names.length == 1 ) {
			return Collections.singleton( names[0] );
		}
		else {
			HashSet hashSet = new HashSet<>( Arrays.asList( names ) );
			return Collections.unmodifiableSet( hashSet );
		}
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy