maspack.util
Class NumericInterval

java.lang.Object
  extended by maspack.util.RangeBase
      extended by maspack.util.NumericInterval
All Implemented Interfaces:
java.lang.Cloneable, Clonable, Range, Scannable
Direct Known Subclasses:
DoubleInterval, IntegerInterval

public abstract class NumericInterval
extends RangeBase

A Range object which inspects a number to make sure it lies within a prescibed interval.


Field Summary
 
Fields inherited from interface maspack.util.Range
IllegalValue
 
Constructor Summary
NumericInterval()
           
 
Method Summary
 boolean canClipToRange(double num)
          Returns true if it is possible to clip a number to this range.
 double clipToRange(double num)
          Clips a number to lie within the interval specified by this NumericRange.
 NumericInterval clone()
           
 boolean contains(NumericInterval rng)
          Returns true if the NumericRange r is a subset of this range.
 boolean equals(NumericInterval rng)
          Returns true if a specified NumericRange is equivalent in value to this one.
 boolean equals(java.lang.Object obj)
          Returns true if a specified object is a NumericRange equivalent in value to this one.
 double getLowerBound()
          Returns the lower bound for the interval of this NumericRange.
 double getRange()
          Returns the upper bound minus the lower bound of this NumericRange
 double getUpperBound()
          Returns the upper bound for the interval of this NumericRange.
 void intersect(Range r)
          Intersects the set of valid values of this Range with those of another.
 boolean isBounded()
          Returns true if this range is bounded; i.e., neither the upper nor the lower bounds are infinite.
 boolean isClosed()
          Returns true if this interval is closed, which is true if both the lower and upper bounds are closed.
 boolean isEmpty()
          Returns true if this range is empty - i.e., if there are no valid values.
 boolean isLowerBoundClosed()
          Returns true if the lower bound for the interval of this NumericRange is closed.
 boolean isTypeCompatible(java.lang.Object obj)
          Returns true if a specified object is an instance of Number.
 boolean isUpperBoundClosed()
          Returns true if the upper bound for the interval of this NumericRange is closed.
 boolean isValid(java.lang.Object obj, StringHolder errMsg)
          Returns true if the specified object is valid for this Range, and false otherwise.
 double makeValid(double val)
           
 java.lang.Object makeValid(java.lang.Object obj)
          Projects an object to lie within the range allowed by this Range, if possible.
 void merge(NumericInterval range)
          Merges this NumericRange with another, updating the bounds for this range to enclose both values.
 void parse(java.lang.String str)
          Sets the value of this NumericRange from a string.
 java.lang.Object validate(java.lang.Number value, boolean clip, StringHolder errMsg)
          Validates a numeric object by checking that it lies within this range interval.
 java.lang.Object validate(Vector vec, boolean clip, StringHolder errMsg)
          Validates a vector by checking that its elements lie within this range interval.
 boolean withinRange(double num)
          Returns true if the specified number lies within the interval specified by this NumericRange.
 boolean withinRange(java.lang.Number num)
          Returns true if the value of a specified Number object lies within the interval specified by this NumericRange.
 
Methods inherited from class maspack.util.RangeBase
isWritable, scan, setError, write
 
Methods inherited from class java.lang.Object
getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

NumericInterval

public NumericInterval()
Method Detail

isEmpty

public boolean isEmpty()
Returns true if this range is empty - i.e., if there are no valid values. This will typically result from the intersection of two non-overlapping ranges.

Specified by:
isEmpty in interface Range
Overrides:
isEmpty in class RangeBase
Returns:
true if this range has no valid values.

contains

public boolean contains(NumericInterval rng)
Returns true if the NumericRange r is a subset of this range.

Parameters:
rng - Range to check
Returns:
true if r is a subset of this range

intersect

public void intersect(Range r)
Description copied from class: RangeBase
Intersects the set of valid values of this Range with those of another. If the resulting intersection is null, then Range.isEmpty() should subsequently return true.

Specified by:
intersect in interface Range
Overrides:
intersect in class RangeBase
Parameters:
r - range to intersect with.

isClosed

public boolean isClosed()
Returns true if this interval is closed, which is true if both the lower and upper bounds are closed.

Returns:
lower interval bound

getLowerBound

public double getLowerBound()
Returns the lower bound for the interval of this NumericRange.

Returns:
lower interval bound

isLowerBoundClosed

public boolean isLowerBoundClosed()
Returns true if the lower bound for the interval of this NumericRange is closed.

Returns:
true if lower interval bound is closed

getUpperBound

public double getUpperBound()
Returns the upper bound for the interval of this NumericRange.

Returns:
upper interval bound

isUpperBoundClosed

public boolean isUpperBoundClosed()
Returns true if the upper bound for the interval of this NumericRange is closed.

