public final class IndexRange
extends java.lang.Object
It represents an integer index range as the pair values:
from
to
This class is intended to specify a valid index range in arrays or ordered collections.
All instances are constraint so that neither from
nor to
can
be negative nor from
can be larger than to
.
You can use isValidFor(length)
to verify that a range instance represents a valid
range for an 0-based indexed object with length
elements.
Constructor and Description |
---|
IndexRange(int fromIndex,
int toIndex)
Creates a new range given its
from and to indices. |
Modifier and Type | Method and Description |
---|---|
boolean |
equals(java.lang.Object other) |
java.util.List<java.lang.Integer> |
filter(java.util.function.IntPredicate predicate)
Find the elements of this range for which an int -> boolean predicate is true
|
void |
forEach(java.util.function.IntConsumer lambda)
Iterate through all indexes in the range in ascending order to be processed by the
provided
integer consumer lambda function. |
int |
getEnd() |
int |
getStart() |
int |
hashCode() |
boolean |
isValidLength(int length)
Checks whether this range is valid for a collection or array of a given size.
|
double[] |
mapToDouble(java.util.function.IntToDoubleFunction lambda)
Apply an int -> double function to this range, producing a double[]
|
int[] |
mapToInteger(java.util.function.IntUnaryOperator lambda)
Apply an int -> int function to this range, producing an int[]
|
void |
shift(int shift) |
void |
shiftEnd(int shift) |
void |
shiftEndLeft(int shift) |
void |
shiftLeft(int shift) |
void |
shiftStart(int shift) |
void |
shiftStartLeft(int shift) |
int |
size()
Returns number indexes expanded by this range.
|
double |
sum(java.util.function.IntToDoubleFunction lambda)
Sums the values of an int -> double function applied to this range
|
java.lang.String |
toString() |
public IndexRange(int fromIndex, int toIndex)
from
and to
indices.fromIndex
- the from
index value.toIndex
- the to
index value.java.lang.IllegalArgumentException
- if fromIndex
is larger than toIndex
or either is
negative.public void shift(int shift)
public void shiftLeft(int shift)
public void shiftStart(int shift)
public void shiftStartLeft(int shift)
public void shiftEnd(int shift)
public void shiftEndLeft(int shift)
public int getStart()
public int getEnd()
public boolean isValidLength(int length)
It assume that 0 is the first valid index for target indexed object which is true for Java Arrays and mainstream collections.
If the input length is less than 0, thus incorrect, this method is guaranteed to return
false
. No exception is thrown.
length
- the targeted collection or array length.true
if this range is valid for that length
, false
otherwise.public int size()
public void forEach(java.util.function.IntConsumer lambda)
integer consumer
lambda function.
Exceptions thrown by the execution of the index consumer lambda
will be propagated to the caller immediately thus stopping early and preventing
further indexes to be processed.
lambda
- the index consumer lambda.java.lang.IllegalArgumentException
- if lambda
is null
.java.lang.RuntimeException
- if thrown by lambda
for some index.java.lang.Error
- if thrown by lambda
for some index.public double[] mapToDouble(java.util.function.IntToDoubleFunction lambda)
lambda
- the int -> double functionpublic double sum(java.util.function.IntToDoubleFunction lambda)
lambda
- the int -> double functionpublic int[] mapToInteger(java.util.function.IntUnaryOperator lambda)
lambda
- the int -> int functionpublic java.util.List<java.lang.Integer> filter(java.util.function.IntPredicate predicate)
predicate
- the int -> boolean predicatepublic boolean equals(java.lang.Object other)
equals
in class java.lang.Object
public int hashCode()
hashCode
in class java.lang.Object
public java.lang.String toString()
toString
in class java.lang.Object