You need to define a range of acceptable values for a variable, and test to see if that variable is within those boundaries.
Use an implementation of Range,
an interface that defines a simple numerical range. There are a number
of different implementations for different types: NumberRange , DoubleRange
, FloatRange , IntRange , and LongRange
. The following example demonstrates the use of DoubleRange to verify that a variable is
within a valid range. A DoubleRange
is created with minimum and maximum values, and a value is tested by
DoubleRange using a method named
containsDouble( ) :
import org.apache.commons.lang.math.DoubleRange;
import org.apache.commons.lang.math.Range;
Range safeSpeed = new DoubleRange( 0.0, 65.0 );
double currentSpeed = getCurrentSpeed( );
if( !safeSpeed.containsDouble( currentSpeed ) ) {
System.out.println( "Warning, current speed is unsafe." );
}Additionally, one can also test to see if another Range is contained within a Range, or if a Range overlaps another Range. The following example demonstrates the
use of containsRange( ) to determine if a Range is entirely contained within another
Range:
import org.apache.commons.lang.math.Range;
import org.apache.commons.lang.math.IntRange;
import org.apache.commons.lang.math.NumberUtils;
double recordHigh = getRecordHigh( );
double recordLow = getRecordLow( );
IntRange recordRange = new IntRange( recordLow, recordHigh );
int todayTemp = getTodaysMaxTemp( );
IntRange daysRange = new IntRange( NumberUtils.min( todayTemp ),
NumberUtils.max( todayTemp ) );
if( !recordRange.containsRange( todayTemp ) ) {
System.out.println( "Today is a record temperature day!" );
}The previous code creates a Range, recordRange, from the record high and low
temperatures. It then creates daysRange, which is a Range of the current day's high and low
temperatures. If dayRange is not
entirely contained within the recordRange, then the current day contains a
record temperature and recordRange.containsRange(daysRange) will
return false. containsRange( ) returns true if every value in the containing range
occurs in the contained range, and overlapsRange( ) returns true if two Range objects share any common value. NumberUtils is used to retrieve the maximum
and minimum values from the todayTemp
array.
In another example, a Range
object is used to ascertain the state of an element from a temperature
measurement. Elemental Gold (Au) melts at 1337.33 Kelvin and boils at
3129.15 Kelvin. The following code is used to read the temperature from
a thermometer and print the current state of the element:
import org.apache.commons.lang.math.Range;
import org.apache.commons.lang.math.DoubleRange;
double melting = 1337.33;
double boiling = 3129.15
// State ranges for element Au
Object[] stateRanges =
new Object[][]{{"solid" , new DoubleRange( 0.0, melting )},
{"liquid", new DoubleRange( melting, boiling )},
{"gas", new DoubleRange( boiling,Double.INFINITY) };
// Read measurement from thermometer
double temp = themometer.getReading( );
String state = "unknown";
// Test the state
for( int i = 0; i < stateRanges.length; i++ ) {
DoubleRange stateRange = (DoubleRange) stateRanges[i][1];
if( stateRange.contains( temp ) ) {
state = (String) stateRanges[i][0];
}
}
System.out.println( "The substance is in a " + state + " state." );
// If the temperature is a temperate 293 K, this line would print
// "The Gold is in a solid state."The ranges in this example overlap; the solid range ends at melting, and the liquid range begins at melting. Because each Range is tested in a defined order, each
Range object in this example is
lower- and upper-bound inclusive. If the temp variable has the same value as melting, the program will indicate solid
state, and if the temp variable has
the same value as boiling, this
program will signify the liquid
state.
For more information about downloading Commons Lang, see Recipe 1.1.
