aima.core.util.math.Interval Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of aima-core Show documentation
Show all versions of aima-core Show documentation
AIMA-Java Core Algorithms from the book Artificial Intelligence a Modern Approach 3rd Ed.
package aima.core.util.math;
/**
* Basic supports for Intervals.
*
* @see Interval
*
* @author Ciaran O'Reilly
* @author Mike Stampone
*/
public class Interval {
private Comparable lower = null;
private boolean lowerInclusive = true;
private Comparable upper = null;
private boolean upperInclusive = true;
public Interval() {
}
/**
* Constructs a closed interval from the two specified end points.
*
* @param lower
* the lower end point of the interval
* @param upper
* the upper end point of the interval
*/
public Interval(Comparable lower, Comparable upper) {
setLower(lower);
setUpper(upper);
}
/**
* Constructs an interval from the two specified end points.
*
* @param lower
* the lower end point of the interval
* @param lowerInclusive
* wether or not the lower end of the interval is inclusive of
* its value.
* @param upper
* the upper end point of the interval
* @param upperInclusive
* whether or not the upper end of the interval is inclusive of
* its value.
*/
public Interval(Comparable lower, boolean lowerInclusive,
Comparable upper, boolean upperInclusive) {
setLower(lower);
setLowerInclusive(lowerInclusive);
setUpper(upper);
setUpperInclusive(upperInclusive);
}
/**
* Returns true
if the specified object is between the end
* points of this interval.
*
* @return true
if the specified value is between the end
* points of this interval.
*/
public boolean isInInterval(C o) {
if (null == lower || null == upper) {
return false;
}
boolean in = true;
if (isLowerInclusive()) {
in = lower.compareTo(o) <= 0;
} else {
in = lower.compareTo(o) < 0;
}
if (in) {
if (isUpperInclusive()) {
in = upper.compareTo(o) >= 0;
} else {
in = upper.compareTo(o) > 0;
}
}
return in;
}
/**
* Returns true
if this interval is lower inclusive.
*
* @return true
if this interval is lower inclusive.
*/
public boolean isLowerInclusive() {
return lowerInclusive;
}
/**
* Returns true
if this interval is not lower inclusive.
*
* @return true
if this interval is not lower inclusive.
*/
public boolean isLowerExclusive() {
return !lowerInclusive;
}
/**
* Sets the interval to lower inclusive or lower exclusive.
*
* @param inclusive
* true
represents lower inclusive and
* false
represents lower exclusive.
*/
public void setLowerInclusive(boolean inclusive) {
this.lowerInclusive = inclusive;
}
/**
* Sets the interval to lower exclusive or lower inclusive.
*
* @param exclusive
* true
represents lower exclusive and
* false
represents lower inclusive.
*/
public void setLowerExclusive(boolean exclusive) {
this.lowerInclusive = !exclusive;
}
/**
* Returns the lower end point of the interval.
*
* @return the lower end point of the interval.
*/
public Comparable getLower() {
return lower;
}
/**
* Sets the lower end point of the interval.
*
* @param lower
* the lower end point of the interval
*/
public void setLower(Comparable lower) {
this.lower = lower;
}
/**
* Returns true
if this interval is upper inclusive.
*
* @return true
if this interval is upper inclusive.
*/
public boolean isUpperInclusive() {
return upperInclusive;
}
/**
* Returns true
if this interval is upper exclusive.
*
* @return true
if this interval is upper exclusive.
*/
public boolean isUpperExclusive() {
return !upperInclusive;
}
/**
* Sets the interval to upper inclusive or upper exclusive.
*
* @param inclusive
* true
represents upper inclusive and
* false
represents upper exclusive.
*/
public void setUpperInclusive(boolean inclusive) {
this.upperInclusive = inclusive;
}
/**
* Sets the interval to upper exclusive or upper inclusive.
*
* @param exclusive
* true
represents upper exclusive and
* false
represents upper inclusive.
*/
public void setUpperExclusive(boolean exclusive) {
this.upperInclusive = !exclusive;
}
/**
* Returns the upper end point of the interval.
*
* @return the upper end point of the interval.
*/
public Comparable getUpper() {
return upper;
}
/**
* Sets the upper end point of the interval.
*
* @param upper
* the upper end point of the interval.
*/
public void setUpper(Comparable upper) {
this.upper = upper;
}
public String toString() {
return (lowerInclusive ? "[" : "(") + lower + ", " + upper
+ (upperInclusive ? "]" : ")");
}
}