Package ru.ispras.castle.ast
Class IntegerRange
- java.lang.Object
-
- ru.ispras.castle.ast.IntegerRange
-
public final class IntegerRange extends java.lang.Object
This class represents a non-empty integer range (interval).
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
IntegerRange.RangePointType
This enumeration contains types of range bounds.
-
Constructor Summary
Constructors Constructor Description IntegerRange(java.math.BigInteger min, java.math.BigInteger max)
Constructs a range with the given lower (min
) and upper (max
) bounds.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
border(IntegerRange rhs)
Checks whether this range borders on the given one (rhs
).int
compareTo(IntegerRange range)
Compares this object to the specified object.boolean
contains(java.math.BigInteger value)
Checks whether this range contains the given point.boolean
contains(IntegerRange rhs)
Checks whether this range contains (as a subset) the given one (rhs
).static java.util.List<IntegerRange>
divide(java.util.Collection<IntegerRange> ranges)
Transforms the collection of ranges to the list of disjoint ranges.boolean
equals(java.lang.Object obj)
java.math.BigInteger
getMax()
Returns the upper bound of the range.java.math.BigInteger
getMin()
Returns the lower bound of the range.int
hashCode()
IntegerRange
intersect(IntegerRange range)
Returns an intersection range for this one and the specified one.java.util.List<IntegerRange>
minus(IntegerRange rhs)
Returns the list of ranges representing the difference between this range and the given one.boolean
overlaps(IntegerRange rhs)
Checks whether this range overlaps with the given one (rhs
).static java.util.Collection<IntegerRange>
select(java.util.Collection<IntegerRange> ranges, IntegerRange bounds)
Selects all ranges from the the given collection that are within the given bounds.IntegerRange
shift(int value)
Returns the shifted variant of this range ([min + value, max + value]
).java.math.BigInteger
size()
Returns the size of the range.java.lang.String
toString()
-
-
-
Constructor Detail
-
IntegerRange
public IntegerRange(java.math.BigInteger min, java.math.BigInteger max)
Constructs a range with the given lower (min
) and upper (max
) bounds.- Parameters:
min
- the lower bound of the range.max
- the upper bound of the range.- Throws:
java.lang.IllegalArgumentException
- ifmin
ormax
is null.java.lang.IllegalArgumentException
- if (min > max
).
-
-
Method Detail
-
divide
public static java.util.List<IntegerRange> divide(java.util.Collection<IntegerRange> ranges)
Transforms the collection of ranges to the list of disjoint ranges.- Parameters:
ranges
- the collection of ranges.- Returns:
- the list of disjoint ranges.
- Throws:
java.lang.IllegalArgumentException
- ifranges
is null.
-
select
public static java.util.Collection<IntegerRange> select(java.util.Collection<IntegerRange> ranges, IntegerRange bounds)
Selects all ranges from the the given collection that are within the given bounds.- Parameters:
ranges
- the collection of ranges.bounds
- the bounds.- Returns:
- the ranges included into the bounds.
- Throws:
java.lang.IllegalArgumentException
- if some of the parameters are null.
-
getMin
public java.math.BigInteger getMin()
Returns the lower bound of the range.- Returns:
- the lower bound of the range.
-
getMax
public java.math.BigInteger getMax()
Returns the upper bound of the range.- Returns:
- the upper bound of the range.
-
size
public java.math.BigInteger size()
Returns the size of the range.- Returns:
- the size of the range.
-
overlaps
public boolean overlaps(IntegerRange rhs)
Checks whether this range overlaps with the given one (rhs
).- Parameters:
rhs
- the range to be compared with this one.- Returns:
true
if this range overlaps with the given one;false
otherwise.- Throws:
java.lang.IllegalArgumentException
- ifrhs
is null.
-
border
public boolean border(IntegerRange rhs)
Checks whether this range borders on the given one (rhs
).When one integer range borders one another one, it means, that a third non-empty integer range can't be inserted between them.
- Parameters:
rhs
- The range to be compared with this one.- Returns:
true
if this range borders on the given one,false
otherwise.- Throws:
java.lang.IllegalArgumentException
- when argument isnull
.
-
contains
public boolean contains(IntegerRange rhs)
Checks whether this range contains (as a subset) the given one (rhs
).- Parameters:
rhs
- the range to be compared with this one.- Returns:
true
if this range contains the given one;false
otherwise.- Throws:
java.lang.IllegalArgumentException
- ifrhs
is null.
-
contains
public boolean contains(java.math.BigInteger value)
Checks whether this range contains the given point.- Parameters:
value
- the point.- Returns:
true
if this range contains the given point;false
otherwise.- Throws:
java.lang.IllegalArgumentException
- ifvalue
is null.
-
shift
public IntegerRange shift(int value)
Returns the shifted variant of this range ([min + value, max + value]
).- Parameters:
value
- the shift value.- Returns:
- the shifted range.
-
minus
public java.util.List<IntegerRange> minus(IntegerRange rhs)
Returns the list of ranges representing the difference between this range and the given one. If the ranges are not overlapping, the list consists of this range; otherwise it may include up to 2 ranges.- Parameters:
rhs
- the range to be subtracted from this one.- Returns:
- the difference.
- Throws:
java.lang.IllegalArgumentException
- ifrhs
is null.
-
intersect
public IntegerRange intersect(IntegerRange range)
Returns an intersection range for this one and the specified one.No modifications of existing objects are made.
- Parameters:
range
- The object the intersection to which is created.- Returns:
- The intersection range for this one and the specified one if there is an overlapping
between them,
null
otherwise.
-
compareTo
public int compareTo(IntegerRange range)
Compares this object to the specified object.- Parameters:
range
- object to which this object is compared- Returns:
-1
when max value of this object is less than the specified object's min value,1
when min value of this object is greater than the specified object's max value,0
when both conditions from above are not met
-
equals
public boolean equals(java.lang.Object obj)
- Overrides:
equals
in classjava.lang.Object
-
hashCode
public int hashCode()
- Overrides:
hashCode
in classjava.lang.Object
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
-