edu.neu.ccs.util
Class StringableFactory

java.lang.Object
  extended byedu.neu.ccs.util.StringableFactory
All Implemented Interfaces:
Cloneable, Serializable

public class StringableFactory
extends Object
implements Cloneable, Serializable

Class whose instances can construct new instances of Stringable objects of a given class using reflection. Use of this type of factory is the preferred technique for TypedViews and GeneralViews to create and return data models built from user input data.

Since:
1.0
Version:
2.4.0
See Also:
Serialized Form

Field Summary
protected  SwingPropertyChangeSupport changeAdapter
          Helper object for property change API.
static String DATA_TYPE
          Bound property name for the data type property.
protected  Class type
          The class of objects to produce with this factory.
 
Constructor Summary
StringableFactory(Class dataType)
          Constructor for a factory capable of producing objects of the given Stringable class.
 
Method Summary
 void addPropertyChangeListener(PropertyChangeListener listener)
          Registers the given object to listen for property change events generated by this factory.
 void addPropertyChangeListener(String propertyName, PropertyChangeListener listener)
          Registers the given object to listen for property change events generated by this factory with the given property name.
static Stringable constructFrom(Class dataType, String data)
          Constructs a new Stringable object by constructing a default instance of the given type and setting the state of the object to the state encapsulated by the given String.
 Stringable constructFrom(String data)
          Returns a new instance of the stored class created using the zero-parameter constructor for the class, whose state is then set from the given String data.
static Stringable decodeTypeAndData(String data)
          Returns a new Stringable object constructed from the given String that encapsulates the type and state information of a previously existing Stringable object.
static String encodeTypeAndData(Stringable obj)
          Returns a String encapsulation of the class and state information for the given Stringable object.
 Class getDataType()
          Returns the class of objects produced by this factory.
 Stringable getDefaultInstance()
          Returns a new instance of the stored class created using the zero-parameter constructor for the class.
static boolean isStringable(Class dataType)
          Returns true if the given class is Stringable, and false if it is not.
 void removePropertyChangeListener(PropertyChangeListener listener)
          Deregisters the given object from listening for property change events generated by this factory.
 void removePropertyChangeListener(String propertyName, PropertyChangeListener listener)
          Deregisters the given object from listening for property change events generated by this factory with the given property name.
 void setDataType(Class dataType)
          Sets the class of objects produced by this factory to the given Stringable class.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DATA_TYPE

public static final String DATA_TYPE
Bound property name for the data type property.

See Also:
Constant Field Values

type

protected Class type
The class of objects to produce with this factory.


changeAdapter

protected SwingPropertyChangeSupport changeAdapter
Helper object for property change API.

Constructor Detail

StringableFactory

public StringableFactory(Class dataType)

Constructor for a factory capable of producing objects of the given Stringable class.

If the class is null, this factory will produce instances of the XString class.

If the given class is not Stringable, then an exception of type Error will be thrown.

Parameters:
dataType - the Stringable class of objects to be produced
Method Detail

getDefaultInstance

public Stringable getDefaultInstance()

Returns a new instance of the stored class created using the zero-parameter constructor for the class.

This method will throw an exception of type Error if a new instance cannot be created.

See Also:
constructFrom(String)

constructFrom

public Stringable constructFrom(String data)
                         throws ParseException
Returns a new instance of the stored class created using the zero-parameter constructor for the class, whose state is then set from the given String data.

Parameters:
data - the data String containing state information
Throws:
ParseException
See Also:
setDataType(Class)

setDataType

public void setDataType(Class dataType)

Sets the class of objects produced by this factory to the given Stringable class.

If the given class is null, this method will not change the stored class. This behavior represents a change from earlier versions of the method in which a null parameter led to a change of the stored class to XString.class. The new behavior works better in practical usage.

If the given class is not Stringable, then an exception of type Error will be thrown.

Parameters:
dataType - the desired class of objects to produce
See Also:
getDataType()

getDataType

public Class getDataType()
Returns the class of objects produced by this factory.

See Also:
setDataType(Class)

addPropertyChangeListener

public void addPropertyChangeListener(PropertyChangeListener listener)
Registers the given object to listen for property change events generated by this factory.

Parameters:
listener - the listener to be registered

addPropertyChangeListener

public void addPropertyChangeListener(String propertyName,
                                      PropertyChangeListener listener)
Registers the given object to listen for property change events generated by this factory with the given property name.

Parameters:
propertyName - the name of the desired property
listener - the listener to be registered

removePropertyChangeListener

public void removePropertyChangeListener(PropertyChangeListener listener)
Deregisters the given object from listening for property change events generated by this factory.

Parameters:
listener - the listener to be deregistered

removePropertyChangeListener

public void removePropertyChangeListener(String propertyName,
                                         PropertyChangeListener listener)
Deregisters the given object from listening for property change events generated by this factory with the given property name.

Parameters:
propertyName - the name of the desired property
listener - the listener to be deregistered

isStringable

public static boolean isStringable(Class dataType)
Returns true if the given class is Stringable, and false if it is not.

Parameters:
dataType - the class in question

encodeTypeAndData

public static String encodeTypeAndData(Stringable obj)
Returns a String encapsulation of the class and state information for the given Stringable object.

Parameters:
obj - the object to be encapsulated

decodeTypeAndData

public static Stringable decodeTypeAndData(String data)
                                    throws ParseException
Returns a new Stringable object constructed from the given String that encapsulates the type and state information of a previously existing Stringable object.

Implementation change: As of 2.4.0 decodes the data parameter using Strings.decode.

Parameters:
data - the String to be decoded
Throws:
NullPointerException - if the given String is null
ParseException - if the given String does not contain the type and state information required to construct a new object

constructFrom

public static Stringable constructFrom(Class dataType,
                                       String data)
                                throws ParseException

Constructs a new Stringable object by constructing a default instance of the given type and setting the state of the object to the state encapsulated by the given String.

Throws an exception of type Error if a fundamental error occurs while instantiating a new object.

Throws a ParseException if an object could be instantiated but the data string could not be used to set the object state.

Parameters:
dataType - the type of object to construct
data - the String to be used to set the state of the constructed object
Throws:
ParseException