public abstract class Type extends Object
Modifier | Constructor and Description |
---|---|
protected |
Type(String name,
Class valueClass)
Creates a new
Type instance. |
Modifier and Type | Method and Description |
---|---|
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 |
getDescription()
Retrieves the description of this type.
|
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.
|
protected Type(String name, Class valueClass) throws IllegalArgumentException
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.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.IllegalArgumentException
- if name == null
.public final String getName()
null
.public String getDescription()
null
.public final Class getValueClass()
null
.public final void checkValue(String value) throws TypeValueException
null
values are always considered
to be valid.value
- the value that should be checked for validity, can be
null
.TypeValueException
- if the specified value is invalid for this type.public final boolean isValidValue(String string)
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.string
- the value that should be checked for validity, can be
null
.true
if and only if the specified value is valid,
false
otherwise.protected boolean isValidValueImpl(String string)
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
.
Type
returns
true
.string
- the String
value that should be checked for validity,
never null
.true
if and only if the specified String
value is valid, false
otherwise.public final Object fromString(String string) throws TypeValueException
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
.string
- the string to convert to an instance of the value class, can be
null
.null
if and only
if string == null
.TypeValueException
- if the specified string does not represent a valid value for this
type.protected abstract Object fromStringImpl(String string) throws TypeValueException
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
.
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.null
.TypeValueException
- if string
is considered to be an invalid value for this
type.public String toString(Object value) throws IllegalArgumentException, ClassCastException, TypeValueException
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:
value == null
then it throws an
IllegalArgumentException
;
getValueClass().isInstance(value) == false
then
it throws a ClassCastException
;
value.
Object.toString()
.
value
- the value, cannot be null
.null
.IllegalArgumentException
- if value == null
.ClassCastException
- if getValueClass().isInstance(value) == false
.TypeValueException
- if the specified value is not in the allowed range.See http://www.xins.org/.