jlibs.core.util
Class Range

java.lang.Object
  extended by jlibs.core.util.Range

public class Range
extends Object

This class represents integer range.

Author:
Santhosh Kumar T

Field Summary
 int max
           
 int min
           
 
Constructor Summary
Range(int min, int max)
          Note that both min and max are inclusive
 
Method Summary
 boolean after(int x)
          returns true if this range is after given number
 boolean before(int x)
          return true if this range is before given number
 boolean contains(int x)
          returns true if this range contains given number
 boolean equals(Object obj)
           
 int hashCode()
           
static List<Range> intersection(List<Range> list1, List<Range> list2)
           
 Range intersection(Range that)
          return the portion of range that is common to this range and given range.
If there is nothing common, then null is returned.
static List<Range> minus(List<Range> list1, List<Range> list2)
           
 Range[] minus(Range that)
          returns the portion(s) of this range that are not present in given range.
 boolean[] position(Range that)
          tells the position this range with respect to given range.
static boolean same(List<Range> list1, List<Range> list2)
           
 Range[] split(Range that)
          this method splits this range into 3 regions with respect to given range
 String toString()
           
static List<Range> union(List<Range> ranges)
           
 Range[] union(Range that)
          returns union of this range with given range.
if both ranges are adjacent/intersecting to each other, then the returned array will have only one range.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

min

public final int min

max

public final int max
Constructor Detail

Range

public Range(int min,
             int max)
Note that both min and max are inclusive

Method Detail

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

before

public boolean before(int x)
return true if this range is before given number
 ------------   o
 


after

public boolean after(int x)
returns true if this range is after given number
   o   --------------
 


contains

public boolean contains(int x)
returns true if this range contains given number
      ------o--------
 


position

public boolean[] position(Range that)
tells the position this range with respect to given range.


the return value is boolean array of size 3.

1st boolean ==> true if some portion of this range is before given range
2nd boolean ==> true if this range intersects with given range
3rd boolean ==> true if some portion of this range is after given range
ideally, you can remebers these boolean as { before, inside, after }


split

public Range[] split(Range that)
this method splits this range into 3 regions with respect to given range


the return value is Range array of size 3.

1st range ==> the portion of this range that is before given range
2nd range ==> the portion of range that is common to this range and given range
3rd range ==> the portion of this range that is after given range.
Note that the values in returned array can be null, if there is no range satifying the requirement.


intersection

public Range intersection(Range that)
return the portion of range that is common to this range and given range.
If there is nothing common, then null is returned.


union

public Range[] union(Range that)
returns union of this range with given range.
if both ranges are adjacent/intersecting to each other, then the returned array will have only one range. otherwise the returned array will have two ranges in sorted order.


minus

public Range[] minus(Range that)
returns the portion(s) of this range that are not present in given range. maximum size of returned array is 2. and the array is sorted.


union

public static List<Range> union(List<Range> ranges)

intersection

public static List<Range> intersection(List<Range> list1,
                                       List<Range> list2)

minus

public static List<Range> minus(List<Range> list1,
                                List<Range> list2)

same

public static boolean same(List<Range> list1,
                           List<Range> list2)


Copyright © 2018. All rights reserved.