Class Range

java.lang.Object
io.github.darvil.utils.Range
All Implemented Interfaces:
Iterable<Integer>

public class Range extends Object implements Iterable<Integer>
A range class to contain a start and end value, or a single value. Both non-negative.
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static class 
     
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final Range
    A range between 0 and infinity.
    static final Range
    A range between 1 and infinity.
    static final Range
    A range of 0.
    static final Range
    A range between 0 and 1.
    static final Range
    A range of 1.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    contains(int value, boolean startInclusive, boolean endInclusive)
    Returns true if the given value is in the range.
    boolean
    containsExclusive(int value)
    Returns true if the given value is in the range, exclusive.
    boolean
    containsInclusive(int value)
    Returns true if the given value is in the range, inclusive.
    int
    end()
    Returns the end value, or Integer.MAX_VALUE if the range is infinite.
    static @NotNull Range.RangeBuilder
    from(int start)
    Creates a new range builder with the given start value.
    @NotNull String
    getMessage(@NotNull String kind)
    Returns a string representation of the range, such as "from 3 to 5 times", or "3 times".
    @NotNull String
    Returns a string representation of the range, such as "{3, 5}" or "{3}".
    boolean
    Returns true if the end value is infinity.
    boolean
    Returns true if this is representing a single value.
    boolean
    Returns true if the range is 0.
    @NotNull Iterator<Integer>
    Returns an iterator that iterates over the range.
    @NotNull Iterator<Integer>
    iterator(boolean startInclusive, boolean endInclusive)
    Returns an iterator that iterates over the range.
    static @NotNull Range
    of(int value)
    Creates a new single-value range with the given value.
    @NotNull Range
    offset(int offset)
    Offsets both the start and end values by the given offset.
    int
    Returns the start value.
    int[]
    Returns an array of all the values in the range, inclusive.
    int[]
    toArray(boolean startInclusive, boolean endInclusive)
    Returns an array of all the values in the range.
     

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait

    Methods inherited from interface java.lang.Iterable

    forEach, spliterator
  • Field Details

    • ANY

      public static final Range ANY
      A range between 0 and infinity.
    • AT_LEAST_ONE

      public static final Range AT_LEAST_ONE
      A range between 1 and infinity.
    • NONE

      public static final Range NONE
      A range of 0.
    • ONE

      public static final Range ONE
      A range of 1.
    • NONE_OR_ONE

      public static final Range NONE_OR_ONE
      A range between 0 and 1.
  • Method Details

    • from

      @NotNull public static @NotNull Range.RangeBuilder from(int start)
      Creates a new range builder with the given start value.
    • of

      @NotNull public static @NotNull Range of(int value)
      Creates a new single-value range with the given value.
    • isSimple

      public boolean isSimple()
      Returns true if this is representing a single value.
    • isZero

      public boolean isZero()
      Returns true if the range is 0.
    • isInfinite

      public boolean isInfinite()
      Returns true if the end value is infinity.
    • start

      public int start()
      Returns the start value.
    • end

      public int end()
      Returns the end value, or Integer.MAX_VALUE if the range is infinite.
    • getMessage

      @NotNull public @NotNull String getMessage(@NotNull @NotNull String kind)
      Returns a string representation of the range, such as "from 3 to 5 times", or "3 times".
      Parameters:
      kind - The kind of thing the range is for, such as "time" or "argument"
      Returns:
      The string representation
    • getRepresentation

      @NotNull public @NotNull String getRepresentation()
      Returns a string representation of the range, such as "{3, 5}" or "{3}". If the end value is Short.MAX_VALUE, it will be represented as "...".
      Returns:
      The string representation
    • contains

      public boolean contains(int value, boolean startInclusive, boolean endInclusive)
      Returns true if the given value is in the range.
      Parameters:
      value - The value to check
      startInclusive - Whether the start value is inclusive
      endInclusive - Whether the end value is inclusive
      Returns:
      true if the value is in the range
    • containsInclusive

      public boolean containsInclusive(int value)
      Returns true if the given value is in the range, inclusive.
      Parameters:
      value - The value to check
      Returns:
      true if the value is in the range
    • containsExclusive

      public boolean containsExclusive(int value)
      Returns true if the given value is in the range, exclusive.
      Parameters:
      value - The value to check
      Returns:
      true if the value is in the range
    • offset

      @NotNull public @NotNull Range offset(int offset)
      Offsets both the start and end values by the given offset. The range will be infinite if it was infinite before.
      Parameters:
      offset - The offset to apply
      Returns:
      The new range
    • iterator

      @NotNull public @NotNull Iterator<Integer> iterator()
      Returns an iterator that iterates over the range. Both the start and end values are inclusive.
      Specified by:
      iterator in interface Iterable<Integer>
      Returns:
      The iterator
      See Also:
    • iterator

      @NotNull public @NotNull Iterator<Integer> iterator(boolean startInclusive, boolean endInclusive)
      Returns an iterator that iterates over the range.
      Parameters:
      startInclusive - Whether the start value is inclusive
      endInclusive - Whether the end value is inclusive
      Returns:
      The iterator
    • toArray

      public int[] toArray()
      Returns an array of all the values in the range, inclusive.

      If the range is infinite, an UnsupportedOperationException will be thrown.

      Returns:
      The array
      Throws:
      UnsupportedOperationException - if the range is infinite
      See Also:
    • toArray

      public int[] toArray(boolean startInclusive, boolean endInclusive)
      Returns an array of all the values in the range.

      If the range is infinite, an UnsupportedOperationException will be thrown.

      The array will always contain at least one element.
      Parameters:
      startInclusive - Whether the start value is inclusive
      endInclusive - Whether the end value is inclusive
      Returns:
      The array
      Throws:
      UnsupportedOperationException - if the range is infinite
    • toString

      public String toString()
      Overrides:
      toString in class Object