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

org.lockss.util.SetUtil Maven / Gradle / Ivy

The newest version!
/*

Copyright (c) 2000, Board of Trustees of Leland Stanford Jr. University
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software without
specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

*/

package org.lockss.util;

import java.util.*;
import java.util.function.*;

/**
 * 

* {@link Set} utilities. *

* * @since 1.1.0 */ public class SetUtil { /** *

* Prevents instantiation. *

* * @since 1.1.0 */ private SetUtil() { // Prevent instantiation } /** *

* Creates a modifiable set from any number of arguments. *

* * @param elements * A succession of elements (possibly zero). * @return A modifiable {@link Set} of those elements (an empty set if no * elements). * @param * The type of element contained in the set. * @since 1.1.0 * @see CollectionUtil2#collection(Supplier, Object...) */ public static Set set(T... elements) { return CollectionUtil2.collection(HashSet::new, elements); } /** *

* Checks that all elements of the set are of the specified type, and returns * an unmodifiable copy of the set, with null elements disallowed. *

* * @param set * The input set. * @param type * The class with which all items of the set must be * assignment-compatible. * @param * The type of element contained in the set. * @throws NullPointerException * if the set is null or if any element is null. * @throws ClassCastException * if an item is not of the proper type. * @since 1.1.0 * @see #immutableSetOfType(Set, Class, boolean) */ public static Set immutableSetOfType(Set set, Class type) { return immutableSetOfType(set, type, false); } /** *

* Checks that all elements of the set are of the specified type, and returns * an unmodifiable copy of the set, with null elements allowed. *

* * @param set * The input set. * @param type * The class with which all items of the set must be * assignment-compatible. * @param * The type of element contained in the set. * @throws NullPointerException * if the set is null * @throws ClassCastException * if an item is not of the proper type. * @since 1.1.0 * @see #immutableSetOfType(Set, Class, boolean) */ public static Set immutableSetOfTypeOrNull(Set set, Class type) { return immutableSetOfType(set, type, true); } /** *

* Checks that all elements of the set are of the specified type, and returns * an unmodifiable copy of the set, with null elements either allowed or * disallowed. *

* * @param set * The input set. * @param type * The class with which all items of the set must be * assignment-compatible. * @param nullOk * Whether null elements are allowed. * @param * The type of element contained in the set. * @throws NullPointerException * if the set is null or if any element is null. * @throws ClassCastException * if an item is not of the proper type. * @since 1.1.0 * @see CollectionUtil2#immutableCollectionOfType(IntFunction, UnaryOperator, Collection, Class, boolean) */ private static Set immutableSetOfType(Set set, Class type, boolean nullOk) { return CollectionUtil2.immutableCollectionOfType((IntFunction>)HashSet::new, Collections::unmodifiableSet, set, type, nullOk); } /** *

* Now that {@link #set(Object...)} is generic, this method does * exactly the same thing and simply calls it. *

* * @param array * An array of objects. * @param * The type of element contained in the set. * @return A modifiable {@link Set} made of the objects in the array. * @since 1.1.0 * @deprecated Use {@link #set(Object...)} instead. * @see #set(Object...) */ @Deprecated public static Set fromArray(T[] array) { return set(array); } /** *

* Create a set containing the elements of the given collection. *

* * @param coll * A collection. * @param * The type of element contained in the set. * @return A set made from the elements of the given collection. * @since 1.1.0 * @see HashSet#HashSet(Collection) */ public static Set theSet(Collection coll) { return new HashSet(coll); } /** *

* Create a set containing the elements of the given list. *

* * @param list * A list. * @param * The type of element contained in the set. * @return A set made from the elements of the given list. * @since 1.1.0 * @see #theSet(Collection) */ public static Set fromList(List list) { return theSet(list); } /** *

* Creates a set containing the elements of the given iterator. *

* * @param iterator * An iterator. * @param * The type of element contained in the set. * @return A set built from consuming the iterator. * @since 1.1.0 * @see CollectionUtil2#fromIterator(Supplier, Iterator) */ public static Set fromIterator(Iterator iterator) { return CollectionUtil2.fromIterator(HashSet::new, iterator); } /** *

* Creates a set containing the elements of the given iterable. *

* * @param iterable * An iterable. * @param * The type of element contained in the set. * @return A set built from the elements in the iterable. * @since 1.1.0 * @see CollectionUtil2#fromIterable(Supplier, Iterable) */ public static Set fromIterable(Iterable iterable) { return fromIterator(iterable.iterator()); } /** *

* Creates a set containing the elements of a comma separated string, with * the caveat that the processing is done by {@link StringTokenizer} with the * delimiter ",". *

*

* {@link StringTokenizer} simplistically looks for separators without a * quoting mechanism to return tokens containing the delimiter, nor does it * trim whitespace between tokens and delimiters. It also does not return * empty tokens, so calling this method with ",,," will return an * empty set, not a set of four empty strings. *

* * @param csv * A simplistic CSV string. * @return A set of tokens as separated by commas in the given input string. * @since 1.1.0 * @see CollectionUtil2#fromCsvStringTokenizer(Supplier, String) **/ public static Set fromCSV(String csv) { return CollectionUtil2.fromCsvStringTokenizer(HashSet::new, csv); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy