com.jstatcom.model
Class NumberRange
- java.lang.Object
-
- com.jstatcom.model.NumberRange
-
public final class NumberRange extends java.lang.ObjectThis class specifies an interval defined by two numbers and a type and provides methods to validate numbers and otherNumberRangeinstances against it.The class is immutable by design, see (Joshua Bloch, "Effective Java", Item 13).
-
-
Field Summary
Fields Modifier and Type Field and Description doublelowerBoundThe lower bound of this range.NumberRangeTypesrangeTypeThe type of this range, which is eitherOPEN, CLOSED, LEFT_OPEN or RIGHT_OPEN.doubleupperBoundThe upper bound of this range.
-
Constructor Summary
Constructors Constructor and Description NumberRange()Creates an open range with(-Infinity,+Infinity).NumberRange(double lowerBoundNew, double upperBoundNew, NumberRangeTypes rangeType)Creates a range according to the given bounds and the type.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method and Description doubledefaultNumber()Finds a number that is valid for this range.NumberRangedefaultNumberRange()Finds a number range that is enclosed by this range.java.lang.Stringencloses(double number)Tests whether a given number is enclosed in this range and returns a formatted message if not.java.lang.Stringencloses(double number, int precision, java.text.NumberFormat formatter)Tests whether a given number is enclosed in this range and returns a formatted message if not.java.lang.Stringencloses(NumberRange numberRange)Tests whether a given range is completely enclosed within this range and returns a formatted message if not.booleanequals(java.lang.Object o)Provides logical equals check.java.lang.StringformattedString(java.text.NumberFormat formatter)Gets a string representation of this range with a special formatter.inthashCode()OverrideshashCode, because equals was overwritten.doublelowerBound()Gets the lower bound for this range.java.lang.StringtoString()Gets string representation of this range.NumberRangeTypestype()Gets the interval type for this range.doubleupperBound()Gets the upper bound for this range.static NumberRangevalueOf(java.lang.String exp)Static initialized that creates aNumberRangefrom a string representation.
-
-
-
Field Detail
-
lowerBound
public final double lowerBound
The lower bound of this range.
-
upperBound
public final double upperBound
The upper bound of this range.
-
rangeType
public final NumberRangeTypes rangeType
The type of this range, which is eitherOPEN, CLOSED, LEFT_OPEN or RIGHT_OPEN.
-
-
Constructor Detail
-
NumberRange
public NumberRange()
Creates an open range with(-Infinity,+Infinity).
-
NumberRange
public NumberRange(double lowerBoundNew, double upperBoundNew, NumberRangeTypes rangeType)Creates a range according to the given bounds and the type.- Parameters:
lowerBoundNew- the lower bound of the intervalupperBoundNew- the upper bound of the intervalrangeType- the interval type to be created- Throws:
java.lang.IllegalArgumentException- if:- lowerBound > upperBound
- lowerBound == upperBound and rangeType is not closed
- lowerBound == -Infinity and rangeType is not left open
- upperBound == +Infinity and rangeType is not right open
- lowerBound == +Infinity
- upperBound == -Infinity
- upperBound or lowerBound is equal to NaN
- rangeType == null
-
-
Method Detail
-
defaultNumber
public double defaultNumber()
Finds a number that is valid for this range.- Returns:
- a valid number within this range
-
defaultNumberRange
public NumberRange defaultNumberRange()
Finds a number range that is enclosed by this range. The range type of the returned range is alwaysCLOSED.- Returns:
- a valid number within this range
-
encloses
public java.lang.String encloses(double number)
Tests whether a given number is enclosed in this range and returns a formatted message if not. If the number is either POS_INF, NEG_INF or NaN, it is never enclosed in the range.- Parameters:
number- the value to check- Returns:
- null if
numberis contained, message if not
-
encloses
public java.lang.String encloses(double number, int precision, java.text.NumberFormat formatter)Tests whether a given number is enclosed in this range and returns a formatted message if not. This version also takes the given precision into account and rounds thenumberbefore checking. If the number is either POS_INF, NEG_INF or NaN, it is never enclosed in the range.- Parameters:
number- the value to checkprecision- the number of digits right to the decimal point to take into account for checkingformatter- a formatter fornumberin the possible error message, can benull- Returns:
- null if
numberis contained, message if not - Throws:
java.lang.IllegalArgumentException- ifif (precision < 0)
-
encloses
public java.lang.String encloses(NumberRange numberRange)
Tests whether a given range is completely enclosed within this range and returns a formatted message if not. A range cannot be enclosed, if one of its bounds is POS_INF or NEG_INF.- Parameters:
numberRange- the range to check- Returns:
- null if
numberRangeis contained, message if not
-
equals
public boolean equals(java.lang.Object o)
Provides logical equals check.- Overrides:
equalsin classjava.lang.Object- Parameters:
o- another range to compare with- Returns:
trueif lower bound, upper bound and range type are all equal
-
formattedString
public java.lang.String formattedString(java.text.NumberFormat formatter)
Gets a string representation of this range with a special formatter. Ifformatter == null, this is identical totoString.- Parameters:
formatter- a number formatter for the bounds, can benull- Returns:
- a string in mathematical notation
-
lowerBound
public double lowerBound()
Gets the lower bound for this range.- Returns:
- lower bound
-
type
public NumberRangeTypes type()
Gets the interval type for this range.- Returns:
- range type
-
upperBound
public double upperBound()
Gets the upper bound for this range.- Returns:
- upper bound
-
hashCode
public int hashCode()
OverrideshashCode, because equals was overwritten. The algorithm was taken from (Joshua Bloch, "Effective Java", Item 8).- Overrides:
hashCodein classjava.lang.Object- Returns:
- the hash code
-
toString
public java.lang.String toString()
Gets string representation of this range.- Overrides:
toStringin classjava.lang.Object- Returns:
- a string in mathematical notation
-
valueOf
public static NumberRange valueOf(java.lang.String exp)
Static initialized that creates aNumberRangefrom a string representation. Valid expressions are:- [1,100]
- (-Infinity,100.3]
- (-Infinity,+Infinity)
- (-12E-2,23]
- [100,100]
- Parameters:
exp- string containing a valid representation for a number range- Returns:
- a number range instance created from
exp - Throws:
java.lang.IllegalArgumentException- ifexpcould not be parsed to a valid number range- See Also:
NumberRange(double lowerBoundNew,double upperBoundNew,NumberRangeTypes rangeType)
-
-
DMelt 3.0 © DataMelt by jWork.ORG