com.jstatcom.model
Class NumberRange

java.lang.Object
  extended by com.jstatcom.model.NumberRange

public final class NumberRange
extends java.lang.Object

This class specifies an interval defined by two numbers and a type and provides methods to validate numbers and other NumberRange instances against it.

The class is immutable by design, see (Joshua Bloch, "Effective Java", Item 13).

Author:
Markus Kraetzig

Field Summary
 double lowerBound
          The lower bound of this range.
 NumberRangeTypes rangeType
          The type of this range, which is either OPEN, CLOSED, LEFT_OPEN or RIGHT_OPEN.
 double upperBound
          The upper bound of this range.
 
Constructor Summary
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
 double defaultNumber()
          Finds a number that is valid for this range.
 NumberRange defaultNumberRange()
          Finds a number range that is enclosed by this range.
 java.lang.String encloses(double number)
          Tests whether a given number is enclosed in this range and returns a formatted message if not.
 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.
 java.lang.String encloses(NumberRange numberRange)
          Tests whether a given range is completely enclosed within this range and returns a formatted message if not.
 boolean equals(java.lang.Object o)
          Provides logical equals check.
 java.lang.String formattedString(java.text.NumberFormat formatter)
          Gets a string representation of this range with a special formatter.
 int hashCode()
          Overrides hashCode, because equals was overwritten.
 double lowerBound()
          Gets the lower bound for this range.
 java.lang.String toString()
          Gets string representation of this range.
 NumberRangeTypes type()
          Gets the interval type for this range.
 double upperBound()
          Gets the upper bound for this range.
static NumberRange valueOf(java.lang.String exp)
          Static initialized that creates a NumberRange from a string representation.
 
Methods inherited from class java.lang.Object
getClass, notify, notifyAll, wait, wait, wait
 

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 either OPEN, 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 interval
upperBoundNew - the upper bound of the interval
rangeType - 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 always CLOSED.

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 number is 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 the number before checking. If the number is either POS_INF, NEG_INF or NaN, it is never enclosed in the range.

Parameters:
number - the value to check
precision - the number of digits right to the decimal point to take into account for checking
formatter - a formatter for number in the possible error message, can be null
Returns:
null if number is contained, message if not
Throws:
java.lang.IllegalArgumentException - if if (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 numberRange is contained, message if not

equals

public boolean equals(java.lang.Object o)
Provides logical equals check.

Overrides:
equals in class java.lang.Object
Parameters:
o - another range to compare with
Returns:
true if 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. If formatter == null, this is identical to toString.

Parameters:
formatter - a number formatter for the bounds, can be null
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()
Overrides hashCode, because equals was overwritten. The algorithm was taken from (Joshua Bloch, "Effective Java", Item 8).

Overrides:
hashCode in class java.lang.Object
Returns:
the hash code

toString

public java.lang.String toString()
Gets string representation of this range.

Overrides:
toString in class java.lang.Object
Returns:
a string in mathematical notation

valueOf

public static NumberRange valueOf(java.lang.String exp)
Static initialized that creates a NumberRange from 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 - if exp could not be parsed to a valid number range
See Also:
NumberRange(double lowerBoundNew,double upperBoundNew,NumberRangeTypes rangeType)