com.ryanm.preflect
Class VariableType<T>

java.lang.Object
  extended by com.ryanm.preflect.VariableType<T>
Type Parameters:
T - Type of variable to handle
Direct Known Subclasses:
BooleanType, CSVPrefType, EnumType, FloatType, IntType, StringType, VoidType

public abstract class VariableType<T>
extends java.lang.Object

Extend this to define new variable types. You'll need code to encode an decode a value to and from strings, construct an appropriate Preference, and optionally to validate/format user input. Remember to register(VariableType) your new variable type or it won't be used

Author:
ryanm

Field Summary
 java.lang.Class<? extends T> type
          Variable type
 
Constructor Summary
protected VariableType(java.lang.Class<? extends T> type)
           
 
Method Summary
protected abstract  Preference buildPreference(Context context, java.lang.Class type, java.lang.String value)
          Build a widget to control the supplied variable.
abstract  T decode(java.lang.String encoded, java.lang.Class runtimeType)
          Decodes the encoded string into a value object
abstract  java.lang.String encode(T value)
          Encodes the value of a given object
protected  java.lang.String formatInput(java.lang.Object input)
          Default format behaviour, just does toString().
static void register(VariableType varType)
          Call this to register your VariableTypes
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

type

public final java.lang.Class<? extends T> type
Variable type

Constructor Detail

VariableType

protected VariableType(java.lang.Class<? extends T> type)
Parameters:
type -
Method Detail

encode

public abstract java.lang.String encode(T value)
Encodes the value of a given object

Parameters:
value - The value to encode
Returns:
The String encoding for the value, or null if encoding was not possible

decode

public abstract T decode(java.lang.String encoded,
                         java.lang.Class runtimeType)
                  throws ParseException
Decodes the encoded string into a value object

Parameters:
encoded - The encoded string
runtimeType - The desired type of the object
Returns:
The value of the encoded string
Throws:
ParseException - If there is a problem parsing the encoded string

buildPreference

protected abstract Preference buildPreference(Context context,
                                              java.lang.Class type,
                                              java.lang.String value)
Build a widget to control the supplied variable. Variable title, summary, order and a default input formatting action are handled for you.

Parameters:
context - Well who'd have thought it! A context!
type - runtime type of the variable. Needed to get enum values, not much use otherwise
value - The current value of the variable
Returns:
An appropriate View
See Also:
formatInput(Object)

formatInput

protected java.lang.String formatInput(java.lang.Object input)
Default format behaviour, just does toString(). Override if that doesn't work for you. Throw an exception to indicate trouble, the exception message will be Toasted at the user

Parameters:
input - The user input, fresh from the Preference created in buildPreference(Context, Class, String)
Returns:
A properly-formatted value that won't cause trouble on the other end

register

public static void register(VariableType varType)
Call this to register your VariableTypes

Parameters:
varType -