com.helger.commons.collection.CollectionHelper Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ph-commons Show documentation
Show all versions of ph-commons Show documentation
Java 1.8+ Library with tons of utility classes required in all projects
/*
* Copyright (C) 2014-2022 Philip Helger (www.helger.com)
* philip[at]helger[dot]com
*
* 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 com.helger.commons.collection;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.EnumSet;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.NavigableMap;
import java.util.NavigableSet;
import java.util.Objects;
import java.util.Set;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import javax.annotation.Nonnegative;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;
import com.helger.commons.ValueEnforcer;
import com.helger.commons.annotation.CodingStyleguideUnaware;
import com.helger.commons.annotation.PresentForCodeCoverage;
import com.helger.commons.annotation.ReturnsImmutableObject;
import com.helger.commons.annotation.ReturnsMutableCopy;
import com.helger.commons.annotation.ReturnsMutableObject;
import com.helger.commons.collection.impl.CommonsArrayList;
import com.helger.commons.collection.impl.CommonsHashMap;
import com.helger.commons.collection.impl.CommonsHashSet;
import com.helger.commons.collection.impl.CommonsLinkedHashMap;
import com.helger.commons.collection.impl.CommonsLinkedHashSet;
import com.helger.commons.collection.impl.CommonsTreeMap;
import com.helger.commons.collection.impl.CommonsTreeSet;
import com.helger.commons.collection.impl.ICommonsList;
import com.helger.commons.collection.impl.ICommonsMap;
import com.helger.commons.collection.impl.ICommonsOrderedMap;
import com.helger.commons.collection.impl.ICommonsSet;
import com.helger.commons.collection.iterate.IIterableIterator;
import com.helger.commons.lang.ClassHelper;
import com.helger.commons.state.EChange;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
/**
* Provides various helper methods to handle collections like {@link List},
* {@link Set} and {@link Map}.
*
* @author Philip Helger
*/
@Immutable
@SuppressFBWarnings ("JCIP_FIELD_ISNT_FINAL_IN_IMMUTABLE_CLASS")
public final class CollectionHelper
{
@PresentForCodeCoverage
private static final CollectionHelper INSTANCE = new CollectionHelper ();
private CollectionHelper ()
{}
@Nullable
public static ECollectionBaseType getCollectionBaseTypeOfClass (@Nullable final Class > aClass)
{
if (aClass != null)
{
// Query Set before Collection, because Set is derived from Collection!
if (Set.class.isAssignableFrom (aClass))
return ECollectionBaseType.SET;
if (Collection.class.isAssignableFrom (aClass))
return ECollectionBaseType.COLLECTION;
if (Map.class.isAssignableFrom (aClass))
return ECollectionBaseType.MAP;
if (ClassHelper.isArrayClass (aClass))
return ECollectionBaseType.ARRAY;
if (Iterator.class.isAssignableFrom (aClass))
return ECollectionBaseType.ITERATOR;
if (Iterable.class.isAssignableFrom (aClass))
return ECollectionBaseType.ITERABLE;
if (Enumeration.class.isAssignableFrom (aClass))
return ECollectionBaseType.ENUMERATION;
}
return null;
}
@Nullable
public static ECollectionBaseType getCollectionBaseTypeOfObject (@Nullable final Object aObj)
{
return aObj == null ? null : getCollectionBaseTypeOfClass (aObj.getClass ());
}
public static boolean isCollectionClass (@Nullable final Class > aClass)
{
return getCollectionBaseTypeOfClass (aClass) != null;
}
public static boolean isCollectionObject (@Nullable final Object aObj)
{
return getCollectionBaseTypeOfObject (aObj) != null;
}
/**
* Get the passed object as a {@link CommonsArrayList} object. This is helpful
* in case you want to compare the String array ["a", "b"] with the
* List<String> ("a", "b") If the passed object is not a recognized.
* container type, than a new list with one element is created!
*
* @param aObj
* The object to be converted. May not be null
.
* @return The object as a collection. Never null
.
*/
@Nonnull
public static CommonsArrayList > getAsList (@Nonnull final Object aObj)
{
ValueEnforcer.notNull (aObj, "Object");
final ECollectionBaseType eType = getCollectionBaseTypeOfObject (aObj);
if (eType == null)
{
// It's not a supported container -> create a new list with one element
return newList (aObj);
}
switch (eType)
{
case COLLECTION:
// It's already a collection
if (aObj instanceof CommonsArrayList >)
return (CommonsArrayList >) aObj;
return newList ((Collection >) aObj);
case SET:
// Convert to list
return newList ((Set >) aObj);
case MAP:
// Use the entry set of the map as list
return newList (((Map , ?>) aObj).entrySet ());
case ARRAY:
// Convert the array to a list
return newList ((Object []) aObj);
case ITERATOR:
// Convert the iterator to a list
return newList ((Iterator >) aObj);
case ITERABLE:
// Convert the iterable to a list
return newList ((Iterable >) aObj);
case ENUMERATION:
// Convert the enumeration to a list
return newList ((Enumeration >) aObj);
default:
throw new IllegalStateException ("Unhandled collection type " + eType + "!");
}
}
@SafeVarargs
@Nullable
@ReturnsImmutableObject
@CodingStyleguideUnaware
public static List makeUnmodifiable (@Nullable final ELEMENTTYPE... aArray)
{
return aArray == null ? null : Collections.unmodifiableList (newList (aArray));
}
@Nullable
@ReturnsImmutableObject
@CodingStyleguideUnaware
public static Collection makeUnmodifiable (@Nullable final Collection aCollection)
{
return aCollection == null ? null : Collections.unmodifiableCollection (aCollection);
}
@Nullable
@ReturnsImmutableObject
@CodingStyleguideUnaware
public static List makeUnmodifiable (@Nullable final List aList)
{
return aList == null ? null : Collections.unmodifiableList (aList);
}
@Nullable
@ReturnsImmutableObject
@CodingStyleguideUnaware
public static Set makeUnmodifiable (@Nullable final Set aSet)
{
return aSet == null ? null : Collections.unmodifiableSet (aSet);
}
@Nullable
@ReturnsImmutableObject
@CodingStyleguideUnaware
public static Map makeUnmodifiable (@Nullable final Map aMap)
{
return aMap == null ? null : Collections.unmodifiableMap (aMap);
}
//
// @Nullable
// @ReturnsImmutableObject
// public static >
// SortedSet makeUnmodifiable (@Nullable final SortedSet
// aSortedSet)
// {
// return aSortedSet == null ? null : Collections.unmodifiableSortedSet
// (aSortedSet);
// }
//
// @Nullable
// @ReturnsImmutableObject
// public static SortedMap
// makeUnmodifiable (@Nullable final SortedMap
// aSortedMap)
// {
// return aSortedMap == null ? null : Collections.unmodifiableSortedMap
// (aSortedMap);
// }
@Nullable
@ReturnsImmutableObject
@CodingStyleguideUnaware
public static NavigableSet makeUnmodifiable (@Nullable final NavigableSet aNavigableSet)
{
return aNavigableSet == null ? null : Collections.unmodifiableNavigableSet (aNavigableSet);
}
@Nullable
@ReturnsImmutableObject
@CodingStyleguideUnaware
public static NavigableMap makeUnmodifiable (@Nullable final NavigableMap aNavigableMap)
{
return aNavigableMap == null ? null : Collections.unmodifiableNavigableMap (aNavigableMap);
}
@Nonnull
@ReturnsImmutableObject
@CodingStyleguideUnaware
public static Collection makeUnmodifiableNotNull (@Nullable final Collection aCollection)
{
return aCollection == null ? Collections.emptyList () : Collections.unmodifiableCollection (aCollection);
}
@Nonnull
@ReturnsImmutableObject
@CodingStyleguideUnaware
public static List makeUnmodifiableNotNull (@Nullable final List aList)
{
return aList == null ? Collections.emptyList () : Collections.unmodifiableList (aList);
}
@Nonnull
@ReturnsImmutableObject
@CodingStyleguideUnaware
public static Set makeUnmodifiableNotNull (@Nullable final Set aSet)
{
return aSet == null ? Collections.emptySet () : Collections.unmodifiableSet (aSet);
}
@Nonnull
@ReturnsImmutableObject
@CodingStyleguideUnaware
public static Map makeUnmodifiableNotNull (@Nullable final Map aMap)
{
return aMap == null ? Collections.emptyMap () : Collections.unmodifiableMap (aMap);
}
// @Nonnull
// @ReturnsImmutableObject
// public static >
// SortedSet makeUnmodifiableNotNull (@Nullable final SortedSet
// aSortedSet)
// {
// return aSortedSet == null ? Collections.emptySortedSet () :
// Collections.unmodifiableSortedSet (aSortedSet);
// }
//
// @Nonnull
// @ReturnsImmutableObject
// public static , VALUETYPE>
// SortedMap makeUnmodifiableNotNull (@Nullable final
// SortedMap aSortedMap)
// {
// return aSortedMap == null ? Collections.emptySortedMap () :
// Collections.unmodifiableSortedMap (aSortedMap);
// }
@Nonnull
@ReturnsImmutableObject
@CodingStyleguideUnaware
public static > NavigableSet makeUnmodifiableNotNull (@Nullable final NavigableSet aNavigableSet)
{
return aNavigableSet == null ? Collections.emptyNavigableSet () : Collections.unmodifiableNavigableSet (aNavigableSet);
}
@Nonnull
@ReturnsImmutableObject
@CodingStyleguideUnaware
public static , VALUETYPE> NavigableMap makeUnmodifiableNotNull (@Nullable final NavigableMap aNavigableMap)
{
return aNavigableMap == null ? Collections.emptyNavigableMap () : Collections.unmodifiableNavigableMap (aNavigableMap);
}
/**
* Get all elements that are only contained in the first contained, and not in
* the second. This method implements aCont1 - aCont2
.
*
* @param
* Set element type
* @param aCollection1
* The first container. May be null
or empty.
* @param aCollection2
* The second container. May be null
or empty.
* @return The difference and never null
. Returns an empty set,
* if the first container is empty. Returns a copy of the first
* container, if the second container is empty. Returns
* aCont1 - aCont2
if both containers are non-empty.
*/
@Nullable
@ReturnsMutableCopy
public static ICommonsSet getDifference (@Nullable final Collection extends ELEMENTTYPE> aCollection1,
@Nullable final Collection extends ELEMENTTYPE> aCollection2)
{
if (isEmpty (aCollection1))
return newSet (0);
if (isEmpty (aCollection2))
return newSet (aCollection1);
final ICommonsSet ret = newSet (aCollection1);
ret.removeAll (aCollection2);
return ret;
}
/**
* Get all elements that are contained in the first AND in the second
* container.
*
* @param
* Collection element type
* @param aCollection1
* The first container. May be null
or empty.
* @param aCollection2
* The second container. May be null
or empty.
* @return An empty set, if either the first or the second container are
* empty. Returns a set of elements that are contained in both
* containers, if both containers are non-empty. The return value is
* never null
.
*/
@Nullable
@ReturnsMutableCopy
public static ICommonsSet getIntersected (@Nullable final Collection extends ELEMENTTYPE> aCollection1,
@Nullable final Collection extends ELEMENTTYPE> aCollection2)
{
if (isEmpty (aCollection1))
return newSet (0);
if (isEmpty (aCollection2))
return newSet (0);
final ICommonsSet ret = newSet (aCollection1);
ret.retainAll (aCollection2);
return ret;
}
@Nonnull
@ReturnsMutableCopy
public static CommonsHashMap newMap (@Nonnegative final int nInitialCapacity)
{
return new CommonsHashMap <> (nInitialCapacity);
}
@Nonnull
@ReturnsMutableCopy
public static CommonsHashMap newMap ()
{
return new CommonsHashMap <> ();
}
@Nonnull
@ReturnsMutableCopy
public static CommonsHashMap newMapMapped (@Nullable final Map extends SRCKEYTYPE, ? extends SRCVALUETYPE> aMap,
@Nonnull final Function super SRCKEYTYPE, ? extends DSTKEYTYPE> aKeyMapper,
@Nonnull final Function super SRCVALUETYPE, ? extends DSTVALUETYPE> aValueMapper)
{
if (isEmpty (aMap))
return newMap (0);
return new CommonsHashMap <> (aMap, aKeyMapper, aValueMapper);
}
@Nonnull
@ReturnsMutableCopy
public static CommonsHashMap newMapMapped (@Nullable final Collection extends SRCTYPE> aCollection,
@Nonnull final Function super SRCTYPE, ? extends DSTKEYTYPE> aKeyMapper,
@Nonnull final Function super SRCTYPE, ? extends DSTVALUETYPE> aValueMapper)
{
if (isEmpty (aCollection))
return newMap (0);
return new CommonsHashMap <> (aCollection, aKeyMapper, aValueMapper);
}
@Nonnull
@ReturnsMutableCopy
public static CommonsHashMap newMap (@Nullable final Map aMap,
@Nonnull final Predicate super Map.Entry extends KEYTYPE, ? extends VALUETYPE>> aFilter)
{
if (isEmpty (aMap))
return newMap (0);
final CommonsHashMap ret = newMap (aMap.size ());
ret.putAll (aMap, aFilter);
return ret;
}
@Nonnull
@ReturnsMutableCopy
public static CommonsHashMap newMap (@Nullable final KEYTYPE aKey,
@Nullable final VALUETYPE aValue)
{
final CommonsHashMap ret = newMap (1);
ret.put (aKey, aValue);
return ret;
}
@Nonnull
@ReturnsMutableCopy
@SafeVarargs
public static CommonsHashMap newMap (@Nullable final ELEMENTTYPE... aValues)
{
if (ArrayHelper.isEmpty (aValues))
return newMap (0);
if ((aValues.length % 2) != 0)
throw new IllegalArgumentException ("The passed array needs an even number of elements!");
final CommonsHashMap ret = newMap (aValues.length / 2);
for (int i = 0; i < aValues.length; i += 2)
ret.put (aValues[i], aValues[i + 1]);
return ret;
}
@Nonnull
@ReturnsMutableCopy
public static CommonsHashMap newMap (@Nullable final KEYTYPE [] aKeys,
@Nullable final VALUETYPE [] aValues)
{
final int nKeys = ArrayHelper.getSize (aKeys);
final int nValues = ArrayHelper.getSize (aValues);
// Check for identical size
if (nKeys != nValues)
throw new IllegalArgumentException ("The passed arrays have different length (" + nKeys + " keys and " + nValues + " values)!");
// Are both empty?
if (nKeys == 0)
return newMap (0);
final CommonsHashMap ret = newMap (nKeys);
for (int i = 0; i < aKeys.length; ++i)
ret.put (aKeys[i], aValues[i]);
return ret;
}
@Nonnull
@ReturnsMutableCopy
public static CommonsHashMap newMap (@Nullable final Collection extends KEYTYPE> aKeys,
@Nullable final Collection extends VALUETYPE> aValues)
{
final int nKeys = getSize (aKeys);
final int nValues = getSize (aValues);
// Check for identical size
if (nKeys != nValues)
throw new IllegalArgumentException ("The passed arrays have different length (" + nKeys + " keys and " + nValues + " values)!");
// Are both empty?
if (nKeys == 0)
return newMap (0);
final CommonsHashMap ret = newMap (nKeys);
final Iterator extends KEYTYPE> itk = aKeys.iterator ();
final Iterator extends VALUETYPE> itv = aValues.iterator ();
while (itk.hasNext ())
ret.put (itk.next (), itv.next ());
return ret;
}
@Nonnull
@ReturnsMutableCopy
public static CommonsHashMap newMap (@Nullable final Map extends KEYTYPE, ? extends VALUETYPE> aMap)
{
if (isEmpty (aMap))
return newMap (0);
return new CommonsHashMap <> (aMap);
}
@Nonnull
@ReturnsMutableCopy
public static CommonsHashMap newMap (@Nullable final Map extends KEYTYPE, ? extends VALUETYPE> [] aMaps)
{
if (aMaps == null || aMaps.length == 0)
return newMap (0);
final CommonsHashMap ret = newMap ();
for (final Map extends KEYTYPE, ? extends VALUETYPE> aMap : aMaps)
ret.putAll (aMap);
return ret;
}
@Nonnull
@ReturnsMutableCopy
public static CommonsHashMap newMap (@Nullable final Collection extends Map.Entry > aCollection)
{
if (isEmpty (aCollection))
return newMap (0);
final CommonsHashMap ret = newMap (aCollection.size ());
ret.putAll (aCollection);
return ret;
}
@Nonnull
@ReturnsMutableCopy
public static CommonsHashMap newMap (@Nullable final Iterable extends Map.Entry > aCollection)
{
if (isEmpty (aCollection))
return newMap (0);
final CommonsHashMap ret = newMap ();
ret.putAll (aCollection);
return ret;
}
@Nonnull
@ReturnsMutableCopy
public static CommonsLinkedHashMap newOrderedMap (@Nonnegative final int nInitialCapacity)
{
return new CommonsLinkedHashMap <> (nInitialCapacity);
}
@Nonnull
@ReturnsMutableCopy
public static CommonsLinkedHashMap newOrderedMap ()
{
return new CommonsLinkedHashMap <> ();
}
@Nonnull
@ReturnsMutableCopy
public static CommonsLinkedHashMap newOrderedMapMapped (@Nullable final Map extends SRCKEYTYPE, ? extends SRCVALUETYPE> aMap,
@Nonnull final Function super SRCKEYTYPE, ? extends DSTKEYTYPE> aKeyMapper,
@Nonnull final Function super SRCVALUETYPE, ? extends DSTVALUETYPE> aValueMapper)
{
if (isEmpty (aMap))
return newOrderedMap (0);
return new CommonsLinkedHashMap <> (aMap, aKeyMapper, aValueMapper);
}
@Nonnull
@ReturnsMutableCopy
public static CommonsLinkedHashMap newOrderedMapMapped (@Nullable final Collection extends SRCTYPE> aCollection,
@Nonnull final Function super SRCTYPE, ? extends DSTKEYTYPE> aKeyMapper,
@Nonnull final Function super SRCTYPE, ? extends DSTVALUETYPE> aValueMapper)
{
if (isEmpty (aCollection))
return newOrderedMap (0);
return new CommonsLinkedHashMap <> (aCollection, aKeyMapper, aValueMapper);
}
@Nonnull
@ReturnsMutableCopy
public static CommonsLinkedHashMap newOrderedMap (@Nullable final Map aMap,
@Nonnull final Predicate super Map.Entry extends KEYTYPE, ? extends VALUETYPE>> aFilter)
{
if (isEmpty (aMap))
return newOrderedMap (0);
final CommonsLinkedHashMap ret = newOrderedMap (aMap.size ());
ret.putAll (aMap, aFilter);
return ret;
}
@Nonnull
@ReturnsMutableCopy
public static CommonsLinkedHashMap newOrderedMap (@Nullable final KEYTYPE aKey,
@Nullable final VALUETYPE aValue)
{
final CommonsLinkedHashMap ret = newOrderedMap (1);
ret.put (aKey, aValue);
return ret;
}
@Nonnull
@ReturnsMutableCopy
@SafeVarargs
public static CommonsLinkedHashMap newOrderedMap (@Nullable final ELEMENTTYPE... aValues)
{
if (ArrayHelper.isEmpty (aValues))
return newOrderedMap (0);
if ((aValues.length % 2) != 0)
throw new IllegalArgumentException ("The passed array needs an even number of elements!");
final CommonsLinkedHashMap ret = newOrderedMap (aValues.length / 2);
for (int i = 0; i < aValues.length; i += 2)
ret.put (aValues[i], aValues[i + 1]);
return ret;
}
/**
* Retrieve a map that is ordered in the way the parameter arrays are passed
* in. Note that key and value arrays need to have the same length.
*
* @param
* The key type.
* @param
* The value type.
* @param aKeys
* The key array to use. May not be null
.
* @param aValues
* The value array to use. May not be null
.
* @return A {@link CommonsLinkedHashMap} containing the passed key-value
* entries. Never null
.
*/
@Nonnull
@ReturnsMutableCopy
public static CommonsLinkedHashMap newOrderedMap (@Nullable final KEYTYPE [] aKeys,
@Nullable final VALUETYPE [] aValues)
{
final int nKeys = ArrayHelper.getSize (aKeys);
final int nValues = ArrayHelper.getSize (aValues);
// Check for identical size
if (nKeys != nValues)
throw new IllegalArgumentException ("The passed arrays have different length (" + nKeys + " keys and " + nValues + " values)!");
// Are both empty?
if (nKeys == 0)
return newOrderedMap (0);
final CommonsLinkedHashMap ret = newOrderedMap (nKeys);
for (int i = 0; i < aKeys.length; ++i)
ret.put (aKeys[i], aValues[i]);
return ret;
}
@Nonnull
@ReturnsMutableCopy
public static CommonsLinkedHashMap newOrderedMap (@Nullable final Collection extends KEYTYPE> aKeys,
@Nullable final Collection extends VALUETYPE> aValues)
{
final int nKeys = getSize (aKeys);
final int nValues = getSize (aValues);
// Check for identical size
if (nKeys != nValues)
throw new IllegalArgumentException ("The passed arrays have different length (" + nKeys + " keys and " + nValues + " values)!");
// Are both empty?
if (nKeys == 0)
return newOrderedMap (0);
final CommonsLinkedHashMap ret = newOrderedMap (nKeys);
final Iterator extends KEYTYPE> itk = aKeys.iterator ();
final Iterator extends VALUETYPE> itv = aValues.iterator ();
while (itk.hasNext ())
ret.put (itk.next (), itv.next ());
return ret;
}
@Nonnull
@ReturnsMutableCopy
public static CommonsLinkedHashMap newOrderedMap (@Nullable final Map extends KEYTYPE, ? extends VALUETYPE> aMap)
{
if (isEmpty (aMap))
return newOrderedMap (0);
return new CommonsLinkedHashMap <> (aMap);
}
@Nonnull
@ReturnsMutableCopy
public static CommonsLinkedHashMap newOrderedMap (@Nullable final Map extends KEYTYPE, ? extends VALUETYPE> [] aMaps)
{
if (ArrayHelper.isEmpty (aMaps))
return newOrderedMap (0);
final CommonsLinkedHashMap ret = newOrderedMap ();
for (final Map extends KEYTYPE, ? extends VALUETYPE> aMap : aMaps)
ret.putAll (aMap);
return ret;
}
@Nonnull
@ReturnsMutableCopy
public static CommonsLinkedHashMap newOrderedMap (@Nullable final Collection extends Map.Entry > aCollection)
{
if (isEmpty (aCollection))
return newOrderedMap (0);
final CommonsLinkedHashMap ret = newOrderedMap (aCollection.size ());
ret.putAll (aCollection);
return ret;
}
@Nonnull
@ReturnsMutableCopy
public static CommonsLinkedHashMap newOrderedMap (@Nullable final Iterable extends Map.Entry > aCollection)
{
if (isEmpty (aCollection))
return newOrderedMap (0);
final CommonsLinkedHashMap ret = newOrderedMap ();
ret.putAll (aCollection);
return ret;
}
@Nonnull
@ReturnsMutableCopy
public static , VALUETYPE> CommonsTreeMap newSortedMap ()
{
return new CommonsTreeMap <> (Comparator.nullsFirst (Comparator.naturalOrder ()));
}
@Nonnull
@ReturnsMutableCopy
public static , DSTVALUETYPE> CommonsTreeMap newSortedMapMapped (@Nullable final Map extends SRCKEYTYPE, ? extends SRCVALUETYPE> aMap,
@Nonnull final Function super SRCKEYTYPE, DSTKEYTYPE> aKeyMapper,
@Nonnull final Function super SRCVALUETYPE, DSTVALUETYPE> aValueMapper)
{
return new CommonsTreeMap <> (aMap, aKeyMapper, aValueMapper);
}
@Nonnull
@ReturnsMutableCopy
public static , VALUETYPE> CommonsTreeMap newSortedMap (@Nullable final Map aMap,
@Nonnull final Predicate super Map.Entry extends KEYTYPE, ? extends VALUETYPE>> aFilter)
{
final CommonsTreeMap ret = newSortedMap ();
ret.putAll (aMap, aFilter);
return ret;
}
@Nonnull
@ReturnsMutableCopy
public static , VALUETYPE> CommonsTreeMap newSortedMap (@Nullable final KEYTYPE aKey,
@Nullable final VALUETYPE aValue)
{
final CommonsTreeMap ret = newSortedMap ();
ret.put (aKey, aValue);
return ret;
}
@Nonnull
@ReturnsMutableCopy
@SafeVarargs
public static > CommonsTreeMap newSortedMap (@Nullable final ELEMENTTYPE... aValues)
{
if (ArrayHelper.isEmpty (aValues))
return newSortedMap ();
if ((aValues.length % 2) != 0)
throw new IllegalArgumentException ("The passed array needs an even number of elements!");
final CommonsTreeMap ret = newSortedMap ();
for (int i = 0; i < aValues.length; i += 2)
ret.put (aValues[i], aValues[i + 1]);
return ret;
}
@Nonnull
@ReturnsMutableCopy
public static , VALUETYPE> CommonsTreeMap newSortedMap (@Nullable final KEYTYPE [] aKeys,
@Nullable final VALUETYPE [] aValues)
{
// Are both empty?
if (ArrayHelper.isEmpty (aKeys) && ArrayHelper.isEmpty (aValues))
return newSortedMap ();
// keys OR values may be null here
if (ArrayHelper.getSize (aKeys) != ArrayHelper.getSize (aValues))
throw new IllegalArgumentException ("The passed arrays have different length!");
final CommonsTreeMap ret = newSortedMap ();
for (int i = 0; i < aKeys.length; ++i)
ret.put (aKeys[i], aValues[i]);
return ret;
}
@Nonnull
@ReturnsMutableCopy
public static , VALUETYPE> CommonsTreeMap newSortedMap (@Nullable final Collection extends KEYTYPE> aKeys,
@Nullable final Collection extends VALUETYPE> aValues)
{
// Are both empty?
if (isEmpty (aKeys) && isEmpty (aValues))
return newSortedMap ();
// keys OR values may be null here
if (getSize (aKeys) != getSize (aValues))
throw new IllegalArgumentException ("Number of keys is different from number of values");
final CommonsTreeMap ret = newSortedMap ();
final Iterator extends KEYTYPE> itk = aKeys.iterator ();
final Iterator extends VALUETYPE> itv = aValues.iterator ();
while (itk.hasNext ())
ret.put (itk.next (), itv.next ());
return ret;
}
@Nonnull
@ReturnsMutableCopy
public static , VALUETYPE> CommonsTreeMap newSortedMap (@Nullable final Map extends KEYTYPE, ? extends VALUETYPE> aMap)
{
if (isEmpty (aMap))
return newSortedMap ();
final CommonsTreeMap ret = newSortedMap ();
ret.putAll (aMap);
return ret;
}
@Nonnull
@ReturnsMutableCopy
public static , VALUETYPE> CommonsTreeMap newSortedMap (@Nullable final Map extends KEYTYPE, ? extends VALUETYPE> [] aMaps)
{
if (aMaps == null || aMaps.length == 0)
return newSortedMap ();
final CommonsTreeMap ret = newSortedMap ();
for (final Map extends KEYTYPE, ? extends VALUETYPE> aMap : aMaps)
ret.putAll (aMap);
return ret;
}
@Nonnull
@ReturnsMutableCopy
public static , VALUETYPE> CommonsTreeMap newSortedMap (@Nullable final Collection extends Map.Entry > aCollection)
{
if (isEmpty (aCollection))
return newSortedMap ();
final CommonsTreeMap ret = newSortedMap ();
ret.putAll (aCollection);
return ret;
}
@Nonnull
@ReturnsMutableCopy
public static , VALUETYPE> CommonsTreeMap newSortedMap (@Nullable final Iterable extends Map.Entry > aCollection)
{
if (isEmpty (aCollection))
return newSortedMap ();
final CommonsTreeMap ret = newSortedMap ();
ret.putAll (aCollection);
return ret;
}
@Nonnull
@ReturnsMutableCopy
public static CommonsHashSet newSet (@Nonnegative final int nInitialCapacity)
{
return new CommonsHashSet <> (nInitialCapacity);
}
@Nonnull
@ReturnsMutableCopy
public static CommonsHashSet newSet ()
{
return new CommonsHashSet <> ();
}
@Nonnull
@ReturnsMutableCopy
public static CommonsHashSet newSetMapped (@Nullable final Collection extends SRCTYPE> aCollection,
@Nonnull final Function super SRCTYPE, DSTTYPE> aMapper)
{
if (isEmpty (aCollection))
return newSet (0);
final CommonsHashSet ret = newSet (aCollection.size ());
ret.addAllMapped (aCollection, aMapper);
return ret;
}
@Nonnull
@ReturnsMutableCopy
public static CommonsHashSet newSetMapped (@Nullable final SRCTYPE [] aArray,
@Nonnull final Function super SRCTYPE, DSTTYPE> aMapper)
{
if (ArrayHelper.isEmpty (aArray))
return newSet (0);
final CommonsHashSet ret = newSet (aArray.length);
ret.addAllMapped (aArray, aMapper);
return ret;
}
@Nonnull
@ReturnsMutableCopy
public static CommonsHashSet newSet (@Nullable final Collection extends ELEMENTTYPE> aCollection,
@Nonnull final Predicate super ELEMENTTYPE> aFilter)
{
if (isEmpty (aCollection))
return newSet (0);
final CommonsHashSet ret = newSet (aCollection.size ());
ret.addAll (aCollection, aFilter);
return ret;
}
@Nonnull
@ReturnsMutableCopy
public static CommonsHashSet newSet (@Nullable final ELEMENTTYPE aValue)
{
final CommonsHashSet ret = newSet (1);
ret.add (aValue);
return ret;
}
@Nonnull
@ReturnsMutableCopy
@SafeVarargs
public static CommonsHashSet newSet (@Nullable final ELEMENTTYPE... aValues)
{
if (ArrayHelper.isEmpty (aValues))
return newSet (0);
final CommonsHashSet ret = newSet (aValues.length);
ret.addAll (aValues);
return ret;
}
@Nonnull
@ReturnsMutableCopy
public static CommonsHashSet newSet (@Nullable final Iterable extends ELEMENTTYPE> aCont)
{
final CommonsHashSet ret = newSet ();
ret.addAll (aCont);
return ret;
}
@Nonnull
@ReturnsMutableCopy
public static CommonsHashSet newSet (@Nullable final Collection extends ELEMENTTYPE> aCont)
{
if (isEmpty (aCont))
return newSet (0);
return new CommonsHashSet <> (aCont);
}
@Nonnull
@ReturnsMutableCopy
public static CommonsHashSet newSet (@Nullable final Iterator extends ELEMENTTYPE> aIter)
{
final CommonsHashSet ret = newSet ();
ret.addAll (aIter);
return ret;
}
@Nonnull
@ReturnsMutableCopy
public static CommonsHashSet newSet (@Nullable final IIterableIterator extends ELEMENTTYPE> aIter)
{
if (aIter == null)
return newSet (0);
return newSet (aIter.iterator ());
}
@Nonnull
@ReturnsMutableCopy
public static CommonsHashSet newSet (@Nullable final Enumeration extends ELEMENTTYPE> aEnum)
{
final CommonsHashSet ret = newSet ();
ret.addAll (aEnum);
return ret;
}
@Nonnull
@ReturnsMutableCopy
@SafeVarargs
public static > EnumSet newEnumSet (@Nonnull final Class aEnumClass,
@Nullable final ELEMENTTYPE... aValues)
{
final EnumSet ret = EnumSet.noneOf (aEnumClass);
if (aValues != null)
Collections.addAll (ret, aValues);
return ret;
}
@Nonnull
@ReturnsMutableCopy
public static > EnumSet newEnumSet (@Nonnull final Class aEnumClass,
@Nullable final Collection aValues)
{
if (isEmpty (aValues))
return EnumSet.noneOf (aEnumClass);
return EnumSet.copyOf (aValues);
}
@Nonnull
@ReturnsMutableCopy
public static > EnumSet newEnumSet (@Nonnull final Class aEnumClass,
@Nullable final EnumSet aValues)
{
if (aValues == null)
return EnumSet.noneOf (aEnumClass);
return EnumSet.copyOf (aValues);
}
@Nonnull
@ReturnsMutableCopy
public static > CommonsTreeSet newSortedSet ()
{
return new CommonsTreeSet <> (Comparator.nullsFirst (Comparator.naturalOrder ()));
}
@Nonnull
@ReturnsMutableCopy
public static > CommonsTreeSet newSortedSetMapped (@Nullable final Collection extends SRCTYPE> aCollection,
@Nonnull final Function super SRCTYPE, DSTTYPE> aMapper)
{
final CommonsTreeSet ret = newSortedSet ();
ret.addAllMapped (aCollection, aMapper);
return ret;
}
@Nonnull
@ReturnsMutableCopy
public static > CommonsTreeSet newSortedSetMapped (@Nullable final SRCTYPE [] aArray,
@Nonnull final Function super SRCTYPE, DSTTYPE> aMapper)
{
final CommonsTreeSet ret = newSortedSet ();
ret.addAllMapped (aArray, aMapper);
return ret;
}
@Nonnull
@ReturnsMutableCopy
public static > CommonsTreeSet newSortedSet (@Nullable final Collection extends ELEMENTTYPE> aCollection,
@Nonnull final Predicate super ELEMENTTYPE> aFilter)
{
final CommonsTreeSet ret = newSortedSet ();
ret.addAll (aCollection, aFilter);
return ret;
}
@Nonnull
@ReturnsMutableCopy
@SuppressFBWarnings (value = { "NP_PARAMETER_MUST_BE_NONNULL_BUT_MARKED_AS_NULLABLE" },
justification = "When using the constructor with the Comparator it works with null values!")
public static > CommonsTreeSet newSortedSet (@Nullable final ELEMENTTYPE aValue)
{
final CommonsTreeSet ret = newSortedSet ();
ret.add (aValue);
return ret;
}
@Nonnull
@ReturnsMutableCopy
@SafeVarargs
public static > CommonsTreeSet newSortedSet (@Nullable final ELEMENTTYPE... aValues)
{
final CommonsTreeSet ret = newSortedSet ();
ret.addAll (aValues);
return ret;
}
@Nonnull
@ReturnsMutableCopy
public static > CommonsTreeSet newSortedSet (@Nullable final Iterable extends ELEMENTTYPE> aCont)
{
final CommonsTreeSet ret = newSortedSet ();
ret.addAll (aCont);
return ret;
}
@Nonnull
@ReturnsMutableCopy
public static > CommonsTreeSet newSortedSet (@Nullable final Collection extends ELEMENTTYPE> aCont)
{
final CommonsTreeSet ret = newSortedSet ();
if (isNotEmpty (aCont))
ret.addAll (aCont);
return ret;
}
@Nonnull
@ReturnsMutableCopy
public static > CommonsTreeSet newSortedSet (@Nullable final Iterator extends ELEMENTTYPE> aIter)
{
final CommonsTreeSet ret = newSortedSet ();
ret.addAll (aIter);
return ret;
}
@Nonnull
@ReturnsMutableCopy
public static > CommonsTreeSet newSortedSet (@Nullable final IIterableIterator extends ELEMENTTYPE> aIter)
{
if (aIter == null)
return newSortedSet ();
return newSortedSet (aIter.iterator ());
}
@Nonnull
@ReturnsMutableCopy
public static > CommonsTreeSet newSortedSet (@Nullable final Enumeration extends ELEMENTTYPE> aEnum)
{
final CommonsTreeSet ret = newSortedSet ();
ret.addAll (aEnum);
return ret;
}
@Nonnull
@ReturnsMutableCopy
public static CommonsLinkedHashSet newOrderedSet (@Nonnegative final int nInitialCapacity)
{
return new CommonsLinkedHashSet <> (nInitialCapacity);
}
@Nonnull
@ReturnsMutableCopy
public static CommonsLinkedHashSet newOrderedSet ()
{
return new CommonsLinkedHashSet <> ();
}
@Nonnull
@ReturnsMutableCopy
public static CommonsLinkedHashSet newOrderedSetMapped (@Nullable final Collection extends SRCTYPE> aCollection,
@Nonnull final Function super SRCTYPE, DSTTYPE> aMapper)
{
if (isEmpty (aCollection))
return newOrderedSet (0);
final CommonsLinkedHashSet ret = newOrderedSet (aCollection.size ());
ret.addAllMapped (aCollection, aMapper);
return ret;
}
@Nonnull
@ReturnsMutableCopy
public static CommonsLinkedHashSet