Class IntRange

java.lang.Object
net.solarnetwork.util.IntRange
All Implemented Interfaces:
Serializable, Comparable<IntRange>

public final class IntRange extends Object implements Serializable, Comparable<IntRange>
An immutable integer range with min/max values.

Inspired and adapted from PCJ's bak.pcj.set.IntRange class.

Since:
1.58
Version:
1.0
Author:
matt
See Also:
  • Constructor Summary

    Constructors
    Constructor
    Description
    IntRange(int min, int max)
    Constructor.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    Test if this range is adjacent to (but not intersecting) a given range.
    boolean
    Test if this range could be merged with another range.
    int
    Compares this object with the specified object for order.
    boolean
    contains(int value)
    Test if a value is within this range, inclusive.
    boolean
    containsAll(int min, int max)
    Test if another range is completely within this range, inclusive.
    boolean
    Test if another range is completely within this range, inclusive.
    boolean
     
    int
    Get the maximum value.
    int
    Get the minimum value.
    int
     
    boolean
    Test if this range intersects with a given range.
    boolean
    Test if this range represents a singleton value, where the minimum and maximum values in the range are equal.
    int
    Get the number of integer values between min and max, inclusive.
    Merge this range with a given range, returning the merged range.
    static IntRange
    rangeOf(int value)
    Create a singleton range (where the minimum and maximum are the same).
    static IntRange
    rangeOf(int min, int max)
    Create a range.
     

    Methods inherited from class java.lang.Object

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

    • IntRange

      public IntRange(int min, int max)
      Constructor.

      Note that if min > max then getMin() will return max and getMax() will return min. That is, the minimum and maximum values passed to this constructor will be compared before storing in this class so that getMin() always returns the actual minimum value.

      Parameters:
      min - the minimum value
      max - the maximum value
  • Method Details

    • rangeOf

      public static IntRange rangeOf(int value)
      Create a singleton range (where the minimum and maximum are the same).
      Parameters:
      value - the singleton value
      Returns:
      the new range
    • rangeOf

      public static IntRange rangeOf(int min, int max)
      Create a range.
      Parameters:
      min - the minimum value
      max - the maximum value
      Returns:
      the new range
    • getMin

      public int getMin()
      Get the minimum value.
      Returns:
      the minimum
    • getMax

      public int getMax()
      Get the maximum value.
      Returns:
      the maximum
    • length

      public int length()
      Get the number of integer values between min and max, inclusive.
      Returns:
      the inclusive length between min and max
    • isSingleton

      public boolean isSingleton()
      Test if this range represents a singleton value, where the minimum and maximum values in the range are equal.
      Returns:
      true if min == max
    • contains

      public boolean contains(int value)
      Test if a value is within this range, inclusive.
      Parameters:
      value - the value to test
      Returns:
      true if min <= value <= max
    • containsAll

      public boolean containsAll(int min, int max)
      Test if another range is completely within this range, inclusive.
      Parameters:
      min - the minimum of the range to test
      max - the maximum of the range to test
      Returns:
      true if this.min <= min <= max <= this.max
    • containsAll

      public boolean containsAll(IntRange o)
      Test if another range is completely within this range, inclusive.
      Parameters:
      o - the range to test
      Returns:
      true if this.min <= o.min <= o.max <= this.max
    • intersects

      public boolean intersects(IntRange o)
      Test if this range intersects with a given range.
      Parameters:
      o - the range to compare to this range
      Returns:
      true if this range intersects (overlaps) with the given range
      Throws:
      NullPointerException - if o is null
    • adjacentTo

      public boolean adjacentTo(IntRange o)
      Test if this range is adjacent to (but not intersecting) a given range.
      Parameters:
      o - the range to compare to this range
      Returns:
      true if this range is adjacent to the given range
      Throws:
      NullPointerException - if o is null
    • canMergeWith

      public boolean canMergeWith(IntRange o)
      Test if this range could be merged with another range.

      Two ranges can be merged if they are either adjacent to or intersect with each other.

      Parameters:
      o - the range to test
      Returns:
      true if this range is either adjacent to or intersects with the given range
    • mergeWith

      public IntRange mergeWith(IntRange o)
      Merge this range with a given range, returning the merged range.
      Parameters:
      o - the range to merge with this range
      Returns:
      the new merged range
      Throws:
      IllegalArgumentException - if the this range cannot be merged with the given range
    • compareTo

      public int compareTo(IntRange o)
      Compares this object with the specified object for order.

      This implementation only compares the min values of each range.

      Specified by:
      compareTo in interface Comparable<IntRange>
    • hashCode

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

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

      public String toString()
      Overrides:
      toString in class Object