Class CollectionsExt<E>
- java.lang.Object
-
- eu.simuline.util.CollectionsExt<E>
-
- Type Parameters:
E- the class of the elements of collections under consideration.
public abstract class CollectionsExt<E> extends java.lang.ObjectAn add on of the core classCollections. This class provides various kinds of immutable collections, where immutability is configurable via aCollectionsExt.Modificationobject.Also this class yields weak hash sets via
weakHashSet(). Moreover, there are methods to convert arrays and lists in one another also recursively. Finally, there are methodsgetUnique(Collection)to retrieve the unique element andreverse(List)reverses a list.- Version:
- 1.0
- Author:
- Ernst Reissner
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classCollectionsExt.AbstractImmutableCollection<C extends java.util.Collection<E>,E>A class ofCollections of elements of classEextendingCinitially throwing anUnsupportedOperationExceptionwhen trying to modify the collection either directly or via its iterator(s) or via a transparent view likeList.subList(int, int).(package private) static classCollectionsExt.AbstractImmutableMultiSet<C extends MultiSet<E>,E>A MultiSet which prevents being modified by throwing an exception for the modifying methods.static classCollectionsExt.ImmutableCollection<E>A collection which prevents being modified by throwing an exception for the modifying methods.static classCollectionsExt.ImmutableCyclicList<E>Represents an immutable cyclic list by throwing an exception when invoking a method which modifies this list.static classCollectionsExt.ImmutableList<E>A list which prevents being modified by throwing an exception for the modifying methods.private static classCollectionsExt.ImmutableMultiplicityAn immutable implementation of the multiplicity interface.(package private) static classCollectionsExt.ImmutableMultiSet<E>A multi-set which prevents being modified by throwing an exception for the modifying methods.static classCollectionsExt.ImmutableSet<E>A set which prevents being modified by throwing an exception for the modifying methods.(package private) static classCollectionsExt.ImmutableSortedMultiSet<E>A sorted multi-set which prevents being modified by throwing an exception for the modifying methods.static classCollectionsExt.ImmutableSortedSet<E>A sorted set which prevents being modified by throwing an exception for the modifying methods.static classCollectionsExt.ModificationEnumerates the kinds of modifications on aCollection.static classCollectionsExt.NonModifyingCyclicIterator<E>An interator which prevents modification on the underlying list, by throwing an exception for the modifying methods.
-
Constructor Summary
Constructors Constructor Description CollectionsExt()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <E> CollectionsExt.ImmutableCollection<E>getImmutableCollection(java.util.Collection<E> coll)Returns an unmodifiable view of the specified collectioncoll.static <E> CyclicList<E>getImmutableCyclicList(CyclicList<E> cyc)static <E> CollectionsExt.ImmutableList<E>getImmutableList(java.util.List<E> list)static <E> MultiSet<E>getImmutableMultiSet(MultiSet<E> mSet)static <E> CollectionsExt.ImmutableSet<E>getImmutableSet(java.util.Set<E> set)static <E> MultiSet<E>getImmutableSortedMultiSet(SortedMultiSet<E> mSet)static <E> CollectionsExt.ImmutableSortedSet<E>getImmutableSortedSet(java.util.SortedSet<E> set)static <T> TgetUnique(java.util.Collection<? extends T> coll)Returns the unique element of the collectioncoll.static voidmain(java.lang.String[] args)static java.lang.ObjectrecToArray(java.lang.Object source, java.lang.Class<?> cls)Convertssourcewhich is typically aList, recursively into an array with the given type.static java.lang.ObjectrecToArray(java.lang.Object source, java.lang.Class<?> cls, Caster caster)Convertssourcewhich is typically aCollection, recursively into an array with the given type using the specified caster for the elementary objects insource.static java.lang.Object[]recToArray(java.util.List<?> list)Converts the list given recursively into an array.static <T> java.util.List<T>reverse(java.util.List<T> list)Returns the reverse of the given list.static <E> java.util.Set<E>weakHashSet()Retuns a weak hash set, i.e. a hash set of weak references.
-
-
-
Method Detail
-
getImmutableCollection
public static <E> CollectionsExt.ImmutableCollection<E> getImmutableCollection(java.util.Collection<E> coll)
Returns an unmodifiable view of the specified collectioncoll. This method allows modules to provide users with "read-only" access to internal collections. Query operations on the returned collection "read through" to the specified collection, and attempts to modify the returned collection, whether direct or via its iterator, result in anUnsupportedOperationException.- Parameters:
coll- an instance of aCollection.- Returns:
- a
Collectionwhich equalscoll, i.e. with the same elements returned by the iterator in the same ordering but is immutable. Note that the return type offers methods#allowModification(CollectionsExt.Modification),CollectionsExt.AbstractImmutableCollection.allowedModifications()andCollectionsExt.ImmutableCollection.unrestricted(). - Throws:
java.lang.NullPointerException- ifcoll==null.
-
getImmutableSet
public static <E> CollectionsExt.ImmutableSet<E> getImmutableSet(java.util.Set<E> set)
-
getImmutableSortedSet
public static <E> CollectionsExt.ImmutableSortedSet<E> getImmutableSortedSet(java.util.SortedSet<E> set)
-
getImmutableList
public static <E> CollectionsExt.ImmutableList<E> getImmutableList(java.util.List<E> list)
-
getImmutableCyclicList
public static <E> CyclicList<E> getImmutableCyclicList(CyclicList<E> cyc)
-
getImmutableSortedMultiSet
public static <E> MultiSet<E> getImmutableSortedMultiSet(SortedMultiSet<E> mSet)
-
weakHashSet
public static <E> java.util.Set<E> weakHashSet()
Retuns a weak hash set, i.e. a hash set of weak references.- See Also:
HashMap,Collections.newSetFromMap(Map)
-
recToArray
public static java.lang.Object[] recToArray(java.util.List<?> list)
Converts the list given recursively into an array.- Parameters:
list- aList.- Returns:
- an array
arrayof objects satisfyingarray[i] == list.get(i)iflist.get(i)is not a list;array[i] == recToArray(list.get(i))otherwise. Note thatarraymay be a nested array but that the dimension is always one. - See Also:
recToArray(Object, Class),ArraysExt.recAsList(Object[])
-
recToArray
public static java.lang.Object recToArray(java.lang.Object source, java.lang.Class<?> cls)Convertssourcewhich is typically aList, recursively into an array with the given type.- Parameters:
source- an arbitraryObject, even an array but typically aList.cls- Up to compatibility (seeBasicTypesCompatibilityChecker.areCompatible(java.lang.Class<?>, java.lang.Object)), the type of the return value. Note that typically this is an array type such asDouble[][][]orList[][][]or evenint[][][]but this need not be the case.- Returns:
-
if
clsandsourceare compatible (seeBasicTypesCompatibilityChecker.areCompatible(java.lang.Class<?>, java.lang.Object)), e.g. ifsourceis an instance ofclsor ifsource == null, the objectsourceis returned.If
clsis not an elementary type as e.g.Boolean.TYPE, and ifsource != null, compatibility means thatsourceis an instance of the corresponding wrapper class. -
if
clsandsourceare not compatible,clsmust be an array type andsourcemust be a list; otherwise an exception is thrown.In the former case, an array
arrayof objects is returned satisfyingarray.length == ((List)source).size()andarray[i] == recToArray(list.get(i), cls2)for all valid indicesiis returned. Thecls2argument for the recursive invocation is the element type ofcls.Note that although the return value is always an array, its type need not be a subtype of
Object[]or ofObject[][]. Consider for instance the case wheresourceis a list ofIntegers andclsisint[]: This yields an
-
if
- Throws:
java.lang.IllegalArgumentException- if neither of the following is true:-
clsandsourceare compatible. -
clsis an array type andsourceis aList.
-
- See Also:
ArraysExt.recAsList(Object, Class)
-
recToArray
public static java.lang.Object recToArray(java.lang.Object source, java.lang.Class<?> cls, Caster caster)Convertssourcewhich is typically aCollection, recursively into an array with the given type using the specified caster for the elementary objects insource.- Parameters:
source- an arbitraryObject, even an array but typically aCollection.cls- Up to compatibility defined bycaster, the type of the return value. Note that typically this is an array type such asDouble[][][]orList[][][]or evenint[][][]but this need not be the case. Note that the base type has to be compatible with the source objects with respect to the specifiedcaster.caster- performs the conversion of the top-level elements of thesource.- Returns:
-
if
clsandsourceare compatible (seeCaster.areCompatible(java.lang.Class<?>, java.lang.Object)), thecast ofsourceis returned.Note that compatibility is up to wrapping of elementary types and unwrapping of their wrappers.
-
if
clsandsourceare not compatible,clsmust be an array type andsourcemust be a list; otherwise an exception is thrown.In the former case, an array
arrayof objects is returned satisfyingarray.length == ((Collection)source).size()andarray[i] == recToArray(list.get(i), ... , caster)for all valid indicesi. Thecls2argument for the recursive invocation is the element type ofcls.Note that although the return value is either the result of a casting process or an array, in the latter case its type need not be a subtype of
Object[]or ofObject[][]. Consider for instance the case wheresourceis a list ofIntegers andclsisint[]:
-
if
- Throws:
java.lang.IllegalArgumentException- if neither of the following is true:-
clsandsourceare compatible with respect tocaster. -
clsis an array type andsourceis aList.
-
- See Also:
ArraysExt.recAsList(Object, Class, Caster)
-
getUnique
public static <T> T getUnique(java.util.Collection<? extends T> coll)
Returns the unique element of the collectioncoll.- Parameters:
coll- a collection ofT's.- Returns:
- the unique element of the collection
coll. - Throws:
java.lang.IllegalStateException- ifcolldoes not contain a unique element.
-
reverse
public static <T> java.util.List<T> reverse(java.util.List<T> list)
Returns the reverse of the given list. In fact the list returned is anArrayList.
-
main
public static void main(java.lang.String[] args)
-
-