Package com.github.tommyettinger.ds
Interface EnhancedCollection<T>
- Type Parameters:
T- the type of items in this collection
- All Superinterfaces:
Collection<T>,Iterable<T>
- All Known Implementing Classes:
BinaryHeap,CaseInsensitiveMap.CaseInsensitiveKeys,CaseInsensitiveOrderedMap.CaseInsensitiveKeys,CaseInsensitiveOrderedSet,CaseInsensitiveSet,EnumFloatMap.Entries,EnumFloatMap.Keys,EnumFloatOrderedMap.OrderedMapEntries,EnumFloatOrderedMap.OrderedMapKeys,EnumIntMap.Entries,EnumIntMap.Keys,EnumIntOrderedMap.OrderedMapEntries,EnumIntOrderedMap.OrderedMapKeys,EnumLongMap.Entries,EnumLongMap.Keys,EnumLongOrderedMap.OrderedMapEntries,EnumLongOrderedMap.OrderedMapKeys,EnumMap.Entries,EnumMap.Keys,EnumMap.Values,EnumOrderedMap.OrderedMapEntries,EnumOrderedMap.OrderedMapKeys,EnumOrderedMap.OrderedMapValues,EnumOrderedSet,EnumSet,FilteredIterableOrderedSet,FilteredIterableSet,FilteredStringOrderedSet,FilteredStringSet,HolderOrderedSet,HolderSet,IdentityOrderedSet,IdentitySet,IntFloatMap.Entries,IntFloatOrderedMap.OrderedMapEntries,IntIntMap.Entries,IntIntOrderedMap.OrderedMapEntries,IntLongMap.Entries,IntLongOrderedMap.OrderedMapEntries,IntObjectMap.Entries,IntObjectMap.Values,IntObjectOrderedMap.OrderedMapEntries,IntObjectOrderedMap.OrderedMapValues,LongFloatMap.Entries,LongFloatOrderedMap.OrderedMapEntries,LongIntMap.Entries,LongIntOrderedMap.OrderedMapEntries,LongLongMap.Entries,LongLongOrderedMap.OrderedMapEntries,LongObjectMap.Entries,LongObjectMap.Values,LongObjectOrderedMap.OrderedMapEntries,LongObjectOrderedMap.OrderedMapValues,NumberedSet,ObjectBag,ObjectDeque,ObjectFloatMap.Entries,ObjectFloatMap.Keys,ObjectFloatOrderedMap.OrderedMapEntries,ObjectFloatOrderedMap.OrderedMapKeys,ObjectIntMap.Entries,ObjectIntMap.Keys,ObjectIntOrderedMap.OrderedMapEntries,ObjectIntOrderedMap.OrderedMapKeys,ObjectList,ObjectLongMap.Entries,ObjectLongMap.Keys,ObjectLongOrderedMap.OrderedMapEntries,ObjectLongOrderedMap.OrderedMapKeys,ObjectObjectMap.Entries,ObjectObjectMap.Keys,ObjectObjectMap.Values,ObjectObjectOrderedMap.OrderedMapEntries,ObjectObjectOrderedMap.OrderedMapKeys,ObjectObjectOrderedMap.OrderedMapValues,ObjectOrderedSet,ObjectSet
Augments
Collection with default methods that can usually use the same implementation across subtypes.
This generally brings Object-based collections to parity with the default methods provided by the various
PrimitiveCollection types.-
Method Summary
Modifier and TypeMethodDescriptiondefault booleanAdds all parameters usingCollection.add(Object)for each one.default booleanAdds all parameters usingCollection.add(Object)for each one.default booleanAdds all parameters usingCollection.add(Object)for each one.default booleanGoes through the given Iterator until it is exhausted, adding every item to this EnhancedCollection usingCollection.add(Object).default booleandefault booleandefault booleanaddAllIterable(Iterable<? extends T> it) Gets theIterable.iterator()from the parameter and delegates toaddAll(Iterator).default voidaddLegible(String str, String delimiter, PartialParser<T> parser) Adds items to this EnhancedCollection drawn from the result oftoString(String)orappendTo(CharSequence, String, boolean).default voidaddLegible(String str, String delimiter, PartialParser<T> parser, int offset, int length) Adds items to this EnhancedCollection drawn from the result oftoString(String)orappendTo(CharSequence, String, boolean).default booleanaddVarargs(T... varargs) Takes an array of items to add, or more simply 0 or more arguments that will each be added.default <S extends CharSequence & Appendable>
SAppends to a StringBuilder from the contents of this EnhancedCollection, usingAppender::appendto append each item's String representation, separating items withseparator, and optionally wrapping the output in square brackets ifbracketsis true.default <S extends CharSequence & Appendable>
SAppends to a StringBuilder from the contents of this EnhancedCollection, but uses the givenAppenderto convert each item to a customizable representation and append them to a StringBuilder.default booleancontainsAll(Object[] array) Returnstrueif this collection contains all the elements in the specified array.default booleancontainsAll(Object[] array, int offset, int length) Returnstrueif this collection contains all the elements in the specified array starting fromoffsetand usinglengthitems from the array.default booleancontainsAll(Iterator<?> it) Returnstrueif this collection contains all the elements remaining in the specified Iterator.default booleancontainsAllIterable(Iterable<?> it) Returnstrueif this collection contains all the elements in the specified Iterable.default booleancontainsAny(Object[] array) default booleancontainsAny(Object[] array, int offset, int length) LikeCollection.containsAll(Collection), but returns true immediately if any item in the givenarrayis present in this EnhancedCollection.default booleancontainsAny(Iterator<?> it) LikeCollection.containsAll(Collection), but returns true immediately if any item in the given Iteratoritis present in this EnhancedCollection.default booleancontainsAnyIterable(Iterable<?> other) LikeCollection.containsAll(Collection), but returns true immediately if any item in the given Iterableotheris present in this EnhancedCollection.default Tfirst()Attempts to get the first item in this EnhancedCollection, where "first" is only defined meaningfully if this type is ordered.default booleanRemoves from this collection all occurrences of any elements contained in the specified array.default booleanRemoves from this collection all occurrences of any elements contained in the specified array, but only starts reading elements from the array starting at the givenoffsetand only useslengthitems.default booleanRemoves from this collection all occurrences of any elements contained in the specified Iterator.default booleanremoveAllIterable(Iterable<? extends T> it) Gets theIterable.iterator()from the parameter and delegates toremoveAll(Iterator).default booleanremoveEach(Object[] array) Removes from this collection element-wise occurrences of elements contained in the specified array.default booleanremoveEach(Object[] array, int offset, int length) Removes from this collection element-wise occurrences of elements contained in the specified array.default booleanremoveEach(Iterator<?> it) Removes from this collection element-wise occurrences of elements given by the specified Iterator.default booleanremoveEachIterable(Iterable<?> other) Removes from this collection element-wise occurrences of elements contained in the specified Iterable.default StringDelegates totoString(String, boolean)with the given itemSeparator and without surrounding brackets.default StringMakes a String from the contents of this EnhancedCollection, using theObject.toString()method of each item, separating items with the givenitemSeparator, and wrapping the result in square brackets ifbracketsis true.default StringMakes a String from the contents of this EnhancedCollection, but uses the givenAppenderto convert each item to a customizable representation and append them to a StringBuilder.
-
Method Details
-
add
Adds all parameters usingCollection.add(Object)for each one. Returns true if the set was modified.- Parameters:
item0- a T itemitem1- a T item- Returns:
- true if this modified the set
-
add
Adds all parameters usingCollection.add(Object)for each one. Returns true if the set was modified.- Parameters:
item0- a T itemitem1- a T itemitem2- a T item- Returns:
- true if this modified the set
-
add
Adds all parameters usingCollection.add(Object)for each one. Returns true if the set was modified.- Parameters:
item0- a T itemitem1- a T itemitem2- a T itemitem3- a T item- Returns:
- true if this modified the set
-
addAllIterable
Gets theIterable.iterator()from the parameter and delegates toaddAll(Iterator).- Parameters:
it- an Iterable of items to append to this EnhancedCollection- Returns:
- true if this collection was modified.
-
addAll
Goes through the given Iterator until it is exhausted, adding every item to this EnhancedCollection usingCollection.add(Object).- Parameters:
it- an Iterator of items to append to this EnhancedCollection- Returns:
- true if this collection was modified.
-
addAll
-
addAll
-
addVarargs
Takes an array of items to add, or more simply 0 or more arguments that will each be added. Ifvarargsis null, this won't add anything and will return false. If you have what is usually an array, consider callingaddAll(Object[])to avoid the possibility of heap pollution from a varargs with a generic type.- Parameters:
varargs- 0 or more items to add; may also be an array- Returns:
- true if this collection was modified
-
removeAllIterable
Gets theIterable.iterator()from the parameter and delegates toremoveAll(Iterator).- Parameters:
it- an Iterable of items to remove fully- Returns:
- true if this collection was modified.
-
removeAll
Removes from this collection all occurrences of any elements contained in the specified Iterator.- Parameters:
it- an Iterator of items to remove fully- Returns:
- true if this collection was modified.
-
removeAll
Removes from this collection all occurrences of any elements contained in the specified array.- Parameters:
array- a non-null array of items to remove fully- Returns:
- true if this collection was modified.
-
removeAll
Removes from this collection all occurrences of any elements contained in the specified array, but only starts reading elements from the array starting at the givenoffsetand only useslengthitems.- Parameters:
array- a non-null array of items to remove fullyoffset- the first index inarrayto uselength- how many items fromarrayshould be used- Returns:
- true if this collection was modified.
-
removeEachIterable
Removes from this collection element-wise occurrences of elements contained in the specified Iterable. Note that if a value is present more than once in this collection, only one of those occurrences will be removed for each occurrence of that value inother. Ifotherhas the same contents as this collection or has additional items, then removing each ofotherwill clear this.- Parameters:
other- an Iterable of any items to remove one-by-one, such as an ObjectList or ObjectSet- Returns:
- true if this collection was modified.
-
removeEach
Removes from this collection element-wise occurrences of elements given by the specified Iterator. Note that if a value is present more than once in this collection, only one of those occurrences will be removed for each timeotheryields that value. Ifotherhas the same contents as this collection or has additional items, then removing each ofotherwill clear this.- Parameters:
it- an Iterator of any items to remove one-by-one, such as an ObjectList or ObjectSet- Returns:
- true if this collection was modified.
-
removeEach
Removes from this collection element-wise occurrences of elements contained in the specified array. Note that if a value is present more than once in this collection, only one of those occurrences will be removed for each occurrence of that value inother. Ifotherhas the same contents as this collection or has additional items, then removing each ofotherwill clear this.- Parameters:
array- an array of any items to remove one-by-one, such as an ObjectList or ObjectSet- Returns:
- true if this collection was modified.
-
removeEach
Removes from this collection element-wise occurrences of elements contained in the specified array. Note that if a value is present more than once in this collection, only one of those occurrences will be removed for each occurrence of that value inother. Ifotherhas the same contents as this collection or has additional items, then removing each ofotherwill clear this.- Parameters:
array- an array of any items to remove one-by-one, such as an ObjectList or ObjectSetoffset- the first index inarrayto uselength- how many items fromarrayshould be used- Returns:
- true if this collection was modified.
-
containsAllIterable
Returnstrueif this collection contains all the elements in the specified Iterable.- Parameters:
it- a non-null Collection or other Iterable to have items checked for containment in this collection- Returns:
trueif this collection contains all the elements in the specified Iterable- Throws:
NullPointerException- if the specified Iterable is null.- See Also:
-
containsAll
Returnstrueif this collection contains all the elements remaining in the specified Iterator.- Parameters:
it- a non-null Iterator to have items checked for containment in this collection- Returns:
trueif this collection contains all the elements in the specified Iterator- Throws:
NullPointerException- if the specified Iterator is null.- See Also:
-
containsAll
Returnstrueif this collection contains all the elements in the specified array.- Parameters:
array- a non-null array to have items checked for containment in this collection- Returns:
trueif this collection contains all the elements in the specified collection- Throws:
NullPointerException- if the specified collection is null.- See Also:
-
containsAll
Returnstrueif this collection contains all the elements in the specified array starting fromoffsetand usinglengthitems from the array.- Parameters:
array- a non-null array to have items checked for containment in this collectionoffset- the first index inarrayto uselength- how many items fromarrayshould be used- Returns:
trueif this collection contains all the elements in the specified collection- Throws:
NullPointerException- if the specified collection is null.- See Also:
-
containsAnyIterable
LikeCollection.containsAll(Collection), but returns true immediately if any item in the given Iterableotheris present in this EnhancedCollection.- Parameters:
other- a Collection or other Iterable of any type to look through- Returns:
- true if any items from the Iterable are present in this EnhancedCollection
-
containsAny
LikeCollection.containsAll(Collection), but returns true immediately if any item in the given Iteratoritis present in this EnhancedCollection.- Parameters:
it- an Iterator of any type to look through- Returns:
- true if any items from the Iterator are present in this EnhancedCollection
-
containsAny
-
containsAny
LikeCollection.containsAll(Collection), but returns true immediately if any item in the givenarrayis present in this EnhancedCollection.- Parameters:
array- an array to look through; will not be modifiedoffset- the first index in array to checklength- how many items in array to check- Returns:
- true if any items from array are present in this EnhancedCollection
-
first
Attempts to get the first item in this EnhancedCollection, where "first" is only defined meaningfully if this type is ordered. Many times, this applies to a class that is not ordered, and in those cases it can get an arbitrary item, and that item is permitted to be different for different calls to first().
This is useful for cases where you would normally be able to call something likeList.get(int)to get an item, any item, from a collection, but whatever class you're using doesn't necessarily provide a get(), first(), peek(), or similar method.
The default implementation usesCollection.iterator(), tries to get the first item, or throws an IllegalStateException if this is empty.- Returns:
- the first item in this EnhancedCollection, as produced by
Collection.iterator() - Throws:
IllegalStateException- if this is empty
-
toString
Delegates totoString(String, boolean)with the given itemSeparator and without surrounding brackets.- Parameters:
itemSeparator- how to separate items, such as", "- Returns:
- a new String representing this map
-
toString
Makes a String from the contents of this EnhancedCollection, using theObject.toString()method of each item, separating items with the givenitemSeparator, and wrapping the result in square brackets ifbracketsis true.
Delegates toappendTo(CharSequence, String, boolean).- Parameters:
itemSeparator- how to separate items, such as", "brackets- true to wrap the result in square brackets, or false to leave the items unadorned- Returns:
- a new String representing this EnhancedCollection
-
toString
Makes a String from the contents of this EnhancedCollection, but uses the givenAppenderto convert each item to a customizable representation and append them to a StringBuilder. To use the default String representation, you can useAppender::appendas an appender.
Be advised thatAppender::appendwill allocate a method reference, each time this is called, on minimized Android builds due to R8 behavior. You can cache an Appender of the appropriate T type easily, however, as with this for when T is String:public static final Appender<String> STRING_APPENDER = Appender::append;
Delegates toappendTo(CharSequence, String, boolean, Appender).- Parameters:
separator- how to separate items, such as", "brackets- true to wrap the output in square brackets, or false to omit themappender- a function that takes a StringBuilder and a T, and returns the modified StringBuilder- Returns:
- a new String representing this EnhancedCollection
-
appendTo
Appends to a StringBuilder from the contents of this EnhancedCollection, usingAppender::appendto append each item's String representation, separating items withseparator, and optionally wrapping the output in square brackets ifbracketsis true.
Be advised thatAppender::appendwill allocate a method reference, each time this is called, on minimized Android builds due to R8 behavior. You can cache an Appender of the appropriate T type easily, however, as with this for when T is String:public static final Appender<String> STRING_APPENDER = Appender::append;
Delegates toappendTo(CharSequence, String, boolean, Appender).- Parameters:
sb- a StringBuilder that this can append toseparator- how to separate items, such as", "brackets- true to wrap the output in square brackets, or false to omit them- Returns:
sb, with the appended items of this EnhancedCollection
-
appendTo
default <S extends CharSequence & Appendable> S appendTo(S sb, String separator, boolean brackets, Appender<T> appender) Appends to a StringBuilder from the contents of this EnhancedCollection, but uses the givenAppenderto convert each item to a customizable representation and append them to a StringBuilder. To use the default String representation, you can useAppender::append, but be advised that it will allocate a method reference, each time this is called, on minimized Android builds due to R8 behavior. You can cache an Appender of the appropriate T type easily, however, as with this for when T is String:public static final Appender<String> STRING_APPENDER = Appender::append;- Parameters:
sb- a StringBuilder that this can append toseparator- how to separate items, such as", "brackets- true to wrap the output in square brackets, or false to omit themappender- a function that takes a StringBuilder and a T, and returns the modified StringBuilder- Returns:
sb, with the appended items of this EnhancedCollection
-
addLegible
Adds items to this EnhancedCollection drawn from the result oftoString(String)orappendTo(CharSequence, String, boolean). A PartialParser will be used to parse items from sections ofstr. Any brackets inside the given range of characters will ruin the parsing, so increase offset by 1 and reduce length by 2 if the original String had brackets added to it.- Parameters:
str- a String containing string representations of itemsdelimiter- the String separating every item in strparser- a PartialParser that returns aTitem from a section ofstr
-
addLegible
default void addLegible(String str, String delimiter, PartialParser<T> parser, int offset, int length) Adds items to this EnhancedCollection drawn from the result oftoString(String)orappendTo(CharSequence, String, boolean). A PartialParser will be used to parse items from sections ofstr. Any brackets inside the given range of characters will ruin the parsing, so increase offset by 1 and reduce length by 2 if the original String had brackets added to it.- Parameters:
str- a String containing string representations of itemsdelimiter- the String separating every item in strparser- a PartialParser that returns aTitem from a section ofstroffset- the first position to read items from instrlength- how many chars to read; -1 is treated as maximum length
-