ecologylab.xml.types.scalar
Class ScalarType<T>

java.lang.Object
  extended by ecologylab.generic.Debug
      extended by ecologylab.xml.types.scalar.ScalarType<T>
Direct Known Subclasses:
BooleanType, ByteType, CharType, ColorType, DateType, DoubleType, FileType, FloatType, IntType, KMLColorType, LongType, ReferenceType, ShortType

public abstract class ScalarType<T>
extends Debug

Basic unit of the scalar type system. Manages marshalling from a Java class that represents a scalar, to a String, and from a String to that Java class.

The ScalarType object is a means for associating a type name with a type index. It also knows how to create an instance of the type, given a String representation. If the ScalarType is a reference type, this is done with getInstance(String); if the ScalarType is a primitive, this is done with getValue(String), which cannot appear as a method in this, the base class, because it will return a different primitive type for each such Type.

Note: unlike with ElementState subtypes, translation of these is controlled entirely by the name of the underlying Java class that gets translated, and not by the class name of subclasses of this.

Author:
andruid

Field Summary
static java.lang.String DEFAULT_DELIMS
           
static java.lang.Object DEFAULT_VALUE
           
static java.lang.String DEFAULT_VALUE_STRING
           
 
Constructor Summary
protected ScalarType(java.lang.Class<T> thatClass)
          Constructor is protected because there should only be 1 instance that gets re-used, for each type.
 
Method Summary
 boolean affordsInterestExpression()
          True if the user should be able to express interest in fields of this type.
 boolean allowDelimitersInTokens()
          When editing, determines whether delimiters can be included in token strings.
 boolean allowNewLines()
           
 void appendValue(java.lang.Appendable buffy, java.lang.reflect.Field field, java.lang.Object context, boolean needsEscaping)
          Get the value from the Field, in the context.
 void appendValue(java.lang.StringBuilder buffy, java.lang.reflect.Field field, java.lang.Object context, boolean needsEscaping)
          Get the value from the Field, in the context.
 void appendValue(T instance, java.lang.Appendable appendable, boolean needsEscaping)
           
 void appendValue(T instance, java.lang.StringBuilder buffy, boolean needsEscaping)
           
 boolean composedOfTerms()
          When editing, is the field one that should be part of the Term model?
protected  T defaultValue()
          The default value for this, in its own type.
 int defaultValueLength()
           
protected  java.lang.String defaultValueString()
          The default value for this type, as a String.
 java.lang.String delimeters()
          For editing: these are the valid delimiters for separating tokens that make up a field of this type.
 java.lang.String getClassName()
           
abstract  T getInstance(java.lang.String value)
          If this is a reference type, build an appropriate Object, given a String representation.
 java.lang.Class getTypeClass()
          Get the class object for the Type for which this manages conversion.
 java.lang.String illegalChars()
          When editing, do not allow the user to include these characters in the resulting value String.
 boolean isDefaultValue(java.lang.reflect.Field field, java.lang.Object context)
           
 boolean isDefaultValue(java.lang.String value)
           
 boolean isFloatingPoint()
          Returns whether or not this is a floating point value of some sort; Types that are floating point values should override this method to return true.
 boolean isPrimitive()
          Find out if this is a reference type or a primitive types.
 boolean isReference()
          Find out if this is a reference type or a primitive types.
 java.lang.String marshall(T instance)
          Get a String representation of the instance, using this.
 boolean needsEscaping()
          Return true if this type may need escaping when emitted as XML.
 boolean setField(java.lang.Object object, java.lang.reflect.Field field, java.lang.String value)
          Set the field represented to the value of Many different types of exceptions may be thrown.
protected  void setFieldError(java.lang.reflect.Field field, java.lang.String value, java.lang.Exception e)
          Display an error message that arose while setting field to value.
 java.lang.String toString(java.lang.reflect.Field field, java.lang.Object context)
          The string representation for a Field of this type Default implementation uses the Object's toString() method.
 
Methods inherited from class ecologylab.generic.Debug
classSimpleName, closeLoggingFile, debug, debug, debug, debug, debugA, debugA, debugA, debugI, debugI, debugI, error, error, getClassName, getInteractive, getPackageName, getPackageName, getPackageName, initialize, level, level, level, logToFile, print, print, println, println, println, println, println, println, printlnA, printlnA, printlnA, printlnI, printlnI, printlnI, printlnI, setLoggingFile, show, show, superString, toggleInteractive, toString, toString, warning, warning, weird, weird
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

DEFAULT_VALUE

public static final java.lang.Object DEFAULT_VALUE

DEFAULT_VALUE_STRING

public static final java.lang.String DEFAULT_VALUE_STRING
See Also:
Constant Field Values

DEFAULT_DELIMS

public static final java.lang.String DEFAULT_DELIMS
See Also:
Constant Field Values
Constructor Detail

ScalarType

protected ScalarType(java.lang.Class<T> thatClass)
Constructor is protected because there should only be 1 instance that gets re-used, for each type. To get the instance of this type object for use in translations, call TypeRegistry.get("type-string").

Method Detail

getInstance

public abstract T getInstance(java.lang.String value)
If this is a reference type, build an appropriate Object, given a String representation. If it is a primitive type, return a boxed value.

