public interface CharIndexable extends PrimitiveCharIterable, Sizeable
The EMPTY
constant or emptyIndexable()
method provide the same
useful return value to indicate emptiness.
FastCharSequenceSearch
Modifier and Type | Interface and Description |
---|---|
static class |
CharIndexable.Base
Abstract base class which provides useful implementations
for
Object.equals(Object) , Object.hashCode() ,
Object.toString() . |
Modifier and Type | Field and Description |
---|---|
static CharIndexable.Base |
EMPTY
An empty indexable.
|
EMPTY_CHAR_ITERATOR
Modifier and Type | Method and Description |
---|---|
default void |
addAllTo(java.util.Collection<? super java.lang.Character> collection)
Add all elements of this indexable to the given collection.
|
default int |
addToArray(char[] array,
int pos)
Add the content of this indexable to the given array.
|
default int |
addToArray(char[] array,
int arrayPos,
int index,
int length)
Add a part of the content of this indexable to the given array.
|
default int |
addToArray(int[] array,
int pos)
Add the content of this indexable to the given int array.
|
default int |
addToArray(int[] array,
int arrayPos,
int index,
int length)
Add a part of the content of this indexable to the given intarray.
|
default CharIndexable.Base |
asBase()
Make this char indexable usable as a class with defined standard Object methods.
|
default java.lang.CharSequence |
asCharSequence()
Get a view of this char indexable as a char sequence.
|
default java.util.Collection<java.lang.Character> |
asCollection()
Get a view on this indexable as an unmodifiable collection.
|
default Indexable<java.lang.Character> |
asIndexable()
Get a view of this indexable as a standard object-based indexable.
|
default IntIndexable.Base |
asIntIndexable()
View this char indexable as an integer indexable.
|
default java.util.List<java.lang.Character> |
asList()
Get a view on this indexable as an unmodifiable list.
|
default int |
binarySearch(char value)
Do a binary search in an indexable ordered in natural ascending order.
|
default int |
binarySearch(char value,
CharOrdering order)
Do a binary search in an ordered indexable.
|
default PrimitiveCharIterator |
charIterator()
Get an primitive char iterator.
|
default PrimitiveCharIterator |
charIterator(int from,
int to)
Get a char iterator from the given sub set.
|
static int |
compare(CharIndexable indexable1,
CharIndexable indexable2)
Compare two char indexables.
|
static CharIndexable.Base |
emptyIndexable()
Get an empty indexable set.
|
static boolean |
equal(CharIndexable indexable1,
CharIndexable indexable2)
Are two char indexables equal?
|
static boolean |
equal(CharIndexable indexable1,
java.lang.Object indexable2)
Are two char indexables equal?
|
default int |
firstMatch(CharPredicate1 check)
Get the first index for which the given check is fulfilled.
|
default char |
foldLeft(char initialValue,
CharOperator2 foldOperation)
Go over all values and do a cumulative calculation.
|
default CharIndexable |
frozen()
Get a frozen version of this indexable.
|
char |
get(int index)
Get the element at the given index.
|
default char |
gyt(int index)
Pythonesque get.
|
static int |
hash(CharIndexable indexable)
Calculate a hashcode for a char indexable.
|
default CharIndexable.Base |
headSet(int toIndex)
Create an indexable subset from the last elements of this indexable set.
|
default java.lang.Iterable<java.lang.Integer> |
indexes()
Get the indexes of this indexable as an iterable range.
|
static CharIndexable.Base |
init(int size,
char value)
Get a char indexable of a given size which always returns the same value.
|
static CharIndexable |
initByIndex(int size,
IntToCharFunction1 producer)
Get a char indexable of a given size which returns elements created by index.
|
default IntIndexable |
intIndexes()
Get the indexes of this indexable as an integer indexable.
|
default java.util.PrimitiveIterator.OfInt |
intIterator()
Get an primitive int iterator.
|
default boolean |
isEmpty()
Is this indexable empty?
|
default boolean |
isOrdered()
Is this indexable sorted naturally?
|
default boolean |
isOrdered(CharOrdering order)
Is this indexable sorted according to the given ordering?
|
default boolean |
isStrictlyOrdered()
Is this indexable strictly sorted naturally?
|
default boolean |
isStrictlyOrdered(CharOrdering order)
Is this indexable strictly sorted according to the given ordering?
|
default java.util.Iterator<java.lang.Character> |
iterator()
Returns an iterator over elements of type
T . |
default int |
lastMatch(CharPredicate1 check)
Get the last index for which the given check is fulfilled.
|
default java.util.ListIterator<java.lang.Character> |
listIterator()
Returns a list iterator over elements of type
T . |
default java.lang.String |
makeString()
Convert this char indexable into a Java String.
|
default int |
nextMatch(int startIndex,
CharPredicate1 check)
Get the next index for which the given check is fulfilled.
|
default MutableCharIndexable |
ordered(CharOrdering order)
Create a (mutable) char indexable from the values of this indexable
which is ordered as defined by the given ordering.
|
default int |
previousMatch(int startIndex,
CharPredicate1 check)
Get the previous index for which the given check is fulfilled.
|
default CharIndexable.Base |
reverse()
Get this indexable but with inverted order.
|
default CharIndexable |
rotated(int steps)
Get a rotated view of this char indexable.
|
static CharIndexable.Base |
singleton(char value)
View a single char value as a char indexable of size 1.
|
default CharIndexable.Base |
subSet(int fromIndex,
int toIndex)
Get an indexable subset.
|
default CharIndexable.Base |
sybSet(int fromIndex,
int toIndex)
Get an indexable subset.
|
default CharIndexable.Base |
tailSet(int fromIndex)
Create an indexable subset from the last elements of this indexable set.
|
default char[] |
toArray()
Convert this indexable into a Java array.
|
default int[] |
toIntArray()
Convert this indexable into an int array.
|
default java.util.ArrayList<java.lang.Character> |
toList()
Create a list from this char indexable.
|
static java.lang.String |
toString(CharIndexable indexable)
Create a string representation of the given indexable.
|
default java.lang.String |
toText()
Create a string representation of this char indexable.
|
default <T> Indexable<T> |
view(CharFunction1<? extends T> mapper)
Return a mapped view on this character indexable.
|
static CharIndexable.Base |
viewArray(char... array)
View an int array as an Indexable.
|
static CharIndexable.Base |
viewArray(char[] array,
int start,
int length)
View part of an integer array as an Indexable.
|
static <T> CharIndexable.Base |
viewArray(T[] array,
ToCharFunction<? super T> valueExtract)
View an object array as a char indexable.
|
default BooleanIndexable |
viewAsBoolean(CharPredicate1 convert)
View this indexable as a boolean indexable while using a conversion for
each value.
|
default java.lang.CharSequence |
viewAsCharSequence()
View this char indexable as a standard Java char sequence.
|
static CharIndexable.Base |
viewByIndex(int size,
IntToCharFunction1 producer)
Get a char indexable of a given size which returns elements created by index.
|
default <T> Indexable<T> |
viewChars(CharFunction1<? extends T> mapper)
View this char indexable as an indexable of another content.
|
static CharIndexable.Base |
viewIndexable(Indexable<? extends java.lang.Character> indexable)
View a generic character indexable as char indexable.
|
static CharIndexable.Base |
viewIndexable(Indexable<? extends java.lang.Character> indexable,
char nullFallback)
View a generic character indexable as char indexable.
|
static <T> CharIndexable.Base |
viewIndexable(Indexable<T> indexable,
ToCharFunction<? super T> extractor)
View a generic indexable as a char indexable.
|
static CharIndexable.Base |
viewList(java.util.List<? extends java.lang.Character> list)
View a list as an Indexable.
|
static CharIndexable.Base |
viewList(java.util.List<? extends java.lang.Character> list,
char nullValue)
View a list as an Indexable.
|
static <T> CharIndexable.Base |
viewList(java.util.List<T> list,
ToCharFunction<? super T> extractor)
View a generic list as a char indexable.
|
default CharIndexable |
viewOp(CharOperator1 op)
View this char indexable transformed by an operator.
|
static CharIndexable.Base |
viewString(java.lang.CharSequence text)
View a string as an CharIndexable.
|
default CharIndexable |
withAppendedValue(char value)
Create a view with of this indexable with another value added at the end.
|
default CharIndexable |
withExchangedValueAt(int index,
char value)
Create a view of this char indexable with an exchanged value.
|
default CharIndexable |
withInsertedValueAt(int index,
char value)
Create a view of this char indexable with an inserted value.
|
default CharIndexable |
withRemovedValueAt(int index)
Create a view of this indexable with one value removed.
|
default CharIndexable |
withSwappedValuesAt(int index1,
int index2)
Create a view of this char indexable with two values swapped.
|
asIntIterable, asLongIterable, containsChar, forEachChar, forEachCharFragile
@NotNull static final CharIndexable.Base EMPTY
char get(int index)
index
- index between 0
and size() - 1
default char gyt(int index)
-1
references the last element, -2
its predecessor, and so on.index
- index between -size()
and size() - 1
@NotNull default CharIndexable.Base reverse()
default boolean isEmpty()
true
: there are no elements in this indexablefalse
: this indexable has elements@NotNull default <T> Indexable<T> viewChars(@NotNull CharFunction1<? extends T> mapper)
T
- element type of resulting indexablemapper
- mapper from character to the result element type@NotNull default java.util.PrimitiveIterator.OfInt intIterator()
@NotNull default PrimitiveCharIterator charIterator()
charIterator
in interface PrimitiveCharIterable
char
iterator which iterates over this indexable@NotNull default PrimitiveCharIterator charIterator(int from, int to)
from
- first index of iteratorto
- one past last index of iterator@NotNull default java.util.Iterator<java.lang.Character> iterator()
T
.iterator
in interface java.lang.Iterable<java.lang.Character>
iterator
in interface PrimitiveCharIterable
@NotNull default java.util.ListIterator<java.lang.Character> listIterator()
T
.@NotNull default CharIndexable.Base subSet(int fromIndex, int toIndex)
fromIndex
to toIndex - 1
.fromIndex
- start index of sub settoIndex
- index after last index@NotNull default CharIndexable.Base sybSet(int fromIndex, int toIndex)
fromIndex
- start index of sub settoIndex
- end index of sub set@NotNull default CharIndexable.Base tailSet(int fromIndex)
fromIndex
- index to start with, negative counts from the back@NotNull default CharIndexable.Base headSet(int toIndex)
toIndex
- index one after the end (equal to the length of the returned set),
negative counts from the back@NotNull default <T> Indexable<T> view(@NotNull CharFunction1<? extends T> mapper)
T
- result type pf mappermapper
- mapper@NotNull default java.util.Collection<java.lang.Character> asCollection()
Collection.addAll(Collection)
.@NotNull default java.util.List<java.lang.Character> asList()
Collection.addAll(Collection)
.toList()
@NotNull default java.util.ArrayList<java.lang.Character> toList()
asList()
default void addAllTo(@NotNull java.util.Collection<? super java.lang.Character> collection)
collection
- collection to add to@NotNull default Indexable<java.lang.Character> asIndexable()
Character
values@NotNull default char[] toArray()
@NotNull default java.lang.String makeString()
toString(CharIndexable)
methods reflects
the indexable nature of this object.
Note that in many cases a java.lang.CharSequence
can be used instead
of a string, see viewAsCharSequence()
which is cheaper especially for
large sequences.
@NotNull default CharIndexable viewOp(@NotNull CharOperator1 op)
op
- transforming operator@NotNull default java.lang.CharSequence viewAsCharSequence()
makeString()
default int addToArray(@NotNull char[] array, int pos)
array
- array where the content is addedpos
- position in the array where the content of this indexable is addeddefault int addToArray(@NotNull char[] array, int arrayPos, int index, int length)
array
- array where the content is addedarrayPos
- position in hte array where the content is addedindex
- start index of this indexable which is added firstlength
- number of entries of this indexable added to the array@NotNull default int[] toIntArray()
default int addToArray(@NotNull int[] array, int pos)
array
- array where the content is addedpos
- position in the array where the content of this indexable is addeddefault int addToArray(@NotNull int[] array, int arrayPos, int index, int length)
array
- array where the content is addedarrayPos
- position in hte array where the content is addedindex
- start index of this indexable which is added firstlength
- number of entries of this indexable added to the array@NotNull default java.lang.Iterable<java.lang.Integer> indexes()
intIndexes()
,
Range.indexes(int)
@NotNull default IntIndexable intIndexes()
indexes()
,
IntIndexable.rangeFromSize(int)
@NotNull default IntIndexable.Base asIntIndexable()
default char foldLeft(char initialValue, @NotNull CharOperator2 foldOperation)
initialValue
- start value for the operation, will be returned if this iterable is emptyfoldOperation
- operation applied to each value, will get the accumulated value as its
first and the current element value as its second argumentdefault int nextMatch(int startIndex, @NotNull CharPredicate1 check)
startIndex
- start index for checking, Pythonesque
indexing supportedcheck
- check to perform on elements of this indexable until true
startIndex
for which the check returns true
,
Pythonesque.UNMAPPABLE
if nonedefault int firstMatch(@NotNull CharPredicate1 check)
check
- check to perform on elements of this indexable until true
true
,
-1
if nonedefault int previousMatch(int startIndex, @NotNull CharPredicate1 check)
startIndex
- start index for checking, Pythonesque
indexing supportedcheck
- check to perform on elements of this indexable until true
startIndex
for which the check returns true
,
Pythonesque.UNMAPPABLE
if nonedefault int lastMatch(@NotNull CharPredicate1 check)
check
- check to perform on elements of this indexable until true
true
,
-1
if none@NotNull default CharIndexable frozen()
Often Indexables are used as a view to underlying collections. Although this interface is immutable, the underlying colelction might nevertheless change. This copies the current state of this indexable into an unmodifiable state, and returns an Indexable which is stable in size and will return always the same element for a given index. Beware: it is still possible that any element itself changes when the elements are mutable.
Calling frozen()
again on the returned object will just return
the object itself, so you can safely call this method more than once.
default boolean isOrdered(@NotNull CharOrdering order)
order
- expected orderingtrue
if this indexable is sorted as defined by order
false
if notdefault boolean isOrdered()
See isStrictlyOrdered() for a stricter alternative.
true
if this indexable is sorted naturallyfalse
if notdefault boolean isStrictlyOrdered(@NotNull CharOrdering order)
Prefer isOrdered() to check for natural ordering because it is faster.
order
- expected orderingtrue
if this indexable is sorted as defined by order
false
if notdefault boolean isStrictlyOrdered()
true
if this indexable is sorted naturallyfalse
if notdefault int binarySearch(char value)
natural ascending order
,
i.e. isOrdered(CharOrdering) has to return true
for this ordering.
For efficiency this prerequisite is not checked, but not fulfilling
it will make this method return bogus results. If this indexable is only ordered,
but not strictly ordered
it is not defined
which index is returned when the looked up value appears in a sequence of equal values.value
- value to look up-(insertIndex + 1)
.default int binarySearch(char value, @NotNull CharOrdering order)
order
,
i.e. isOrdered(CharOrdering) has to return true
for this order.
For efficiency this prerequisite is not checked, but not fulfilling
it will make this method return bogus results. If this indexable is only ordered,
but not strictly ordered
it is not defined
which index is returned when the looked up value appears in a sequence of equal values.value
- value to look uporder
- sort order-(insertIndex + 1)
.@NotNull default MutableCharIndexable ordered(@NotNull CharOrdering order)
order
- sort order@NotNull default CharIndexable.Base asBase()
@NotNull default CharIndexable withInsertedValueAt(int index, char value)
ExpandableIntIndexable
if you want to carry out more
stuff like this.
As this creates a view of this indexable, changes to this indexable might result in a disaster when using the returned indexable!
index
- index where the value is inserted before the current index
(not Pythonesque, because it is possible to add an value at index == size()
,
and it is expected that most insertion will happen at 0 or at the end)value
- inserted value at that indexwithAppendedValue(char)
@NotNull default CharIndexable withAppendedValue(char value)
value
- value to addwithInsertedValueAt(int, char)
@NotNull default CharIndexable withExchangedValueAt(int index, char value)
ExpandableIntIndexable
if you want to carry out more
stuff like this.
As this creates a view of this indexable, changes to this indexable could lead to unexpected results when using the returned indexable!
index
- index where the element is exchanged, hiding the current element.
(Pythonesque)value
- exchanged value at that index@NotNull default CharIndexable withRemovedValueAt(int index)
index
- index of the removed value (Pythonesque)@NotNull default CharIndexable withSwappedValuesAt(int index1, int index2)
index1
- index of the first value (Pythonesque)index2
- index of the second value (Pythonesque)@NotNull default CharIndexable rotated(int steps)
steps
will rotate left, i.e. any index will be accessed
as if the number of steps is added before extraction (modulo length).
E.g. a rotation of -1
will return the last element when index 0
is
requested, and the first element for index 1
.steps
- steps to rotatefrozen()
to create an indexable which no longer depends on this one@NotNull default BooleanIndexable viewAsBoolean(@NotNull CharPredicate1 convert)
Use frozen()
on the result to decouple it from this indexable.
convert
- converter applied to each value of this indexable before
it is returned from the resulting indexable@NotNull default java.lang.String toText()
()
this method will always work,
toString()
will only do what is expected when the implementation
extends CharIndexable.Base
(which takes care of toString()
or provides the expected implementation itself.
Use asCharSequence()
to create a non-copy view of this char indexable,
and use subSet(int, int)
, headSet(int)
, or tailSet(int)
first to create texts from parts of this indexable.
asCharSequence()
@NotNull default java.lang.CharSequence asCharSequence()
The main advantage over Object.toString()
is that this method will
always work even if the implementation of this interface is neither
extending CharIndexable.Base
nor takes care of creating a string
itself, and that this method does not copy the chars around which can
be preferable for large sizes.
@NotNull static CharIndexable.Base viewList(@NotNull java.util.List<? extends java.lang.Character> list)
list
- list used as a base, required to have only non-null elements@NotNull static CharIndexable.Base viewList(@NotNull java.util.List<? extends java.lang.Character> list, char nullValue)
list
- list used as a base, required to have only non-null elementsnullValue
- value returned for null
elements in the list@NotNull static <T> CharIndexable.Base viewList(@NotNull java.util.List<T> list, @NotNull ToCharFunction<? super T> extractor)
T
- element type of listlist
- generic listextractor
- extractor function which extracts a char from the elements of list
list
@NotNull static CharIndexable.Base viewIndexable(@NotNull Indexable<? extends java.lang.Character> indexable)
indexable
- generic indexable with Character elements, required to have only non-null elements@NotNull static CharIndexable.Base viewIndexable(@NotNull Indexable<? extends java.lang.Character> indexable, char nullFallback)
indexable
- generic indexable with Character elementsnullFallback
- fallback for null
elements@NotNull static <T> CharIndexable.Base viewIndexable(@NotNull Indexable<T> indexable, @NotNull ToCharFunction<? super T> extractor)
T
- element type of indexableindexable
- generic indexableextractor
- extractor function which extracts a char from the elements of indexable
indexable
@NotNull static CharIndexable.Base viewString(@NotNull java.lang.CharSequence text)
text
- text string@NotNull static CharIndexable.Base viewArray(@NotNull char... array)
viewArray(char[], int, int)
.array
- array used as base@NotNull static CharIndexable.Base viewArray(@NotNull char[] array, int start, int length)
array
- array used as basestart
- index of first byte to use from the arraylength
- number of elements to use from the array@NotNull static <T> CharIndexable.Base viewArray(@NotNull T[] array, @NotNull ToCharFunction<? super T> valueExtract)
T
- array element typearray
- viewed arrayvalueExtract
- converter from array elements to the char values of this indexable@NotNull static CharIndexable.Base singleton(char value)
value
- single value@NotNull static CharIndexable.Base init(int size, char value)
size
- size of the returned indexablevalue
- value returned for each elementsize
with always the same element@NotNull static CharIndexable.Base viewByIndex(int size, @NotNull IntToCharFunction1 producer)
producer
when an element is requested.
Use frozen()
on the result to create an indexable which avoids this.size
- size of the returned indexableproducer
- producer which is called with an index and expected to return the associated valueproducer
@NotNull static CharIndexable initByIndex(int size, @NotNull IntToCharFunction1 producer)
size
- size of the returned indexableproducer
- producer which is called with an index and expected to return the associated valueproducer
viewByIndex(int, IntToCharFunction1)
@NotNull static CharIndexable.Base emptyIndexable()
@NotNull static java.lang.String toString(@NotNull CharIndexable indexable)
indexable
- indexablemakeString()
static int hash(@NotNull CharIndexable indexable)
indexable
- indexable for which the hash code is requiredstatic boolean equal(@NotNull CharIndexable indexable1, @NotNull CharIndexable indexable2)
indexable1
- first indexableindexable2
- second indexabletrue
if both indexables contain the same values in the same sequencefalse
if sizes or values differstatic boolean equal(@NotNull CharIndexable indexable1, @Nullable java.lang.Object indexable2)
Object.equals(Object)
method.indexable1
- first indexableindexable2
- object expected to be an indexable itselftrue
if both indexables contain the same values in the same sequencefalse
if the second object is not an indexable, of if sizes or values differstatic int compare(@NotNull CharIndexable indexable1, @NotNull CharIndexable indexable2)
indexable1
- first indexableindexable2
- second indexable< 0>
if indexable1 < indxable2
,
0
if indexable1 == indexable2
, or
> 0
if indexable1 > indexable2