Package com.github.tommyettinger.ds
Class NumberedSet<T>
java.lang.Object
com.github.tommyettinger.ds.NumberedSet<T>
- Type Parameters:
T- the type of items; should implementObject.equals(Object)andObject.hashCode()
- All Implemented Interfaces:
Arrangeable,EnhancedCollection<T>,Ordered<T>,Iterable<T>,Collection<T>,Set<T>
An Ordered Set of
T items where the indexOf(Object) operation runs in constant time, but
any removal from the middle of the order runs in linear time. If you primarily append to a Set with
add(Object), this will perform like ObjectIntOrderedMap, since it's backed internally by
one of those Maps; indexOf() delegates to the map's ObjectIntMap.get(Object) method. This
has to do some bookkeeping to make sure the index for each item is stored as the value for the key matching
the item in the map. That bookkeeping will fail if you use the Iterator.remove() method on this
class' iterator; you can correct the indices with renumber(), or renumber(int) if you know
the first incorrect index.-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected classstatic classNested classes/interfaces inherited from interface com.github.tommyettinger.ds.Arrangeable
Arrangeable.ArrangeableList<T>Nested classes/interfaces inherited from interface com.github.tommyettinger.ds.Ordered
Ordered.OfBoolean, Ordered.OfByte, Ordered.OfChar, Ordered.OfDouble, Ordered.OfFloat, Ordered.OfInt, Ordered.OfLong, Ordered.OfShort -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected NumberedSet.NumberedSetIterator<T>protected NumberedSet.NumberedSetIterator<T>protected NumberedSet<T>.InternalMap -
Constructor Summary
ConstructorsConstructorDescriptionNumberedSet(int initialCapacity) NumberedSet(int initialCapacity, float loadFactor) NumberedSet(NumberedSet<? extends T> other) NumberedSet(Ordered<? extends T> ordered) Can be used to make a NumberedSet from anyOrderedmap or set with Object keys or items, using the keys for a map and the items for a set.NumberedSet(Ordered<T> other, int offset, int count) Creates a new set by copyingcountitems from the given Ordered, starting atoffsetin that Ordered, into this.NumberedSet(Collection<? extends T> coll) Creates a new set that contains all distinct elements incoll.NumberedSet(Iterator<? extends T> coll) Creates a new set that contains all distinct elements incoll.NumberedSet(T[] items) Creates a new set that contains all distinct elements initems. -
Method Summary
Modifier and TypeMethodDescriptionbooleanSets the key at the specified index.booleanbooleanAdds up tocountitems, starting fromoffset, in the Orderedotherto this set, inserting starting atinsertionIndexin the iteration order.booleanAdds up tocountitems, starting fromoffset, in the T arrayarrayto this set, inserting starting atinsertionIndexin the iteration order.booleanAdds up tocountitems, starting fromoffset, in the Orderedotherto this set, inserting at the end of the iteration order.booleanaddAll(Collection<? extends T> c) booleanAdds all items in the T arrayarrayto this set, inserting at the end of the iteration order.booleanAdds up tolengthitems, starting fromoffset, in the T arrayarrayto this set, inserting at the end of the iteration order.intaddOrIndex(T t) If the given itemtis present, this returns its index without modifying the NumberedSet; otherwise, it adds t to the end of the collection and returns the index for it there.booleanChanges the itembeforetoafterwithout changing its position in the order or its value.booleanChanges the item at the givenindexin the order toafter, without changing the ordering of other entries or any values.appendTo(StringBuilder sb, String entrySeparator, boolean braces) appendTo(StringBuilder sb, String itemSeparator, boolean braces, Appender<T> keyAppender) Appends to a StringBuilder from the contents of this NumberedSet, but uses the givenAppenderto convert all keys to a customizable representation and append them to a StringBuilder.voidclear()voidclear(int maximumCapacity) Clears the map and reduces the size of the backing arrays to be the specified capacity / loadFactor, if they are larger.booleanReturns true if this NumberedSet contains the given item, or false otherwise.booleancontainsAll(Object[] values) Returnstrueif this collection contains all the elements in the specified array.booleancontainsAll(Object[] values, int offset, int length) Returnstrueif this collection contains all the elements in the specified array starting fromoffsetand usinglengthitems from the array.booleancontainsAll(Collection<?> c) booleancontainsAny(Object[] values) booleancontainsAny(Object[] values, int offset, int length) LikeCollection.containsAll(Collection), but returns true immediately if any item in the givenarrayis present in this EnhancedCollection.booleancontainsAnyIterable(Iterable<?> c) LikeCollection.containsAll(Collection), but returns true immediately if any item in the given Iterableotheris present in this EnhancedCollection.voidensureCapacity(int additionalCapacity) Increases the size of the backing array to accommodate the specified number of additional items / loadFactor.booleanprotected booleanCompares the objects left and right, which are usually keys, for equality, returning true if they are considered equal.first()Attempts to get the first item in this EnhancedCollection, where "first" is only defined meaningfully if this type is ordered.getAt(int index) Returns the item at the given index, which must be at least 0 and less thansize().intGets the default value, aintwhich is returned byindexOf(Object)if the key is not found.intGets the current hashMultiplier, used inplace(Object)to mix hash codes.floatintGets the length of the internal array used to store all keys, as well as empty space awaiting more items to be entered.inthashCode()intGets the index of a given item in this set's ordering.intindexOfOrDefault(Object item, int defaultValue) Gets the index of a given item in this set's ordering.booleanisEmpty()iterator()Returns aListIteratorstarting at index 0.Returns aListIteratorstarting at index 0.listIterator(int index) Returns aListIteratorstarting at the specified index.booleannotEmpty()order()Gets the ObjectList of T items that this data structure holds, in the order it uses for iteration.static <T> NumberedSet<T>parse(String str, String delimiter, PartialParser<T> parser) Callsparse(String, String, PartialParser, boolean)with brackets set to false.static <T> NumberedSet<T>parse(String str, String delimiter, PartialParser<T> parser, boolean brackets) Creates a new collection and fills it by callingEnhancedCollection.addLegible(String, String, PartialParser, int, int)on either all ofstr(ifbracketsis false) orstrwithout its first and last chars (ifbracketsis true).static <T> NumberedSet<T>parse(String str, String delimiter, PartialParser<T> parser, int offset, int length) Creates a new collection and fills it by callingEnhancedCollection.addLegible(String, String, PartialParser, int, int)with the given five parameters as-is.protected intReturns an index >= 0 and <=ObjectIntMap.maskfor the specifieditem, mixed.booleanTries to remove an item from this set and callsrenumber(int)if that item was removedbooleanBulk-removes each item in the given array from this set.booleanBulk-removes each item in the given array from this set.booleanremoveAll(Collection<?> c) removeAt(int index) Removes and returns the item at the given index in this set's order.voidremoveRange(int start, int end) Removes the items between the specified start index, inclusive, and end index, exclusive.voidrenumber()Reassigns all index values to matchorder().voidrenumber(int start) Reassigns the index values for each index starting withstart, and going to the end.voidresize(int newSize) booleanretainAll(Collection<?> c) voidsetDefaultValue(int defaultValue) Sets the default value, aintwhich is returned byindexOf(Object)if the key is not found.voidsetHashMultiplier(int hashMultiplier) Sets the hashMultiplier to the given int, which will be made odd if even and always negative (by OR-ing with 0x80000001).voidsetLoadFactor(float loadFactor) voidshrink(int maximumCapacity) intsize()Returns the number of elements in this Arrangeable.Object[]toArray()<T1> T1[]toArray(T1[] a) toString()Delegates totoString(String, boolean)with the given itemSeparator and without braces.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.Makes a String from the contents of this NumberedSet, but uses the givenAppenderto convert all set items to a customizable representation and append them to a temporary StringBuilder.voidtruncate(int newSize) Reduces the size of the set to the specified size.static <T> NumberedSet<T>with()Constructs an empty set given the type as a generic type argument.static <T> NumberedSet<T>with(T item) Creates a new NumberedSet that holds only the given item, but can be resized.static <T> NumberedSet<T>with(T... varargs) Creates a new NumberedSet that holds only the given items, but can be resized.static <T> NumberedSet<T>with(T item0, T item1) Creates a new NumberedSet that holds only the given items, but can be resized.static <T> NumberedSet<T>with(T item0, T item1, T item2) Creates a new NumberedSet that holds only the given items, but can be resized.static <T> NumberedSet<T>with(T item0, T item1, T item2, T item3) Creates a new NumberedSet that holds only the given items, but can be resized.static <T> NumberedSet<T>with(T item0, T item1, T item2, T item3, T item4) Creates a new NumberedSet that holds only the given items, but can be resized.static <T> NumberedSet<T>with(T item0, T item1, T item2, T item3, T item4, T item5) Creates a new NumberedSet that holds only the given items, but can be resized.static <T> NumberedSet<T>with(T item0, T item1, T item2, T item3, T item4, T item5, T item6) Creates a new NumberedSet that holds only the given items, but can be resized.static <T> NumberedSet<T>with(T item0, T item1, T item2, T item3, T item4, T item5, T item6, T item7) Creates a new NumberedSet that holds only the given items, but can be resized.Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface com.github.tommyettinger.ds.Arrangeable
rearrange, shuffleMethods inherited from interface java.util.Collection
parallelStream, removeIf, stream, toArrayMethods inherited from interface com.github.tommyettinger.ds.EnhancedCollection
add, add, add, addAll, addAllIterable, addLegible, addLegible, addVarargs, appendTo, appendTo, containsAll, containsAllIterable, containsAny, removeAll, removeAllIterable, removeEach, removeEach, removeEach, removeEachIterableMethods inherited from interface com.github.tommyettinger.ds.Ordered
getOrderType, random, random, reverse, selectRanked, selectRankedIndex, shuffle, sort, swapMethods inherited from interface java.util.Set
spliterator
-
Field Details
-
map
-
iterator1
-
iterator2
-
-
Constructor Details
-
NumberedSet
public NumberedSet() -
NumberedSet
public NumberedSet(int initialCapacity, float loadFactor) -
NumberedSet
public NumberedSet(int initialCapacity) -
NumberedSet
-
NumberedSet
Can be used to make a NumberedSet from anyOrderedmap or set with Object keys or items, using the keys for a map and the items for a set.- Parameters:
ordered- anyOrderedwith the same type as this NumberSet
-
NumberedSet
Creates a new set that contains all distinct elements incoll.- Parameters:
coll- all distinct items in this Collection will become items in this NumberedSet
-
NumberedSet
Creates a new set that contains all distinct elements incoll.- Parameters:
coll- all distinct items in this Collection will become items in this NumberedSet
-
NumberedSet
Creates a new set that contains all distinct elements initems.- Parameters:
items- all distinct elements in this array will become items in this NumberedSet
-
NumberedSet
Creates a new set by copyingcountitems from the given Ordered, starting atoffsetin that Ordered, into this.- Parameters:
other- another Ordered of the same typeoffset- the first index in other's ordering to draw an item fromcount- how many items to copy from other
-
-
Method Details
-
place
Returns an index >= 0 and <=ObjectIntMap.maskfor the specifieditem, mixed.- Parameters:
item- a non-null Object; its hashCode() method should be used by most implementations- Returns:
- an index between 0 and
ObjectIntMap.mask(both inclusive)
-
equate
Compares the objects left and right, which are usually keys, for equality, returning true if they are considered equal. This is used by the rest of this class to determine whether two keys are considered equal. Normally, this returnsleft.equals(right), but subclasses can override it to use reference equality, fuzzy equality, deep array equality, or any other custom definition of equality. Usually,place(Object)is also overridden if this method is.
You can override this, which will affect the internal map that NumberedSet uses.- Parameters:
left- must be non-null; typically a key being compared, but not necessarilyright- may be null; typically a key being compared, but can often be null for an empty key slot, or some other type- Returns:
- true if left and right are considered equal for the purposes of this class
-
order
Description copied from interface:OrderedGets the ObjectList of T items that this data structure holds, in the order it uses for iteration. This should usually return a direct reference to an ObjectList used inside this object, so changes to the list will affect this. -
renumber
public void renumber()Reassigns all index values to matchorder(). This should be called if you have removed any items usingIterator.remove()from this NumberedSet, since the iterator's remove() method doesn't update the numbering on its own. Use this method if you don't know the first incorrect index, orrenumber(int)if you do. Note that you can remove multiple items using the iterator, and only need to renumber just before you need the indices (such as forindexOf(Object)). -
renumber
public void renumber(int start) Reassigns the index values for each index starting withstart, and going to the end. This should be called if you have removed any items usingIterator.remove()from this NumberedSet, since the iterator's remove() method doesn't update the numbering on its own. Userenumber()with no argument if you don't know the first incorrect index, or this method if you do. Note that you can remove multiple items using the iterator, and only need to renumber just before you need the indices (such as forindexOf(Object)).- Parameters:
start- the first index to reassign, which must be non-negative
-
remove
Tries to remove an item from this set and callsrenumber(int)if that item was removed -
containsAll
- Specified by:
containsAllin interfaceCollection<T>- Specified by:
containsAllin interfaceSet<T>
-
containsAll
Description copied from interface:EnhancedCollectionReturnstrueif this collection contains all the elements in the specified array.- Specified by:
containsAllin interfaceEnhancedCollection<T>- Parameters:
values- 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- See Also:
-
containsAll
Description copied from interface:EnhancedCollectionReturnstrueif this collection contains all the elements in the specified array starting fromoffsetand usinglengthitems from the array.- Specified by:
containsAllin interfaceEnhancedCollection<T>- Parameters:
values- 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- See Also:
-
containsAnyIterable
Description copied from interface:EnhancedCollectionLikeCollection.containsAll(Collection), but returns true immediately if any item in the given Iterableotheris present in this EnhancedCollection.- Specified by:
containsAnyIterablein interfaceEnhancedCollection<T>- Parameters:
c- 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
- Specified by:
containsAnyin interfaceEnhancedCollection<T>
-
containsAny
Description copied from interface:EnhancedCollectionLikeCollection.containsAll(Collection), but returns true immediately if any item in the givenarrayis present in this EnhancedCollection.- Specified by:
containsAnyin interfaceEnhancedCollection<T>- Parameters:
values- 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
-
addAll
-
addAll
Adds up tocountitems, starting fromoffset, in the Orderedotherto this set, inserting at the end of the iteration order.- Parameters:
other- a non-nullOrderedofToffset- the first index inotherto usecount- how many indices inotherto use- Returns:
- true if this is modified by this call, as
addAll(Collection)does
-
addAll
Adds up tocountitems, starting fromoffset, in the Orderedotherto this set, inserting starting atinsertionIndexin the iteration order.- Parameters:
insertionIndex- where to insert into the iteration orderother- a non-nullOrderedofToffset- the first index inotherto usecount- how many indices inotherto use- Returns:
- true if this is modified by this call, as
addAll(Collection)does
-
addAll
Adds all items in the T arrayarrayto this set, inserting at the end of the iteration order.- Specified by:
addAllin interfaceEnhancedCollection<T>- Parameters:
array- a non-null array ofT- Returns:
- true if this is modified by this call, as
addAll(Collection)does
-
addAll
Adds up tolengthitems, starting fromoffset, in the T arrayarrayto this set, inserting at the end of the iteration order.- Specified by:
addAllin interfaceEnhancedCollection<T>- Parameters:
array- a non-null array ofToffset- the first index inotherto uselength- how many indices inotherto use- Returns:
- true if this is modified by this call, as
addAll(Collection)does
-
addAll
Adds up tocountitems, starting fromoffset, in the T arrayarrayto this set, inserting starting atinsertionIndexin the iteration order.- Parameters:
insertionIndex- where to insert into the iteration orderarray- a non-null array ofToffset- the first index inotherto usecount- how many indices inotherto use- Returns:
- true if this is modified by this call, as
addAll(Collection)does
-
retainAll
-
removeAll
-
removeAll
Bulk-removes each item in the given array from this set. If an item appears more than once inarr, this will be able to quickly verify that it was removed the first time it appeared, and won't spend as long processing later items. This callsrenumber()only after all removals were completed, and only if one or more items were actually removed.- Specified by:
removeAllin interfaceEnhancedCollection<T>- Parameters:
arr- a non-null array of items to remove from this set- Returns:
- true if this had one or more items removed, or false if it is unchanged
-
removeAll
Bulk-removes each item in the given array from this set. If an item appears more than once invalues, this will be able to quickly verify that it was removed the first time it appeared, and won't spend as long processing later items. This callsrenumber()only after all removals were completed, and only if one or more items were actually removed.- Specified by:
removeAllin interfaceEnhancedCollection<T>- Parameters:
values- a non-null array of items to remove from this setoffset- the index of the first item in values to removelength- how many items, at most, to get from values and remove from this- Returns:
- true if this had one or more items removed, or false if it is unchanged
-
removeAt
Removes and returns the item at the given index in this set's order.- Parameters:
index- the index of the item to remove- Returns:
- the removed item
-
ensureCapacity
public void ensureCapacity(int additionalCapacity) Increases the size of the backing array to accommodate the specified number of additional items / loadFactor. Useful before adding many items to avoid multiple backing array resizes.- Parameters:
additionalCapacity- how many more items this must be able to hold; the load factor increases the actual capacity change
-
getHashMultiplier
public int getHashMultiplier()Gets the current hashMultiplier, used inplace(Object)to mix hash codes. IfsetHashMultiplier(int)is never called, the hashMultiplier will always be drawn fromUtilities.GOOD_MULTIPLIERS, with the index equal to64 - shift.- Returns:
- the current hashMultiplier
-
setHashMultiplier
public void setHashMultiplier(int hashMultiplier) Sets the hashMultiplier to the given int, which will be made odd if even and always negative (by OR-ing with 0x80000001). This can be any negative, odd int, but should almost always be drawn fromUtilities.GOOD_MULTIPLIERSor something like it.- Parameters:
hashMultiplier- any int; will be made odd if even.
-
getTableSize
public int getTableSize()Gets the length of the internal array used to store all keys, as well as empty space awaiting more items to be entered. This length is equal to the length of the array used to store all values, and empty space for values, here. This is also called the capacity.- Returns:
- the length of the internal array that holds all keys
-
alter
Changes the itembeforetoafterwithout changing its position in the order or its value. Returns true ifafterhas been added to the NumberedSet andbeforehas been removed; returns false ifafteris already present orbeforeis not present. If you are iterating over a NumberedSet and have an index, you should preferalterAt(int, Object), which doesn't need to search for an index like this does and so can be faster.- Parameters:
before- an item that must be present for this to succeedafter- an item that must not be in this map for this to succeed- Returns:
- true if
beforewas removed andafterwas added, false otherwise
-
alterAt
Changes the item at the givenindexin the order toafter, without changing the ordering of other entries or any values. Ifafteris already present, this returns false; it will also return false ifindexis invalid for the size of this map. Otherwise, it returns true. Unlikealter(Object, Object), this operates in constant time.- Parameters:
index- the index in the order of the item to change; must be non-negative and less thansize()after- the item that will replace the contents atindex; this item must not be present for this to succeed- Returns:
- true if
aftersuccessfully replaced the item atindex, false otherwise
-
getAt
Returns the item at the given index, which must be at least 0 and less thansize().- Parameters:
index- the index to retrieve; must be between 0, inclusive, andsize(), exclusive- Returns:
- the item at
index
-
clear
public void clear(int maximumCapacity) Clears the map and reduces the size of the backing arrays to be the specified capacity / loadFactor, if they are larger. -
clear
public void clear() -
indexOf
Gets the index of a given item in this set's ordering. Unlike most collections, this takes O(1) time here. This returnsgetDefaultValue()(usually -1) if the item was not present.- Parameters:
item- the item to retrieve the index for- Returns:
- the index of the item, or
getDefaultValue()(usually -1) if it was not found
-
indexOfOrDefault
Gets the index of a given item in this set's ordering. Unlike most collections, this takes O(1) time here. This returnsdefaultValueif the item was not present.- Parameters:
item- the item to retrieve the index for- Returns:
- the index of the item, or
defaultValueif it was not found
-
notEmpty
public boolean notEmpty() -
size
public int size()Description copied from interface:ArrangeableReturns the number of elements in this Arrangeable. Often this is shared withCollection.size(), but isn't always.- Specified by:
sizein interfaceArrangeable- Specified by:
sizein interfaceCollection<T>- Specified by:
sizein interfaceSet<T>- Returns:
- the number of elements in this Arrangeable
-
isEmpty
public boolean isEmpty() -
getDefaultValue
public int getDefaultValue()Gets the default value, aintwhich is returned byindexOf(Object)if the key is not found. If not changed, the default value is -1 .- Returns:
- the current default value
-
setDefaultValue
public void setDefaultValue(int defaultValue) Sets the default value, aintwhich is returned byindexOf(Object)if the key is not found. If not changed, the default value is -1 . Note thatindexOfOrDefault(Object, int)is also available, which allows specifying a "not-found" value per-call.- Parameters:
defaultValue- may be any int; should usually be one that doesn't occur as a typical value
-
shrink
public void shrink(int maximumCapacity) -
contains
Returns true if this NumberedSet contains the given item, or false otherwise. -
truncate
public void truncate(int newSize) Reduces the size of the set to the specified size. If the set is already smaller than the specified size, no action is taken.- Parameters:
newSize- the target size to try to reach by removing items, if smaller than the current size
-
iterator
Returns aListIteratorstarting at index 0. This caches the iterator to avoid repeated allocation, and so is not suitable for nested iteration. You can useNumberedSetIterator(NumberedSet)if you need nested iteration. This is equivalent tolistIterator().- Specified by:
iteratorin interfaceCollection<T>- Specified by:
iteratorin interfaceIterable<T>- Specified by:
iteratorin interfaceSet<T>- Returns:
- a ListIterator, or more specifically a
NumberedSet.NumberedSetIteratorover this set
-
listIterator
Returns aListIteratorstarting at index 0. This caches the iterator to avoid repeated allocation, and so is not suitable for nested iteration. You can useNumberedSetIterator(NumberedSet)if you need nested iteration.- Returns:
- a ListIterator, or more specifically a
NumberedSet.NumberedSetIteratorover this set
-
listIterator
Returns aListIteratorstarting at the specified index. This caches the iterator to avoid repeated allocation, and so is not suitable for nested iteration. You can useNumberedSetIterator(NumberedSet, int)if you need nested iteration. Giving an index of 0 is equivalent to callinglistIterator(), and starts at the first item in the order.- Parameters:
index- the first index in this set's order to iterate from- Returns:
- a ListIterator, or more specifically a
NumberedSet.NumberedSetIteratorover this set
-
removeRange
public void removeRange(int start, int end) Removes the items between the specified start index, inclusive, and end index, exclusive. Note that this takes different arguments than some other range-related methods; this needs a start index and an end index, rather than a count of items. This matches the behavior in the JDK collections.- Specified by:
removeRangein interfaceOrdered<T>- Parameters:
start- the first index to remove, inclusiveend- the last index (after what should be removed), exclusive
-
toArray
-
toArray
public <T1> T1[] toArray(T1[] a) -
add
-
addOrIndex
If the given itemtis present, this returns its index without modifying the NumberedSet; otherwise, it adds t to the end of the collection and returns the index for it there.- Parameters:
t- an item to get the index of, adding it if not present- Returns:
- the index of
tin this Arrangement
-
add
Sets the key at the specified index. Returns true if the key was not already in the set. If this set already contains the key, the existing key's index is changed if needed and false is returned. Note, the order of the parameters matches the order inObjectListand the rest of the JDK, not OrderedSet in libGDX.- Parameters:
index- where in the iteration order to add the given key, or to move it if already presentkey- what T item to try to add, if not already present- Returns:
- true if the key was added for the first time, or false if the key was already present (even if moved)
-
resize
public void resize(int newSize) -
equals
-
getLoadFactor
public float getLoadFactor() -
setLoadFactor
public void setLoadFactor(float loadFactor) -
first
Description copied from interface:EnhancedCollectionAttempts 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.- Specified by:
firstin interfaceEnhancedCollection<T>- Returns:
- the first item in this EnhancedCollection, as produced by
Collection.iterator()
-
hashCode
public int hashCode() -
toString
-
toString
Delegates totoString(String, boolean)with the given itemSeparator and without braces. This is different fromtoString(), which includes braces by default.- Specified by:
toStringin interfaceEnhancedCollection<T>- Parameters:
itemSeparator- how to separate set items, such as", "- Returns:
- a new String representing this set
-
toString
Description copied from interface:EnhancedCollectionMakes 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 toEnhancedCollection.appendTo(CharSequence, String, boolean).- Specified by:
toStringin interfaceEnhancedCollection<T>- Parameters:
itemSeparator- how to separate items, such as", "braces- 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 NumberedSet, but uses the givenAppenderto convert all set items to a customizable representation and append them to a temporary StringBuilder. To use the default String representation, you can useAppender::appendas an appender.- Specified by:
toStringin interfaceEnhancedCollection<T>- Parameters:
entrySeparator- how to separate set items, such as", "braces- true to wrap the output in curly braces, or false to omit themkeyAppender- a function that takes a StringBuilder and a T, and returns the modified StringBuilder- Returns:
- a new String representing this set
-
appendTo
-
appendTo
public StringBuilder appendTo(StringBuilder sb, String itemSeparator, boolean braces, Appender<T> keyAppender) Appends to a StringBuilder from the contents of this NumberedSet, but uses the givenAppenderto convert all keys to a customizable representation and append them to a StringBuilder. To use the default String representation, you can useAppender::appendas an appender.- Parameters:
sb- a StringBuilder that this can append toitemSeparator- how to separate set items, such as", "braces- true to wrap the output in curly braces, or false to omit themkeyAppender- a function that takes a StringBuilder and a T, and returns the modified StringBuilder- Returns:
sb, with the appended items of this set
-
with
Constructs an empty set given the type as a generic type argument. This is usually less useful than just using the constructor, but can be handy in some code-generation scenarios when you don't know how many arguments you will have.- Type Parameters:
T- the type of items; must be given explicitly- Returns:
- a new set containing nothing
-
with
Creates a new NumberedSet that holds only the given item, but can be resized.- Type Parameters:
T- the type of item, typically inferred- Parameters:
item- one T item- Returns:
- a new NumberedSet that holds the given item
-
with
Creates a new NumberedSet that holds only the given items, but can be resized.- Type Parameters:
T- the type of item, typically inferred- Parameters:
item0- a T itemitem1- a T item- Returns:
- a new NumberedSet that holds the given items
-
with
Creates a new NumberedSet that holds only the given items, but can be resized.- Type Parameters:
T- the type of item, typically inferred- Parameters:
item0- a T itemitem1- a T itemitem2- a T item- Returns:
- a new NumberedSet that holds the given items
-
with
Creates a new NumberedSet that holds only the given items, but can be resized.- Type Parameters:
T- the type of item, typically inferred- Parameters:
item0- a T itemitem1- a T itemitem2- a T itemitem3- a T item- Returns:
- a new NumberedSet that holds the given items
-
with
Creates a new NumberedSet that holds only the given items, but can be resized.- Type Parameters:
T- the type of item, typically inferred- Parameters:
item0- a T itemitem1- a T itemitem2- a T itemitem3- a T itemitem4- a T item- Returns:
- a new NumberedSet that holds the given items
-
with
Creates a new NumberedSet that holds only the given items, but can be resized.- Type Parameters:
T- the type of item, typically inferred- Parameters:
item0- a T itemitem1- a T itemitem2- a T itemitem3- a T itemitem4- a T itemitem5- a T item- Returns:
- a new NumberedSet that holds the given items
-
with
public static <T> NumberedSet<T> with(T item0, T item1, T item2, T item3, T item4, T item5, T item6) Creates a new NumberedSet that holds only the given items, but can be resized.- Type Parameters:
T- the type of item, typically inferred- Parameters:
item0- a T itemitem1- a T itemitem2- a T itemitem3- a T itemitem4- a T itemitem5- a T itemitem6- a T item- Returns:
- a new NumberedSet that holds the given items
-
with
public static <T> NumberedSet<T> with(T item0, T item1, T item2, T item3, T item4, T item5, T item6, T item7) Creates a new NumberedSet that holds only the given items, but can be resized.- Type Parameters:
T- the type of item, typically inferred- Parameters:
item0- a T itemitem1- a T itemitem2- a T itemitem3- a T itemitem4- a T itemitem5- a T itemitem6- a T item- Returns:
- a new NumberedSet that holds the given items
-
with
Creates a new NumberedSet that holds only the given items, but can be resized. This overload will only be used when an array is supplied and the type of the items requested is the component type of the array, or if varargs are used and there are 9 or more arguments.- Type Parameters:
T- the type of item, typically inferred- Parameters:
varargs- a T varargs or T array; remember that varargs allocate- Returns:
- a new NumberedSet that holds the given items
-
parse
Callsparse(String, String, PartialParser, boolean)with brackets set to false.- Parameters:
str- a String that will be parsed in fulldelimiter- the delimiter between items in strparser- a PartialParser that returns aTitem from a section ofstr- Returns:
- a new collection parsed from str
-
parse
public static <T> NumberedSet<T> parse(String str, String delimiter, PartialParser<T> parser, boolean brackets) Creates a new collection and fills it by callingEnhancedCollection.addLegible(String, String, PartialParser, int, int)on either all ofstr(ifbracketsis false) orstrwithout its first and last chars (ifbracketsis true). Each item is expected to be separated bydelimiter.- Parameters:
str- a String that will be parsed in full (depending on brackets)delimiter- the delimiter between items in strparser- a PartialParser that returns aTitem from a section ofstrbrackets- if true, the first and last chars in str will be ignored- Returns:
- a new collection parsed from str
-
parse
public static <T> NumberedSet<T> parse(String str, String delimiter, PartialParser<T> parser, int offset, int length) Creates a new collection and fills it by callingEnhancedCollection.addLegible(String, String, PartialParser, int, int)with the given five parameters as-is.- Parameters:
str- a String that will have the given section parseddelimiter- the delimiter between items in strparser- a PartialParser that returns aTitem from a section ofstroffset- the first position to parse in str, inclusivelength- how many chars to parse, starting from offset- Returns:
- a new collection parsed from str
-