Parameters:
value - String representation of the instance.

setField

public boolean setField(java.lang.Object object,
                        java.lang.reflect.Field field,
                        java.lang.String value)
Set the field represented to the value of Many different types of exceptions may be thrown. These include IllegalAccessException on the one hand, which would come from problemswith using reflection to access the Field. This is very unlikely.

More likely are problems with conversion of the parameter value into into an object or primitive of the proper type.

Parameters:
object - The object whose field should be modified.
field - The field to be set.
value - String representation of the value to set the field to. This Type will convert the value to the appropriate type, using getInstance(String) for reference types, and type specific getValue(String) methods for primitive types.
Returns:
true if the field is set properly, or if the parameter value that is passed in is null. false if the field cannot be accessed, or if value cannot be converted to the appropriate type.

setFieldError

protected void setFieldError(java.lang.reflect.Field field,
                             java.lang.String value,
                             java.lang.Exception e)
Display an error message that arose while setting field to value.

Parameters:
field -
value -
e -

getClassName

public java.lang.String getClassName()
Overrides:
getClassName in class Debug
Returns:
Returns the simple className (unqualified) for this type.

isPrimitive

public boolean isPrimitive()
Find out if this is a reference type or a primitive types.

Returns:
true for a primitive type. false for a reference type.

needsEscaping

public boolean needsEscaping()
Return true if this type may need escaping when emitted as XML.

Returns:
true, by default, for all reference types (includes Strings, PURLs, ...); false otherwise.

isReference

public boolean isReference()
Find out if this is a reference type or a primitive types.

Returns:
true for a reference type. false for a primitive type.

toString

public java.lang.String toString(java.lang.reflect.Field field,
                                 java.lang.Object context)
The string representation for a Field of this type Default implementation uses the Object's toString() method. This is usually going to be wrong.


appendValue

public void appendValue(java.lang.Appendable buffy,
                        java.lang.reflect.Field field,
                        java.lang.Object context,
                        boolean needsEscaping)
                 throws java.lang.IllegalArgumentException,
                        java.lang.IllegalAccessException,
                        java.io.IOException
Get the value from the Field, in the context. Append its value to the buffy.

Should only be called *after* checking !isDefault() yourself.

Parameters:
buffy -
field -
context -
needsEscaping - TODO
Throws:
java.lang.IllegalAccessException
java.lang.IllegalArgumentException
java.io.IOException

marshall

public java.lang.String marshall(T instance)
Get a String representation of the instance, using this. The default just calls the toString() method on the instance.

Parameters:
instance -
Returns:

appendValue

public void appendValue(java.lang.StringBuilder buffy,
                        java.lang.reflect.Field field,
                        java.lang.Object context,
                        boolean needsEscaping)
                 throws java.lang.IllegalArgumentException,
                        java.lang.IllegalAccessException
Get the value from the Field, in the context. Append its value to the buffy.

Should only be called *after* checking !isDefault() yourself.

Parameters:
buffy -
field -
context -
needsEscaping - TODO
Throws:
java.lang.IllegalAccessException
java.lang.IllegalArgumentException

appendValue

public void appendValue(T instance,
                        java.lang.StringBuilder buffy,
                        boolean needsEscaping)

appendValue

public void appendValue(T instance,
                        java.lang.Appendable appendable,
                        boolean needsEscaping)
                 throws java.io.IOException
Throws:
java.io.IOException

defaultValueString

protected java.lang.String defaultValueString()
The default value for this type, as a String. This value is the one that translateToXML(...) wont bother emitting. In this case, "null".


defaultValue

protected T defaultValue()
The default value for this, in its own type. Not meaningful for primitive types.

Returns:

defaultValueLength

public final int defaultValueLength()

isDefaultValue

public boolean isDefaultValue(java.lang.String value)

isDefaultValue

public boolean isDefaultValue(java.lang.reflect.Field field,
                              java.lang.Object context)
                       throws java.lang.IllegalArgumentException,
                              java.lang.IllegalAccessException
Throws:
java.lang.IllegalArgumentException
java.lang.IllegalAccessException

isFloatingPoint

public boolean isFloatingPoint()
Returns whether or not this is a floating point value of some sort; Types that are floating point values should override this method to return true. The implication of returning true is that the precision of this can be controlled when it is emitted as XML.

Returns:
false

allowNewLines

public boolean allowNewLines()

getTypeClass

public java.lang.Class getTypeClass()
Get the class object for the Type for which this manages conversion.

Returns:
Class associated with this Type.

delimeters

public java.lang.String delimeters()
For editing: these are the valid delimiters for separating tokens that make up a field of this type.

Returns:

allowDelimitersInTokens

public boolean allowDelimitersInTokens()
When editing, determines whether delimiters can be included in token strings.

Returns:

illegalChars

public java.lang.String illegalChars()
When editing, do not allow the user to include these characters in the resulting value String.

Returns:

composedOfTerms

public boolean composedOfTerms()
When editing, is the field one that should be part of the Term model?

Returns:
true for Strings

affordsInterestExpression

public boolean affordsInterestExpression()
True if the user should be able to express interest in fields of this type.

Returns:
true for Strings