Class Range2


  • public class Range2
    extends Object
    A class that represents a range of values on the integer number line. A range has two properties: a minimum value and a maximum value. Range objects always ensure that the following class invariant is true:

    minimum <= maximum

    • Constructor Summary

      Constructors 
      Constructor Description
      Range2()
      Initialize this range with minimum value -1 and a maximum value 1.
      Range2​(int min, int max)
      Initialize this range with the specified minimum and maximum values.
    • Constructor Detail

      • Range2

        public Range2()
        Initialize this range with minimum value -1 and a maximum value 1.
      • Range2

        public Range2​(int min,
                      int max)
        Initialize this range with the specified minimum and maximum values.
        Parameters:
        min - the minimum value of the range
        max - the maximum value of the range
        Throws:
        IllegalArgumentException - if min is greater than max
    • Method Detail

      • getMinimum

        public int getMinimum()
        Returns the minimum value of the range
        Returns:
        the minimum value of the range
      • getMaximum

        public int getMaximum()
        Returns the maximum value of the range
        Returns:
        the maximum value of the range
      • setMinimum

        public Range2 setMinimum​(int min)
        Sets the minimum value of this range to the specified value.
        Parameters:
        min - the new minimum value for this range
        Returns:
        a reference to this range
        Throws:
        IllegalArgumentException - if min is greater than the current maximum value of this range
      • setMaximum

        public Range2 setMaximum​(int max)
        Sets the maximum value of this range to the specified value.
        Parameters:
        max - the new maximum value for this range
        Returns:
        a reference to this range
        Throws:
        IllegalArgumentException - if max is less than the current minimum value of this range
      • slice

        public Range2 slice​(int value)
        Removes all values greater than value from this range. For example, if this range is the range [10, 20] then slice(15) changes this range to be the range [10, 15] (i.e., it removes the values 16 through 20 from this range).
        Parameters:
        value - the greatest value to keep in this range
        Returns:
        a reference to this range
        Throws:
        IllegalArgumentException - if value is not contained in this range
      • contains

        public boolean contains​(int value)
        Checks if value is inside this range. A value is considered inside this range if the value is greater than or equal to the minimum value of this range and value is less than or equal to the maximum value of this range.
        Parameters:
        value - the value to check
        Returns:
        true if value is inside this range, false otherwise
      • contains

        public boolean contains​(Range2 other)
        Checks if another range is entirely inside of this range. For example, if this range is the range [10, 20] and the other range is the range [14, 16] then the other range is inside of this range.

        If the other range is equal to this range (has the same minimum and maximum value) then this method returns true.

        Parameters:
        other - the range to check
        Returns:
        true if other is inside this range, false otherwise
      • removeAll

        public Range2 removeAll​(Range2 other)
        Removes all of the values of another range from this range. For example, if this range is the range [10, 20] and the other range is [15, 25] then removeAll will change this range to [10, 14] (i.e., it removes the values 15 through 20 from this range). If the other range does not overlap this range then no values are removed from this range.

        Only ranges that partially overlap this range or do not overlap this range at all can be passed as an argument to this method.

        Parameters:
        other - the other range containing values to remove from this range
        Returns:
        a reference to this range
        Throws:
        IllegalArgumentException - if this range contains the other range
        IllegalArgumentException - if the other range contains this range
      • toString

        public String toString()
        Returns the string representation of a range. The string contains the minimum and maximum values separated by a comma and space all inside of a pair of square brackets. For example, the string

        [2, 25]

        represents the range whose minimum value is 2 and whose maximum value is 25

        Overrides:
        toString in class Object
        Returns:
        a string representation of the range