Returns:
true if upper interval bound is closed

isBounded

public boolean isBounded()
Returns true if this range is bounded; i.e., neither the upper nor the lower bounds are infinite.

Returns:
true if the range is bounded

getRange

public double getRange()
Returns the upper bound minus the lower bound of this NumericRange

Returns:
range interval for this range

isValid

public boolean isValid(java.lang.Object obj,
                       StringHolder errMsg)
Description copied from class: RangeBase
Returns true if the specified object is valid for this Range, and false otherwise. If the object is not valid, and errMsg is not null, then errMsg.value should be set to a message describing why the object is not valid.

Specified by:
isValid in interface Range
Specified by:
isValid in class RangeBase
Parameters:
obj - Object to be testes
errMsg - Optional handle for storing error message
Returns:
true if the object is valid

makeValid

public double makeValid(double val)

makeValid

public java.lang.Object makeValid(java.lang.Object obj)
Projects an object to lie within the range allowed by this Range, if possible. If obj is already within the range, then obj should be returned unchanged. Otherwise, if it can be projected to the range, then a new (projected) object should be returned. Otherwise, if it cannot be projected, Range.IllegalValue should be returned.

In particular, projectToRange(obj) != obj should be a valid indication that the obj is not within range.

Specified by:
makeValid in interface Range
Overrides:
makeValid in class RangeBase
Parameters:
obj - object to be projected
Returns:
original obj if within range, or a projected object, or null.

withinRange

public boolean withinRange(java.lang.Number num)
Returns true if the value of a specified Number object lies within the interval specified by this NumericRange.

Parameters:
num - Number to be tested
Returns:
returns true if the Number's value is within range

withinRange

public boolean withinRange(double num)
Returns true if the specified number lies within the interval specified by this NumericRange.

Parameters:
num - number to be tested
Returns:
returns true if the number is within range

clipToRange

public double clipToRange(double num)
Clips a number to lie within the interval specified by this NumericRange. For purposes of clipping, the interval is assumed to be closed.

Parameters:
num - number to be clipped
Returns:
clipped version of the number

canClipToRange

public boolean canClipToRange(double num)
Returns true if it is possible to clip a number to this range. This will be the case if the number is in range, or if the bound that it is outside of is closed.


isTypeCompatible

public boolean isTypeCompatible(java.lang.Object obj)
Returns true if a specified object is an instance of Number.

Parameters:
obj - object to test
Returns:
true if the object is a Number

merge

public void merge(NumericInterval range)
Merges this NumericRange with another, updating the bounds for this range to enclose both values.

Parameters:
range - range to merge with this one

equals

public boolean equals(java.lang.Object obj)
Returns true if a specified object is a NumericRange equivalent in value to this one.

Overrides:
equals in class java.lang.Object
Parameters:
obj - object to compare with
Returns:
true if the object is equivalent to this NumericRange

equals

public boolean equals(NumericInterval rng)
Returns true if a specified NumericRange is equivalent in value to this one.

Parameters:
rng - numeric range to compare with
Returns:
true if rng is equivalent to this NumericRange

parse

public void parse(java.lang.String str)
Sets the value of this NumericRange from a string. The string format should match that specified for scan.

Parameters:
str - string specifying the value of this NumericRange

validate

public java.lang.Object validate(java.lang.Number value,
                                 boolean clip,
                                 StringHolder errMsg)
Validates a numeric object by checking that it lies within this range interval. For purposes of this method, the interval is treated as closed. If the object's number is within range, the object is returned unchanged. Otherwise, the method returns either a new clipped version of the number (if clip is true), or the special value Range.IllegalValue. In the latter two cases, an error message will also be returned if the variable errMsg is non-null.

Parameters:
value - numeric object to validate
clip - if true, clip the number to the range if possib;e
errMsg - if non-null, is used to return an error message if the number is out of range
Returns:
either the original number, a clipped version of it, or Range.IllegalValue

validate

public java.lang.Object validate(Vector vec,
                                 boolean clip,
                                 StringHolder errMsg)
Validates a vector by checking that its elements lie within this range interval. For purposes of this method, the interval is treated as closed. If all elements are within range, the vector is returned unchanged. Otherwise, the method returns either a new clipped vector (if clip is true), or the special value Range.IllegalValue. In the latter two cases, an error message will also be returned if the variable errMsg is non-null.

Parameters:
vec - vector to validate
clip - if true, clip the vector to the range if possible
errMsg - if non-null, is used to return an error message if one or more elements are out of range
Returns:
either the original vector, a clipped version of it, or Range.IllegalValue

clone

public NumericInterval clone()
Specified by:
clone in interface Clonable
Overrides:
clone in class RangeBase