Class IntRange

java.lang.Object
org.jjazz.utilities.api.IntRange

public class IntRange extends Object
A basic integer (zero or positive) interval.

This is an immutable class.

  • Field Details

    • EMPTY_RANGE

      public static final IntRange EMPTY_RANGE
      The special shared instance for the empty range.
    • from

      public final int from
    • to

      public final int to
  • Constructor Details

    • IntRange

      public IntRange(int from, int to)
      A range representing [from; to].
      Parameters:
      from - Must be >= 0
      to - Must be >= from
  • Method Details

    • isEmpty

      public boolean isEmpty()
    • size

      public int size()
      Get the size of the range.
      Returns:
      (to - from + 1), or 0 for the EMPTY_RANGE.
    • contains

      public boolean contains(int x)
      Check if value is within the range.
      Parameters:
      x -
      Returns:
    • contains

      public boolean contains(IntRange r)
      Check if specified range is contained in this range.
      Parameters:
      r -
      Returns:
    • isAdjacent

      public boolean isAdjacent(IntRange r)
      Test if r is adjacent to this range, e.g. [4;6] and [7;12] are adjacent.

      If ranges intersect, there are not adjacent. If one of the range is empty, return false.

      Parameters:
      r -
      Returns:
    • getUnion

      public IntRange getUnion(IntRange r)
      Get a new range made from the lowest and highest bounds from this object and r.

      If one of the range is empty, return the empty range.

      Parameters:
      r -
      Returns:
    • getIntersection

      public IntRange getIntersection(IntRange r)
      Parameters:
      r -
      Returns:
      Can return the EMPTY_RANGE if no intersection.
    • isIntersecting

      public boolean isIntersecting(IntRange r)
      Check if specified range intersects with this range.

      Note: [2;4] and [4;7] intersects.

      Parameters:
      r -
      Returns:
    • getCenter

      public float getCenter()
      The center of the range.
      Returns:
      (to-from)/2 + from
    • getTransformed

      public IntRange getTransformed(int fromOffset, int toOffset)
      Get a new range with bounds modified.

      If this object is the the empty range, just return the empty range.

      Parameters:
      fromOffset -
      toOffset -
      Returns:
    • getTransformed

      public IntRange getTransformed(int offset)
    • toString

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

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

      public boolean equals(Object obj)
      Overrides:
      equals in class Object