Class IndexRangeNegative

Object
IndexRangeNegative

public final class IndexRangeNegative extends Object
Specifying a sub-range of indices, tolerating also negative indices.

Negative indices, loop around the end of the range.

Author:
Owen Feehan
  • Constructor Summary

    Constructors
    Constructor
    Description
    IndexRangeNegative(int startIndex, int endIndex)
    Create with a start and end index.
  • Method Summary

    Modifier and Type
    Method
    Description
    int
    The end-index, maybe corrected so that it is no longer negative.
    int
    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>
    extract(List<T> list)
    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
     
     

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • IndexRangeNegative

      public IndexRangeNegative(int startIndex, int endIndex) throws OperationFailedException
      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

      public <T> List<T> extract(List<T> list) throws OperationFailedException
      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) or startIndex>=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) or startIndex>=endIndex.
    • correctedStartIndex

      public int correctedStartIndex(int size) throws OperationFailedException
      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 - if startIndex is invalid for a list of size size.
    • correctedEndIndex

      public int correctedEndIndex(int size) throws OperationFailedException
      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 - if endIndex is invalid for a list of size size.
    • 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

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object