maspack.util
Class RangeBase

java.lang.Object
  extended by maspack.util.RangeBase
All Implemented Interfaces:
java.lang.Cloneable, Clonable, Range, Scannable
Direct Known Subclasses:
EnumRange, FormatRange, NameRange, NumericInterval, NumericIntervalRange

public abstract class RangeBase
extends java.lang.Object
implements Range

A base class for creating Range objects.


Field Summary
 
Fields inherited from interface maspack.util.Range
IllegalValue
 
Constructor Summary
RangeBase()
           
 
Method Summary
 RangeBase clone()
           
 void intersect(Range r)
          Intersects the set of valid values of this Range with those of another.
 boolean isEmpty()
          Returns true if this range is empty - i.e., if there are no valid values.
abstract  boolean isValid(java.lang.Object obj, StringHolder errMsg)
          Returns true if the specified object is valid for this Range, and false otherwise.
 boolean isWritable()
          Returns true if this component should in fact be written to secondary storage.
 java.lang.Object makeValid(java.lang.Object obj)
          Projects an object to lie within the range allowed by this Range, if possible.
 void scan(ReaderTokenizer rtok, java.lang.Object ref)
          Scans this element from a ReaderTokenizer.
static void setError(StringHolder errMsg, java.lang.String msg)
           
 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 java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

RangeBase

public RangeBase()
Method Detail

setError

public static void setError(StringHolder errMsg,
                            java.lang.String msg)

isValid

public abstract boolean isValid(java.lang.Object obj,
                                StringHolder errMsg)
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
Parameters:
obj - Object to be testes
errMsg - Optional handle for storing error message
Returns:
true if the object is valid

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
Parameters:
obj - object to be projected
Returns:
original obj if within range, or a projected object, or null.

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
Returns:
true if this range has no valid values.

intersect

public void intersect(Range r)
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
Parameters:
r - range to intersect with.

scan

public void scan(ReaderTokenizer rtok,
                 java.lang.Object ref)
          throws java.io.IOException
Scans this element from a ReaderTokenizer. The expected text format is assumed to be compatible with that produced by write.

Specified by:
scan in interface Scannable
Parameters:
rtok - Tokenizer from which to scan the element
ref - optional reference object which can be used for resolving references to other objects
Throws:
java.io.IOException - if an I/O or formatting error occured

isWritable

public boolean isWritable()
Returns true if this component should in fact be written to secondary storage. This gives subclasses control over whether or not they are actually written out.

Specified by:
isWritable in interface Scannable
Returns:
true if this component should be written to secondary storage.

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
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

clone

public RangeBase clone()
Specified by:
clone in interface Clonable
Overrides:
clone in class java.lang.Object