Class CollectionAdapter<T>
- java.lang.Object
-
- org.eclipse.collections.impl.collection.mutable.AbstractCollectionAdapter<T>
-
- org.eclipse.collections.impl.collection.mutable.CollectionAdapter<T>
-
- All Implemented Interfaces:
java.io.Serializable
,java.lang.Iterable<T>
,java.util.Collection<T>
,MutableCollection<T>
,InternalIterable<T>
,RichIterable<T>
public final class CollectionAdapter<T> extends AbstractCollectionAdapter<T> implements java.io.Serializable
This class provides a MutableCollection interface wrapper around a JDK Collections Collection interface instance. All the MutableCollection interface methods are supported in addition to the JDK Collection interface methods.To create a new instance that wraps a collection with the MutableSet interface, use the
wrapSet(Iterable)
factory method. To create a new instance that wraps a collection with the MutableList interface, use thewrapList(Iterable)
factory method. To wrap a collection with the MutableCollection interface alone, use theadapt(Collection)
factory method.- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description private java.util.Collection<T>
delegate
private static long
serialVersionUID
-
Constructor Summary
Constructors Constructor Description CollectionAdapter(java.util.Collection<T> newDelegate)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static <E> MutableCollection<E>
adapt(java.util.Collection<E> collection)
MutableCollection<T>
asSynchronized()
Returns a synchronized wrapper backed by this collection.MutableCollection<T>
asUnmodifiable()
Returns an unmodifiable view of this collection.boolean
equals(java.lang.Object o)
protected java.util.Collection<T>
getDelegate()
int
hashCode()
MutableCollection<T>
newEmpty()
Deprecated.useFastList.newList()
orUnifiedSet.newSet()
insteadImmutableCollection<T>
toImmutable()
Converts thisMutableCollection
to anImmutableCollection
.CollectionAdapter<T>
with(T element)
This method allows mutable and fixed size collections the ability to add elements to their existing elements.CollectionAdapter<T>
with(T... elements)
CollectionAdapter<T>
withAll(java.lang.Iterable<? extends T> elements)
This method allows mutable and fixed size collections the ability to add multiple elements to their existing elements.CollectionAdapter<T>
without(T element)
This method allows mutable and fixed size collections the ability to remove elements from their existing elements.CollectionAdapter<T>
withoutAll(java.lang.Iterable<? extends T> elements)
This method allows mutable and fixed size collections the ability to remove multiple elements from their existing elements.static <E> MutableList<E>
wrapList(java.lang.Iterable<E> iterable)
static <E> MutableSet<E>
wrapSet(java.lang.Iterable<E> iterable)
-
Methods inherited from class org.eclipse.collections.impl.collection.mutable.AbstractCollectionAdapter
add, addAll, addAllIterable, allSatisfy, allSatisfyWith, anySatisfy, anySatisfyWith, appendString, asLazy, chunk, clear, collect, collect, collectBoolean, collectByte, collectChar, collectDouble, collectFloat, collectIf, collectIf, collectInt, collectLong, collectShort, collectWith, collectWith, contains, containsAll, containsAllArguments, containsAllIterable, count, countWith, detect, detectIfNone, detectOptional, detectWith, detectWithIfNone, detectWithOptional, each, flatCollect, flatCollect, forEachWith, forEachWithIndex, getFirst, getLast, getOnly, groupBy, groupBy, groupByEach, groupByEach, groupByUniqueKey, groupByUniqueKey, injectInto, injectInto, injectInto, injectInto, injectInto, injectIntoWith, into, isEmpty, iterator, max, max, maxBy, min, min, minBy, noneSatisfy, noneSatisfyWith, notEmpty, partition, partitionWith, reject, reject, rejectWith, rejectWith, remove, removeAll, removeAllIterable, removeIf, removeIfWith, retainAll, retainAllIterable, select, select, selectAndRejectWith, selectInstancesOf, selectWith, selectWith, size, sumByDouble, sumByFloat, sumByInt, sumByLong, sumOfDouble, sumOfFloat, sumOfInt, sumOfLong, tap, toArray, toArray, toBag, toBiMap, toList, toMap, toMap, toSet, toSortedBag, toSortedBag, toSortedList, toSortedMap, toSortedMap, toSortedSet, toSortedSet, toString, wrap, zip, zip, zipWithIndex, zipWithIndex
-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface java.util.Collection
parallelStream, removeIf, spliterator, stream, toArray
-
Methods inherited from interface org.eclipse.collections.api.InternalIterable
forEach
-
Methods inherited from interface org.eclipse.collections.api.collection.MutableCollection
aggregateBy, aggregateInPlaceBy, collectBoolean, collectByte, collectChar, collectDouble, collectFloat, collectInt, collectLong, collectShort, countBy, countByEach, countByWith, flatCollectWith
-
Methods inherited from interface org.eclipse.collections.api.RichIterable
aggregateBy, appendString, appendString, containsAny, containsAnyIterable, containsBy, containsNone, containsNoneIterable, countBy, countByEach, countByWith, flatCollectBoolean, flatCollectByte, flatCollectChar, flatCollectDouble, flatCollectFloat, flatCollectInt, flatCollectLong, flatCollectShort, flatCollectWith, forEach, getAny, groupByAndCollect, injectIntoDouble, injectIntoFloat, injectIntoInt, injectIntoLong, makeString, makeString, makeString, makeString, maxByOptional, maxOptional, maxOptional, minByOptional, minOptional, minOptional, reduce, reduceInPlace, reduceInPlace, summarizeDouble, summarizeFloat, summarizeInt, summarizeLong, toImmutableBag, toImmutableBiMap, toImmutableList, toImmutableMap, toImmutableSet, toImmutableSortedBag, toImmutableSortedBag, toImmutableSortedBagBy, toImmutableSortedList, toImmutableSortedList, toImmutableSortedListBy, toImmutableSortedSet, toImmutableSortedSet, toImmutableSortedSetBy, toSortedBagBy, toSortedList, toSortedListBy, toSortedMapBy, toSortedSetBy
-
-
-
-
Field Detail
-
serialVersionUID
private static final long serialVersionUID
- See Also:
- Constant Field Values
-
delegate
private final java.util.Collection<T> delegate
-
-
Constructor Detail
-
CollectionAdapter
public CollectionAdapter(java.util.Collection<T> newDelegate)
-
-
Method Detail
-
getDelegate
protected java.util.Collection<T> getDelegate()
- Specified by:
getDelegate
in classAbstractCollectionAdapter<T>
-
asUnmodifiable
public MutableCollection<T> asUnmodifiable()
Description copied from interface:MutableCollection
Returns an unmodifiable view of this collection. This is the equivalent of usingCollections.unmodifiableCollection(this)
with a return type that supports the full iteration protocols available onMutableCollection
. Methods which would mutate the underlying collection will throw UnsupportedOperationExceptions.- Specified by:
asUnmodifiable
in interfaceMutableCollection<T>
- Returns:
- an unmodifiable view of this collection.
- See Also:
Collections.unmodifiableCollection(Collection)
-
asSynchronized
public MutableCollection<T> asSynchronized()
Description copied from interface:MutableCollection
Returns a synchronized wrapper backed by this collection. This is the equivalent of usingCollections.synchronizedCollection(this)
only with a return type that supports the full iteration protocols available onMutableCollection
. The preferred way of iterating over a synchronized collection is to use the internal iteration methods which are properly synchronized internally.MutableCollection synchedCollection = collection.asSynchronized(); ... synchedCollection.forEach(each -> ... ); synchedCollection.select(each -> ... ); synchedCollection.collect(each -> ... );
If you want to iterate using an imperative style, you must protect external iterators using a synchronized block. This includes explicit iterators as well as JDK 5 style for loops.- Specified by:
asSynchronized
in interfaceMutableCollection<T>
- Returns:
- a synchronized view of this collection.
- See Also:
Collections.synchronizedCollection(Collection)
-
toImmutable
public ImmutableCollection<T> toImmutable()
Description copied from interface:MutableCollection
Converts thisMutableCollection
to anImmutableCollection
.- Specified by:
toImmutable
in interfaceMutableCollection<T>
-
wrapSet
public static <E> MutableSet<E> wrapSet(java.lang.Iterable<E> iterable)
-
wrapList
public static <E> MutableList<E> wrapList(java.lang.Iterable<E> iterable)
-
adapt
public static <E> MutableCollection<E> adapt(java.util.Collection<E> collection)
-
equals
public boolean equals(java.lang.Object o)
- Specified by:
equals
in interfacejava.util.Collection<T>
- Overrides:
equals
in classjava.lang.Object
-
hashCode
public int hashCode()
- Specified by:
hashCode
in interfacejava.util.Collection<T>
- Overrides:
hashCode
in classjava.lang.Object
-
with
public CollectionAdapter<T> with(T... elements)
-
with
public CollectionAdapter<T> with(T element)
Description copied from interface:MutableCollection
This method allows mutable and fixed size collections the ability to add elements to their existing elements. In order to support fixed size a new instance of a collection would have to be returned taking the elements of the original collection and appending the new element to form the new collection. In the case of mutable collections, the original collection is modified, and is returned. In order to use this method properly with mutable and fixed size collections the following approach must be taken:MutableCollection<String> list = list.with("1"); list = list.with("2"); return list;
In the case ofFixedSizeCollection
a new instance of MutableCollection will be returned by with, and any variables that previously referenced the original collection will need to be redirected to reference the new instance. For other MutableCollection types you will replace the reference to collection with the same collection, since the instance will return "this" after calling add on itself.- Specified by:
with
in interfaceMutableCollection<T>
- See Also:
Collection.add(Object)
-
without
public CollectionAdapter<T> without(T element)
Description copied from interface:MutableCollection
This method allows mutable and fixed size collections the ability to remove elements from their existing elements. In order to support fixed size a new instance of a collection would have to be returned containing the elements that would be left from the original collection after calling remove. In the case of mutable collections, the original collection is modified, and is returned. In order to use this method properly with mutable and fixed size collections the following approach must be taken:MutableCollection<String> list = list.without("1"); list = list.without("2"); return list;
In the case ofFixedSizeCollection
a new instance of MutableCollection will be returned by without, and any variables that previously referenced the original collection will need to be redirected to reference the new instance. For other MutableCollection types you will replace the reference to collection with the same collection, since the instance will return "this" after calling remove on itself.- Specified by:
without
in interfaceMutableCollection<T>
- See Also:
Collection.remove(Object)
-
withAll
public CollectionAdapter<T> withAll(java.lang.Iterable<? extends T> elements)
Description copied from interface:MutableCollection
This method allows mutable and fixed size collections the ability to add multiple elements to their existing elements. In order to support fixed size a new instance of a collection would have to be returned taking the elements of the original collection and appending the new elements to form the new collection. In the case of mutable collections, the original collection is modified, and is returned. In order to use this method properly with mutable and fixed size collections the following approach must be taken:MutableCollection<String> list = list.withAll(FastList.newListWith("1", "2"));
In the case ofFixedSizeCollection
a new instance of MutableCollection will be returned by withAll, and any variables that previously referenced the original collection will need to be redirected to reference the new instance. For other MutableCollection types you will replace the reference to collection with the same collection, since the instance will return "this" after calling addAll on itself.- Specified by:
withAll
in interfaceMutableCollection<T>
- See Also:
Collection.addAll(Collection)
-
withoutAll
public CollectionAdapter<T> withoutAll(java.lang.Iterable<? extends T> elements)
Description copied from interface:MutableCollection
This method allows mutable and fixed size collections the ability to remove multiple elements from their existing elements. In order to support fixed size a new instance of a collection would have to be returned containing the elements that would be left from the original collection after calling removeAll. In the case of mutable collections, the original collection is modified, and is returned. In order to use this method properly with mutable and fixed size collections the following approach must be taken:MutableCollection<String> list = list.withoutAll(FastList.newListWith("1", "2"));
In the case ofFixedSizeCollection
a new instance of MutableCollection will be returned by withoutAll, and any variables that previously referenced the original collection will need to be redirected to reference the new instance. For other MutableCollection types you will replace the reference to collection with the same collection, since the instance will return "this" after calling removeAll on itself.- Specified by:
withoutAll
in interfaceMutableCollection<T>
- See Also:
Collection.removeAll(Collection)
-
newEmpty
@Deprecated public MutableCollection<T> newEmpty()
Deprecated.useFastList.newList()
orUnifiedSet.newSet()
insteadDescription copied from interface:MutableCollection
Creates a new empty mutable version of the same collection type. For example, if this instance is a FastList, this method will return a new empty FastList. If the class of this instance is immutable or fixed size (i.e. SingletonList) then a mutable alternative to the class will be provided.- Specified by:
newEmpty
in interfaceMutableCollection<T>
-
-