Class IntRange

java.lang.Object
org.jjazz.utilities.api.IntRange
All Implemented Interfaces:
Iterable<Integer>

public class IntRange extends Object implements Iterable<Integer>
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

    • ofX

      public static IntRange ofX(Rectangle r)
      Create an IntRange from the first and last x position of the rectangle.
      Parameters:
      r -
      Returns:
      An empty range if r.x is < 0 or r.width <= 0
    • ofY

      public static IntRange ofY(Rectangle r)
      Create an IntRange from the first and last x position of the rectangle.
      Parameters:
      r -
      Returns:
      An empty range if r.y is < 0 or r.height <= 0
    • merge

      public static List<IntRange> merge(List<IntRange> rhythmSectionRanges)
      Merge adjacent IntRanges from a list.
      Parameters:
      rhythmSectionRanges -
      Returns:
    • isEmpty

      public boolean isEmpty()
    • stream

      public IntStream stream()
    • 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:
    • getUnion

      public IntRange getUnion(int x)
      Extend the current range to include x.

      If this range is empty, return the empty range.

      Parameters:
      x -
      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
      Throws:
      IllegalStateException - If range is empty
    • clamp

      public int clamp(int value)
      Clamps the value to fit in this range.
      Parameters:
      value -
      Returns:
    • 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
    • iterator

      public Iterator<Integer> iterator()
      Specified by:
      iterator in interface Iterable<Integer>