Package com.github.tommyettinger.ds
Interface PrimitiveCollection.OfChar
- All Superinterfaces:
PrimitiveCollection<Character>
- All Known Subinterfaces:
PrimitiveSet.SetOfChar
- All Known Implementing Classes:
CharBag,CharBitSet,CharDeque,CharList
- Enclosing interface:
- PrimitiveCollection<T>
A PrimitiveCollection with unboxed
char items.-
Nested Class Summary
Nested classes/interfaces inherited from interface com.github.tommyettinger.ds.PrimitiveCollection
PrimitiveCollection.OfBoolean, PrimitiveCollection.OfByte, PrimitiveCollection.OfChar, PrimitiveCollection.OfDouble, PrimitiveCollection.OfFloat, PrimitiveCollection.OfInt, PrimitiveCollection.OfLong, PrimitiveCollection.OfShort -
Method Summary
Modifier and TypeMethodDescriptionbooleanadd(char item) default booleanaddAll(char[] array) default booleanaddAll(char[] array, int offset, int length) default booleanaddAll(PrimitiveCollection.OfChar other) default booleanaddAll(CharIterator it) default voidaddDense(CharSequence cs) Adds items to this PrimitiveCollection drawn from the result oftoDenseString()ordenseAppendTo(CharSequence, boolean).default voidaddDense(CharSequence cs, int offset, int length) Adds items to this PrimitiveCollection drawn from the result oftoDenseString()ordenseAppendTo(CharSequence, boolean).default voidaddLegible(String str, String delimiter) Adds items to this PrimitiveCollection drawn from the result oftoString(String)orappendTo(CharSequence, String, boolean).default voidaddLegible(String str, String delimiter, int offset, int length) Adds items to this PrimitiveCollection drawn from the result oftoString(String)orappendTo(CharSequence, String, boolean).default booleanaddVarargs(char... 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>
SDelegates toappendTo(CharSequence, String, boolean, CharAppender)usingCharAppender.DEFAULTto append int items.default <S extends CharSequence & Appendable>
SappendTo(S sb, String separator, boolean brackets, CharAppender appender) Appends to an Appendable CharSequence from the contents of this PrimitiveCollection, but uses the givenCharAppenderto convert each item to a customizable representation and append them tosb.booleancontains(char item) default booleancontainsAll(char[] array) default booleancontainsAll(char[] array, int offset, int length) default booleandefault booleandefault booleancontainsAny(char[] array) default booleancontainsAny(char[] array, int offset, int length) default booleandefault booleandefault <S extends CharSequence & Appendable>
SdenseAppendTo(S sb, boolean brackets) Appends tosbany items in this PrimitiveCollection as the char items themselves, with no separators and optionally with square brackets surrounding the text ifbracketsis true.default booleanequalContents(Object other) Compares this PrimitiveCollection.OfChar with a PrimitiveSet.SetOfChar by checking their identity, their types (both must implement PrimitiveCollection.OfChar, and other must implement PrimitiveSet.SetOfChar), and their sizes, before checking if other contains each item in this PrimitiveCollection.OfChar, in any order or quantity.default charfirst()Attempts to get the first item in this PrimitiveCollection, where "first" is only defined meaningfully if this type is ordered.default voidforEach(com.github.tommyettinger.function.CharConsumer action) Performs the given action for each element of thePrimitiveCollection.OfCharuntil all elements have been processed or the action throws an exception.iterator()static char[]readArrayDense(char[] buffer, int bufferIndex, CharSequence cs, int offset, int length) Reads zero or more items from the result oftoDenseString()ordenseAppendTo(CharSequence, boolean)and assigns them to consecutive items inbuffer, starting atbufferIndex.static char[]Reads zero or more items from the result oftoDenseString()ordenseAppendTo(CharSequence, boolean)and assigns them to consecutive items in a new char array sized tocs.length().static charreadDense(CharSequence cs, int position) Reads in exactly onecharfrom the givenpositionin the givenCharSequence, such as a String or StringBuilder, and returns that char.booleanremove(char item) default booleanremoveAll(char[] array) default booleanremoveAll(char[] array, int offset, int length) default booleanRemoves from this collection all occurrences of any elements contained in the specified other collection.default booleanRemoves from this collection all occurrences of any elements contained in the specified CharIterator.default booleanremoveEach(char[] array) default booleanremoveEach(char[] array, int offset, int length) default booleanRemoves from this collection element-wise occurrences of elements contained in the specified other collection.default booleanRemoves from this collection element-wise occurrences of elements contained in the specified CharIterator.default booleanremoveIf(com.github.tommyettinger.function.CharPredicate filter) Removes all the elements of this collection that satisfy the given predicate.default booleanretainAll(char[] array) default booleanretainAll(char[] array, int offset, int length) default booleandefault char[]toArray()Allocates a new char array with exactlyPrimitiveCollection.size()items, fills it with the contents of this PrimitiveCollection, and returns it.default char[]toArray(char[] receiver) Fills the given array with the entire contents of this PrimitiveCollection, up toPrimitiveCollection.size()items, or if receiver is not large enough, then this allocates a new char array withPrimitiveCollection.size()items and returns that.default StringReturns a String representing this PrimitiveCollection as the char items themselves, with no surrounding brackets.default StringtoDenseString(boolean brackets) Returns a String representing this PrimitiveCollection as the char items themselves, with surrounding square brackets ifbracketsis true.default StringDelegates totoString(String, boolean)with the given separator and without brackets.default StringDelegates toappendTo(CharSequence, String, boolean)using a new StringBuilder and converts it to a new String.default StringtoString(String separator, boolean brackets, CharAppender appender) Makes a String from the contents of this PrimitiveCollection, but uses the givenCharAppenderto convert each item to a customizable representation and append them to a StringBuilder.
-
Method Details
-
add
boolean add(char item) -
remove
boolean remove(char item) -
contains
boolean contains(char item) -
addAll
-
addAll
-
addAll
default boolean addAll(char[] array) -
addAll
default boolean addAll(char[] array, int offset, int length) -
addVarargs
default boolean addVarargs(char... varargs) 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.- Parameters:
varargs- 0 or more items to add; may also be an array- Returns:
- true if this collection was modified
-
removeAll
Removes from this collection all occurrences of any elements contained in the specified other collection.- Parameters:
other- a primitive collection of char items to remove fully, such as a CharList or a CharDeque- Returns:
- true if this collection was modified.
-
removeAll
Removes from this collection all occurrences of any elements contained in the specified CharIterator.- Parameters:
it- a CharIterator of items to remove fully- Returns:
- true if this collection was modified.
-
removeAll
default boolean removeAll(char[] array) -
removeAll
default boolean removeAll(char[] array, int offset, int length) -
removeEach
Removes from this collection element-wise occurrences of elements contained in the specified other collection. 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- a primitive collection of char items to remove one-by-one, such as a CharList or a CharDeque- Returns:
- true if this collection was modified.
-
removeEach
Removes from this collection element-wise occurrences of elements contained in the specified CharIterator. 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 init. Ifithas the same contents as this collection or has additional items, then removing each ofitwill clear this.- Parameters:
it- a CharIterator of items to remove one-by-one- Returns:
- true if this collection was modified.
-
removeEach
default boolean removeEach(char[] array) -
removeEach
default boolean removeEach(char[] array, int offset, int length) -
containsAll
-
containsAll
-
containsAll
default boolean containsAll(char[] array) -
containsAll
default boolean containsAll(char[] array, int offset, int length) -
containsAny
-
containsAny
-
containsAny
default boolean containsAny(char[] array) -
containsAny
default boolean containsAny(char[] array, int offset, int length) -
removeIf
default boolean removeIf(com.github.tommyettinger.function.CharPredicate filter) Removes all the elements of this collection that satisfy the given predicate. Errors or runtime exceptions thrown during iteration or by the predicate are relayed to the caller.- Parameters:
filter- a predicate which returnstruefor elements to be removed- Returns:
trueif any elements were removed- Throws:
UnsupportedOperationException- if elements cannot be removed from this collection. Implementations may throw this exception if a matching element cannot be removed or if, in general, removal is not supported.- "Implementation Requirements"
- The default implementation traverses all elements of the collection using
its
iterator(). Each matching element is removed usingIterator.remove(). If the collection's iterator does not support removal then anUnsupportedOperationExceptionwill be thrown on the first matching element.
-
retainAll
-
retainAll
default boolean retainAll(char[] array) -
retainAll
default boolean retainAll(char[] array, int offset, int length) -
iterator
CharIterator iterator()- Specified by:
iteratorin interfacePrimitiveCollection<Character>
-
toArray
default char[] toArray()Allocates a new char array with exactlyPrimitiveCollection.size()items, fills it with the contents of this PrimitiveCollection, and returns it.- Returns:
- a new char array
-
toArray
default char[] toArray(char[] receiver) Fills the given array with the entire contents of this PrimitiveCollection, up toPrimitiveCollection.size()items, or if receiver is not large enough, then this allocates a new char array withPrimitiveCollection.size()items and returns that.- Parameters:
receiver- a char array that will be filled with the items from this, if possible- Returns:
receiver, if it was modified, or a new char array otherwise
-
forEach
default void forEach(com.github.tommyettinger.function.CharConsumer action) Performs the given action for each element of thePrimitiveCollection.OfCharuntil all elements have been processed or the action throws an exception. Actions are performed in the order of iteration, if that order is specified. Exceptions thrown by the action are relayed to the caller.- Parameters:
action- The action to be performed for each element
-
first
default char first()Attempts to get the first item in this PrimitiveCollection, 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 usesiterator(), tries to get the first item, or throws an IllegalStateException if this is empty.- Returns:
- the first item in this PrimitiveCollection, as produced by
iterator() - Throws:
IllegalStateException- if this is empty
-
equalContents
Compares this PrimitiveCollection.OfChar with a PrimitiveSet.SetOfChar by checking their identity, their types (both must implement PrimitiveCollection.OfChar, and other must implement PrimitiveSet.SetOfChar), and their sizes, before checking if other contains each item in this PrimitiveCollection.OfChar, in any order or quantity. This is most useful for the key "set" or value collection in a primitive-backed map, since quantity doesn't matter for keys and order doesn't matter for either. Many implementations may need to reset the iterator on this PrimitiveCollection.OfChar, but that isn't necessary forother.
This is not meant for general object equality, but instead for equality following Set semantics. Classes that implement PrimitiveCollection that are not Set-like should usually not use this.- Parameters:
other- another Object that should be a PrimitiveSet.SetOfChar- Returns:
- true if other is a PrimitiveSet.SetOfChar with exactly the same items, false otherwise
-
toString
Delegates totoString(String, boolean)with the given separator and without brackets.- Parameters:
separator- how to separate items, such as", "- Returns:
- a new String representing this PrimitiveCollection
-
toString
Delegates toappendTo(CharSequence, String, boolean)using a new StringBuilder and converts it to a new String.- Parameters:
separator- how to separate items, such as", "brackets- true to wrap the output in square brackets, or false to omit them- Returns:
- a new String representing this PrimitiveCollection
-
toString
Makes a String from the contents of this PrimitiveCollection, but uses the givenCharAppenderto convert each item to a customizable representation and append them to a StringBuilder. To use the default String representation, you can useCharAppender::appendas an appender, or better yet, useCharAppender.DEFAULT, which caches the above method reference when Android won't do that.- 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 an Appendable CharSequence and a char, and returns the modified sequence- Returns:
- a new String representing this PrimitiveCollection
-
appendTo
Delegates toappendTo(CharSequence, String, boolean, CharAppender)usingCharAppender.DEFAULTto append int items.- Type Parameters:
S- any type that is both a CharSequence and an Appendable, such as StringBuilder, StringBuffer, CharBuffer, or CharList- Parameters:
sb- a StringBuilder or similar 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 PrimitiveCollection
-
appendTo
default <S extends CharSequence & Appendable> S appendTo(S sb, String separator, boolean brackets, CharAppender appender) Appends to an Appendable CharSequence from the contents of this PrimitiveCollection, but uses the givenCharAppenderto convert each item to a customizable representation and append them tosb. To use the default String representation, you can useCharAppender::appendas an appender, or better yet, useCharAppender.DEFAULT, which caches the above method reference when Android won't do that.- Type Parameters:
S- any type that is both a CharSequence and an Appendable, such as StringBuilder, StringBuffer, CharBuffer, or CharList- Parameters:
sb- a StringBuilder or similar 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 an Appendable CharSequence and a char, and returns the modified sequence- Returns:
sb, with the appended items of this PrimitiveCollection
-
toDenseString
Returns a String representing this PrimitiveCollection as the char items themselves, with no surrounding brackets. This does not necessarily make semantically-well-formed text, particularly if it contains surrogate characters without matching them into pairs, but it should always be possible to append anychars.- Returns:
- a String made of "0" and "1"
-
toDenseString
Returns a String representing this PrimitiveCollection as the char items themselves, with surrounding square brackets ifbracketsis true. This does not necessarily make semantically-well-formed text, particularly if it contains surrogate characters without matching them into pairs, but it should always be possible to append anychars.- Parameters:
brackets- if true, the result will be surrounded by square brackets- Returns:
- a String made of "0" and "1", optionally with surrounding square brackets
-
denseAppendTo
Appends tosbany items in this PrimitiveCollection as the char items themselves, with no separators and optionally with square brackets surrounding the text ifbracketsis true. This does not necessarily make semantically-well-formed text, particularly if it contains surrogate characters without matching them into pairs, but it should always be possible to append anychars.- Parameters:
sb- the StringBuilder to append tobrackets- if true, square brackets will surround the appended text- Returns:
sb, for chaining
-
readDense
Reads in exactly onecharfrom the givenpositionin the givenCharSequence, such as a String or StringBuilder, and returns that char. As you may have guessed, this is equivalent toCharSequence.charAt(int). This is only here for completeness.- Parameters:
cs- a CharSequenceposition- the first position to read exactly one char from incs- Returns:
- the char at the given
positionincs
-
addDense
Adds items to this PrimitiveCollection drawn from the result oftoDenseString()ordenseAppendTo(CharSequence, boolean). Each item is exactly one character long and is exactly that character, so all chars in the Unicode BMP can be present. No surrounding brackets should be present incs(they will be treated as items).- Parameters:
cs- a CharSequence containing only BASE90 chars (between'%'and'~', both inclusive)
-
addDense
Adds items to this PrimitiveCollection drawn from the result oftoDenseString()ordenseAppendTo(CharSequence, boolean). Each item is exactly the shown character in the CharSequence. Any brackets inside the given range of characters will be interpreted as items, not as visual wrappers, so increase offset by 1 and reduce length by 2 if the original CharSequence had brackets added to it.- Parameters:
cs- a CharSequence containing arbitrary charsoffset- the first position to read chars from incslength- how many chars to read; -1 is treated as maximum length
-
readArrayDense
Reads zero or more items from the result oftoDenseString()ordenseAppendTo(CharSequence, boolean)and assigns them to consecutive items in a new char array sized tocs.length(). Each item is exactly one character and can be any Javachar.
This may be useful to parse the dense output of one primitive collection into an array to be given to a map constructor or map's addAll() method, which may be able to take an array for keys and for values.- Parameters:
cs- a CharSequence containing only BASE90 chars (between'%'and'~', both inclusive)- Returns:
- a new array sized to
cs.length()items, or sized to 0 ifcsis null
-
readArrayDense
static char[] readArrayDense(char[] buffer, int bufferIndex, CharSequence cs, int offset, int length) Reads zero or more items from the result oftoDenseString()ordenseAppendTo(CharSequence, boolean)and assigns them to consecutive items inbuffer, starting atbufferIndex. Each item is exactly one character and can be any Javachar.
This may be useful to parse the dense output of one primitive collection into an array to be given to a map constructor or map's addAll() method, which may be able to take an array for keys and for values.- Parameters:
buffer- an array that will be modified in-place; should not be nullbufferIndex- the first index inbufferto assign tocs- a CharSequence containing BASE90 chars (between'%'and'~', both inclusive)offset- the first position to read BASE90 chars from incslength- how many chars to read; -1 is treated as maximum length- Returns:
buffer, potentially after modifications
-
addLegible
Adds items to this PrimitiveCollection drawn from the result oftoString(String)orappendTo(CharSequence, String, boolean). Each item can vary significantly in length, and should useBase.BASE10digits, which should be human-readable. 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 BASE10 charsdelimiter- the String separating every item in str
-
addLegible
Adds items to this PrimitiveCollection drawn from the result oftoString(String)orappendTo(CharSequence, String, boolean). Each item can vary significantly in length, and should useBase.BASE10digits, which should be human-readable. 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 BASE10 charsdelimiter- the String separating every item in stroffset- the first position to read BASE10 chars from instrlength- how many chars to read; -1 is treated as maximum length
-