Class IndexRangeNegative
Object
IndexRangeNegative
Specifying a sub-range of indices, tolerating also negative indices.
Negative indices, loop around the end of the range.
- Author:
- Owen Feehan
-
Constructor Summary
ConstructorsConstructorDescriptionIndexRangeNegative
(int startIndex, int endIndex) Create with a start and end index. -
Method Summary
Modifier and TypeMethodDescriptionint
correctedEndIndex
(int size) The end-index, maybe corrected so that it is no longer negative.int
correctedStartIndex
(int size) The start-index, maybe corrected so that it is no longer negative.boolean
<T> List
<T> extract
(int numberElements, IntFunction<T> extractIndex) Extracts a subset of elements from a collection according to the specified index/range.<T> List
<T> Extracts a subset of elements from a list according to the specified index/range.int
The index if the last element to use in the subsetting range, inclusive.int
The index if the first element to use in the subsetting range, zero-indexed.int
hashCode()
toString()
-
Constructor Details
-
IndexRangeNegative
Create with a start and end index.- Parameters:
startIndex
- the start-index (which can be negative).endIndex
- the end-index (which can be negative).- Throws:
OperationFailedException
- if the indices are incorrectly ordered.
-
-
Method Details
-
extract
Extracts a subset of elements from a list according to the specified index/range.- Type Parameters:
T
- type of element in the list- Parameters:
list
- the list to extract a subset from- Returns:
- a newly created list that contains a subset of elements from
list
corresponding to the index/range. - Throws:
OperationFailedException
- if an index lies outside the range(-listSize, listSize)
orstartIndex>=endIndex
.
-
extract
public <T> List<T> extract(int numberElements, IntFunction<T> extractIndex) throws OperationFailedException Extracts a subset of elements from a collection according to the specified index/range.Collection is any generic structure, with a fixed number of elements, that can be accessed via
extractIndex
.- Type Parameters:
T
- type of element in the list- Parameters:
numberElements
- the number of elements in the collection.extractIndex
- extracts an element at a particular index from the collection. Zero-indexed.- Returns:
- a newly created list that contains a subset of elements from the collection corresponding to the index/range.
- Throws:
OperationFailedException
- if an index lies outside the range(-numberElements, numberElements)
orstartIndex>=endIndex
.
-
correctedStartIndex
The start-index, maybe corrected so that it is no longer negative.- Parameters:
size
- the total number of elements- Returns:
- the maybe-corrected start-index
- Throws:
OperationFailedException
- ifstartIndex
is invalid for a list of sizesize
.
-
correctedEndIndex
The end-index, maybe corrected so that it is no longer negative.- Parameters:
size
- the total number of elements- Returns:
- the maybe-corrected start-index
- Throws:
OperationFailedException
- ifendIndex
is invalid for a list of sizesize
.
-
getStartIndex
public int getStartIndex()The index if the first element to use in the subsetting range, zero-indexed.This can be negative, in which case it goes backwards from the end of the string e.g. index -2 is the same as
size(elements) - 2
. -
getEndIndex
public int getEndIndex()The index if the last element to use in the subsetting range, inclusive.This can be negative, in which case it goes backwards from the end of the string e.g. index -2 is the same as
size(elements) - 2
.Therefore, -1 will use the final element as the last element.
-
equals
-
hashCode
public int hashCode() -
toString
-