org.xins.common.types
Class Type

java.lang.Object
  extended byorg.xins.common.types.Type
Direct Known Subclasses:
Base64, Boolean, Date, Descriptor, EnumType, Float32, Float64, Hex, Int16, Int32, Int64, Int8, List, PatternType, Properties, Text, Timestamp, URL

public abstract class Type
extends Object

Value type. This is an abstract base class for type classes. Each type defines a name and it defines what values are considered valid and what values are considered invalid.

Since:
XINS 1.0.0
Version:
$Revision: 1.21 $ $Date: 2006/08/28 09:12:34 $
Author:
Ernst de Haan

Constructor Summary
protected Type(String name, Class valueClass)
          Creates a new Type instance.
 
Method Summary
 void checkValue(String value)
          Checks if the specified value is valid for this type and throws an exception if not.
 Object fromString(String string)
          Converts from a String to an instance of the value class for this type (wrapper method).
protected abstract  Object fromStringImpl(String string)
          Converts from a String to an instance of the value class for this type (implementation method).
 String getName()
          Retrieves the name of this type.
 Class getValueClass()
          Retrieves the value class.
 boolean isValidValue(String string)
          Determines if the specified String value is considered valid for this type (wrapper method).
protected  boolean isValidValueImpl(String string)
          Determines if the specified String value is considered valid for this type (implementation method).
 String toString()
          Returns a textual presentation of this object.
 String toString(Object value)
          Generates a string representation of the specified value for this type.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Type

protected Type(String name,
               Class valueClass)
        throws IllegalArgumentException
Creates a new Type instance. Both the name of the type and the value class must be specified. The value class in the class (or interface) that values for this type should be instances of. If null is specified as the value class, then that is the same as specifying Object.class as the value class.

Parameters:
name - the name of the type, not null.
valueClass - the class or interface that values should be instances of, or null if any class is valid.
Throws:
IllegalArgumentException - if name == null.
Method Detail

getName

public final String getName()
Retrieves the name of this type.

Returns:
the name of this type, never null.

getValueClass

public final Class getValueClass()
Retrieves the value class. All values for this type are instances of this class.

Returns:
the class values should be instances of, never null.

checkValue

public final void checkValue(String value)
                      throws TypeValueException
Checks if the specified value is valid for this type and throws an exception if not.

Note that null values are always considered to be valid.

Parameters:
value - the value that should be checked for validity, can be null.
Throws:
TypeValueException - if the specified value is invalid for this type.

isValidValue

public final boolean isValidValue(String string)
Determines if the specified String value is considered valid for this type (wrapper method).

This method first checks if string == null and if it is not, then it returns the result of a call to isValidValueImpl(String). Note that null values are always considered to be valid.

Parameters:
string - the value that should be checked for validity, can be null.
Returns:
true if and only if the specified value is valid, false otherwise.

isValidValueImpl

protected boolean isValidValueImpl(String string)
Determines if the specified String value is considered valid for this type (implementation method).

This method is called from isValidValue(String). When called from that method, it is guaranteed that the argument is not null.

The implementation of this method in class Type returns true.

Parameters:
string - the String value that should be checked for validity, never null.
Returns:
true if and only if the specified String value is valid, false otherwise.

fromString

public final Object fromString(String string)
                        throws TypeValueException
Converts from a String to an instance of the value class for this type (wrapper method).

This method returns null if string == null. Otherwise it first calls isValidValueImpl(String) to check if the value is in principle valid. If it is, then fromStringImpl(String) is called. If the result of that call is not an instance of the value class, then an Error is thrown. Notice that this error is also thrown if fromStringImpl(String) returns null.

Parameters:
string - the string to convert to an instance of the value class, can be null.
Returns:
an instance of the value class, will be null if and only if string == null.
Throws:
TypeValueException - if the specified string does not represent a valid value for this type.

fromStringImpl

protected abstract Object fromStringImpl(String string)
                                  throws TypeValueException
Converts from a String to an instance of the value class for this type (implementation method).

This method is not required to check the validity of the specified value (since isValidValueImpl(String) should have been called before) but if it does, then it may throw a TypeValueException.

Parameters:
string - the string to convert to an instance of the value class, guaranteed to be not null and guaranteed to have been passed to isValidValueImpl(String) without getting an exception.
Returns:
an instance of the value class, cannot be null.
Throws:
TypeValueException - if string is considered to be an invalid value for this type.

toString

public String toString(Object value)
                throws IllegalArgumentException,
                       ClassCastException,
                       TypeValueException
Generates a string representation of the specified value for this type. The specified value must be an instance of the value class for this type (see getValueClass()). Also, it may have to fall within a certain range of valid values, depending on the type.

The default implementation of this method in class Type does the following:

Parameters:
value - the value, cannot be null.
Returns:
the string representation of the specified value for this type, cannot be null.
Throws:
IllegalArgumentException - if value == null.
ClassCastException - if getValueClass().isInstance(value) == false.
TypeValueException - if the specified value is not in the allowed range.

toString

public final String toString()
Returns a textual presentation of this object. The implementation of this method just returns the name of this type.

Returns:
the textual presentation, never null.


See http://www.xins.org/.