maspack.util
Class IntegerInterval

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

public class IntegerInterval
extends NumericInterval

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
IntegerInterval()
          Creates a IntRange which accepts any integer value.
IntegerInterval(IntegerInterval rng)
          Creates a new IntRange which is a copy of an existing one.
IntegerInterval(int lower, int upper)
          Creates a new IntRange that accepts any value in the closed interval [lower, upper].
IntegerInterval(java.lang.String str)
          Creates a new IntRange from a specification string.
 
Method Summary
 DoubleInterval clone()
           
 void scan(ReaderTokenizer rtok, java.lang.Object ref)
          Scans this element from a ReaderTokenizer.
 void set(int lower, int upper)
          Sets this IntRange so that it accepts any value in the closed interval [lower, upper].
 void set(NumericInterval rng)
          Sets this IntRange from the value of an existing NumericInterval.
 void setLowerBound(int lower)
          Sets the lower bound for the interval of this IntRange.
 void setUpperBound(int upper)
          Sets the upper bound for the interval of this IntRange.
 java.lang.String toString()
           
 java.lang.Object validate(int[] values, boolean clip, StringHolder errMsg)
          Validates an array of integers by checking that its elements lie within this range interval.
 void write(java.io.PrintWriter pw, NumberFormat fmt, java.lang.Object ref)
          Writes a text description of this element to a PrintWriter.
 
Methods inherited from class maspack.util.NumericInterval
canClipToRange, clipToRange, contains, equals, equals, getLowerBound, getRange, getUpperBound, intersect, isBounded, isClosed, isEmpty, isLowerBoundClosed, isTypeCompatible, isUpperBoundClosed, isValid, makeValid, makeValid, merge, parse, validate, validate, withinRange, withinRange
 
Methods inherited from class maspack.util.RangeBase
isWritable, setError
 
Methods inherited from class java.lang.Object
getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

IntegerInterval

public IntegerInterval()
Creates a IntRange which accepts any integer value.


IntegerInterval

public IntegerInterval(int lower,
                       int upper)
Creates a new IntRange that accepts any value in the closed interval [lower, upper].

Parameters:
lower - interval lower bound
upper - interval upper bound

IntegerInterval

public IntegerInterval(IntegerInterval rng)
Creates a new IntRange which is a copy of an existing one.

Parameters:
rng - range to copy

IntegerInterval

public IntegerInterval(java.lang.String str)
                throws java.lang.IllegalArgumentException
Creates a new IntRange from a specification string. For the string syntax, see scan.

Parameters:
str - specification string for this IntRange
Throws:
java.lang.IllegalArgumentException - if the string is not in the correct format.
Method Detail

setLowerBound

public void setLowerBound(int lower)
Sets the lower bound for the interval of this IntRange.

Parameters:
lower - new lower interval bound

setUpperBound

public void setUpperBound(int upper)
Sets the upper bound for the interval of this IntRange.

Parameters:
upper - new upper interval bound

set

public void set(int lower,
                int upper)
Sets this IntRange so that it accepts any value in the closed interval [lower, upper].

Parameters:
lower - interval lower bound
upper - interval upper bound

set

public void set(NumericInterval rng)
Sets this IntRange from the value of an existing NumericInterval.

Parameters:
rng - numeric range to copy

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

scan

public void scan(ReaderTokenizer rtok,
                 java.lang.Object ref)
          throws java.io.IOException
Scans this element from a ReaderTokenizer. The input should consist of five tokens, which specifiy the interval as follows:

Specified by:
scan in interface Scannable
Overrides:
scan in class RangeBase
Parameters:
rtok - Tokenizer from which to scan the element
ref - reference object (not used, may be null)
Throws:
java.io.IOException - if an I/O or formatting error occured

write

public void write(java.io.PrintWriter pw,
                  NumberFormat fmt,
                  java.lang.Object ref)
           throws java.io.IOException
Writes a text description of this element to a PrintWriter. The text description should be compatable with scan and complete enough to allow full reconstruction of the element.

Specified by:
write in interface Scannable
Overrides:
write in class RangeBase
Parameters:
pw - stream for writing the element
fmt - numeric formating information
ref - optional reference object which can be used for producing references to other objects
Throws:
java.io.IOException - if an I/O error occured

validate

public java.lang.Object validate(int[] values,
                                 boolean clip,
                                 StringHolder errMsg)
Validates an array of integers by checking that its elements lie within this range interval. If all elements are within range, the array is returned unchanged. Otherwise, the method returns either a new clipped array (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:
values - array to validate
clip - if true, clip the array to the range
errMsg - if non-null, is used to return an error message if one or more elements are out of range
Returns:
either the original array, a clipped version of it, or Range.IllegalValue

clone

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