Class SortedRangeList

  • All Implemented Interfaces:
    java.lang.Iterable<Range>

    public class SortedRangeList
    extends java.lang.Object
    implements java.lang.Iterable<Range>
    Provides a list of integer ranges that are maintained in sorted order. When a range is added any ranges that overlap or are adjacent to one another will coalesce into a single range.
    • Constructor Summary

      Constructors 
      Constructor Description
      SortedRangeList()
      Creates a new empty sorted range list.
      SortedRangeList​(SortedRangeList list)
      Creates a new sorted range list with ranges equivalent to those in the specified list.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void addRange​(int min, int max)
      Adds the range from min to max to this sorted range list.
      void clear()  
      boolean contains​(int value)
      Returns true if the value is contained in any ranges within this list.
      boolean contains​(int min, int max)
      Returns true if a single range contains all the values from min to max.
      int getMax()
      Returns the maximum int value in this sorted range list.
      int getMin()
      Returns the minimum int value in this sorted range list.
      int getNumRanges()
      Returns the number of ranges in the list.
      long getNumValues()
      Gets the total number of int values in this range.
      Range getRange​(int index)
      Gets the nth range in this list as indicated by the value of index.
      int getRangeIndex​(int value)
      Gets the range index for the range containing the specified value.
      java.util.Iterator<Range> getRanges()
      Returns an iterator over all the ranges in this list.
      java.util.Iterator<Range> getRanges​(boolean forward)
      Returns an iterator over all the ranges in this list that iterates in the direction specified.
      SortedRangeList intersect​(SortedRangeList other)
      Creates a new SortedRangeList that is the intersection of this range list and the other range list specified.
      boolean intersects​(int min, int max)
      Returns true if the range from min to max intersects (overlaps) any ranges in this sorted range list.
      boolean isEmpty()
      Returns true if the range list is empty.
      java.util.Iterator<Range> iterator()  
      void remove​(SortedRangeList other)
      Removes all the ranges that are in the specified other list from this list.
      void removeRange​(int min, int max)
      Removes the indicated range of values from the list.
      java.lang.String toString()  
      • 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
    • Constructor Detail

      • SortedRangeList

        public SortedRangeList()
        Creates a new empty sorted range list.
      • SortedRangeList

        public SortedRangeList​(SortedRangeList list)
        Creates a new sorted range list with ranges equivalent to those in the specified list.
        Parameters:
        list - the sorted range list to make an equivalent copy of.
    • Method Detail

      • addRange

        public void addRange​(int min,
                             int max)
        Adds the range from min to max to this sorted range list. If the range is adjacent to or overlaps any other existing ranges, then those ranges will coalesce.
        Parameters:
        min - the range minimum
        max - the range maximum (inclusive)
      • getRanges

        public java.util.Iterator<Range> getRanges()
        Returns an iterator over all the ranges in this list.
      • getRanges

        public java.util.Iterator<Range> getRanges​(boolean forward)
        Returns an iterator over all the ranges in this list that iterates in the direction specified.
        Parameters:
        forward - true indicates to iterate forward from minimum to maximum range. false indicates backward iteration form maximum to minimum.
      • getMin

        public int getMin()
                   throws java.util.NoSuchElementException
        Returns the minimum int value in this sorted range list.
        Throws:
        java.util.NoSuchElementException - if the list is empty.
      • getMax

        public int getMax()
                   throws java.util.NoSuchElementException
        Returns the maximum int value in this sorted range list.
        Throws:
        java.util.NoSuchElementException - if the list is empty.
      • getNumRanges

        public int getNumRanges()
        Returns the number of ranges in the list.
      • removeRange

        public void removeRange​(int min,
                                int max)
        Removes the indicated range of values from the list. This will remove any ranges or portion of ranges that overlap the indicated range.
        Parameters:
        min - the minimum value for the range to remove.
        max - the maximum value for the range to remove.
      • contains

        public boolean contains​(int value)
        Returns true if the value is contained in any ranges within this list.
        Parameters:
        value - the value to check for.
      • contains

        public boolean contains​(int min,
                                int max)
        Returns true if a single range contains all the values from min to max.
        Parameters:
        min - the minimum value
        max - the maximum value
      • getRangeIndex

        public int getRangeIndex​(int value)
        Gets the range index for the range containing the specified value.
        Parameters:
        value - the value to look for.
        Returns:
        the range index or a negative value if the range list doesn't contain the value.
      • getRange

        public Range getRange​(int index)
        Gets the nth range in this list as indicated by the value of index.
        Parameters:
        index - value indicating which nth range to get.
        Returns:
        the range or null if there is no such range in this list.
      • getNumValues

        public long getNumValues()
        Gets the total number of int values in this range.
        Returns:
        the number of int values.
      • intersects

        public boolean intersects​(int min,
                                  int max)
        Returns true if the range from min to max intersects (overlaps) any ranges in this sorted range list.
        Parameters:
        min - the range minimum value.
        max - the range maximum value
      • isEmpty

        public boolean isEmpty()
        Returns true if the range list is empty.
      • remove

        public void remove​(SortedRangeList other)
        Removes all the ranges that are in the specified other list from this list.
        Parameters:
        other - the other sorted range list.
      • intersect

        public SortedRangeList intersect​(SortedRangeList other)
        Creates a new SortedRangeList that is the intersection of this range list and the other range list specified.
        Parameters:
        other - the other range list
        Returns:
        the new SortedRangeList representing the intersection.
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • iterator

        public java.util.Iterator<Range> iterator()
        Specified by:
        iterator in interface java.lang.Iterable<Range>
      • clear

        public void clear()