jimm.datavision
Class Parameter

java.lang.Object
  extended by java.util.Observable
      extended by jimm.datavision.Parameter
All Implemented Interfaces:
java.lang.Cloneable, Draggable, Identity, Nameable, Writeable

public class Parameter
extends java.util.Observable
implements Identity, Nameable, Writeable, Draggable, java.lang.Cloneable

A parameter is a piece of data the value of which is determined by asking the user each time a report runs. Default values are only used when asking the user for values, not when generating values in getValue.

I started out with subclasses for each type of parameter. The problem is, the user gets to pick what kind of data the parameter holds and that type can be changed any time after the parameter gets created. Therefore, we hold objects and change our output based on the type of the data.

Author:
Jim Menard, jimm@io.com

Field Summary
protected  int arity
           
static int ARITY_LIST_MULTIPLE
           
static int ARITY_LIST_SINGLE
           
static int ARITY_ONE
           
static int ARITY_RANGE
           
protected  java.util.ArrayList defaultValues
           
protected static java.text.SimpleDateFormat formatter
           
protected  java.lang.Long id
           
protected  java.lang.String name
           
protected static java.text.ParsePosition parsePosition
           
protected  java.lang.String question
           
protected  Report report
           
protected  int type
           
static int TYPE_BOOLEAN
           
static int TYPE_DATE
           
static int TYPE_NUMERIC
           
static int TYPE_STRING
           
protected  java.util.ArrayList values
           
 
Constructor Summary
Parameter(java.lang.Long id, Report report)
          Constructor.
Parameter(java.lang.Long id, Report report, int type, java.lang.String name, java.lang.String question, int arity)
          Constructor.
Parameter(java.lang.Long id, Report report, java.lang.String typeName, java.lang.String name, java.lang.String question, java.lang.String arityString)
          Constructor.
 
Method Summary
 void addDefaultValue(java.lang.Object newDefaultValue)
          Adds a default value to the list.
 void addValue(java.lang.Object newValue)
          Adds a value to the list.
 java.lang.Object clone()
           
protected  java.lang.Object convertType(java.lang.Object val)
          Converts the specified object to the proper type for this parameter.
 java.util.Iterator defaultValues()
          Returns an iterator over the default values for this parameter.
 java.lang.String designLabel()
           
 java.lang.String dragString()
          Returns the string used to identify the object when dragging it.
 java.lang.String formulaString()
           
 int getArity()
          Returns the arity of this field.
 java.lang.Object getDefaultForType(int type)
          Returns the default value for a specific parameter type.
 java.lang.Object getDefaultValue(int i)
          Returns the i'th defaultValue for this parameter.
 java.lang.Object getId()
          Returns the identity.
 java.lang.String getName()
          Returns the name for this parameter.
 java.lang.String getQuestion()
          Returns the question for this parameter.
 int getType()
          Returns the type of this field.
 java.lang.Object getValue()
          Returns the parameter value(s) the user has previously specified.
 java.lang.Object getValue(int i)
          Returns the current value or, if that is null, the default value.
 boolean isLegal(int aType, int anArity)
          Returns true if the specified combination of type and arity are legal.
 void removeDefaultValues()
          Erases all default values.
 void removeValues()
          Erases all values.
 void setArity(int newArity)
          Sets the parameter arity.
 void setDefaultValue(int i, java.lang.Object newDefaultValue)
          Sets the i'th defaultValue.
 void setName(java.lang.String newName)
          Sets the name.
 void setQuestion(java.lang.String newQuestion)
          Sets the question.
 void setType(int newType)
          Sets the parameter type.
 void setValue(int i, java.lang.Object newValue)
          Sets the i'th value.
 java.lang.String toString()
           
protected  java.lang.String typeString()
          Returns the string used as the "type" attribute when writing this parameter as XML.
 java.util.Iterator values()
          Returns an iterator over the values for this parameter.
 void writeXML(XMLWriter out)
          Writes this parameter as an XML tag.
 
Methods inherited from class java.util.Observable
addObserver, clearChanged, countObservers, deleteObserver, deleteObservers, hasChanged, notifyObservers, notifyObservers, setChanged
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

TYPE_BOOLEAN

public static final int TYPE_BOOLEAN
See Also:
Constant Field Values

TYPE_STRING

public static final int TYPE_STRING
See Also:
Constant Field Values

TYPE_NUMERIC

public static final int TYPE_NUMERIC
See Also:
Constant Field Values

TYPE_DATE

public static final int TYPE_DATE
See Also:
Constant Field Values

ARITY_ONE

public static final int ARITY_ONE
See Also:
Constant Field Values

ARITY_RANGE

public static final int ARITY_RANGE
See Also:
Constant Field Values

ARITY_LIST_SINGLE

public static final int ARITY_LIST_SINGLE
See Also:
Constant Field Values

ARITY_LIST_MULTIPLE

public static final int ARITY_LIST_MULTIPLE
See Also:
Constant Field Values

formatter

protected static java.text.SimpleDateFormat formatter

parsePosition

protected static java.text.ParsePosition parsePosition

id

protected java.lang.Long id

report

protected Report report

name

protected java.lang.String name

question

protected java.lang.String question

type

protected int type

arity

protected int arity

defaultValues

protected java.util.ArrayList defaultValues

values

protected java.util.ArrayList values
Constructor Detail

Parameter

public Parameter(java.lang.Long id,
                 Report report)
Constructor. Creates a string parameter with no name or question string.

Parameters:
id - the unique identifier for the new parameter; if null, generate a new id
report - the report in which this parameter resides

Parameter

public Parameter(java.lang.Long id,
                 Report report,
                 java.lang.String typeName,
                 java.lang.String name,
                 java.lang.String question,
                 java.lang.String arityString)
Constructor.

If id is null, generates a new id number. This number is one higher than any previously-seen id number. This does not guarantee that no later parameter will be created manually with the same id number.

Parameters:
id - the unique identifier for the new parameter; if null, generate a new id
report - the report in which this parameter resides
typeName - one of "string", "numeric", or "date"; found in report XML
name - the name of this parameter
question - the question to ask when getting the parameter's value from the user
arityString - arity (single, range, list) as a string

Parameter

public Parameter(java.lang.Long id,
                 Report report,
                 int type,
                 java.lang.String name,
                 java.lang.String question,
                 int arity)
Constructor.

If id is null, generates a new id number. This number is one higher than any previously-seen id number. This does not guarantee that no later parameter will be created manually with the same id number.

Parameters:
id - the unique identifier for the new parameter; if null, generate a new id
report - the report in which this parameter resides
type - one of TYPE_BOOLEAN, TYPE_STRING, TYPE_NUMERIC, or TYPE_DATE
name - the name of this parameter
question - the name of this parameter
arity - one of ARITY_ONE, ARITY_RANGE, ARITY_LIST_SINGLE, or ARITY_LIST_MULTIPLE
Method Detail

clone

public java.lang.Object clone()
Overrides:
clone in class java.lang.Object

getId

public java.lang.Object getId()
Description copied from interface: Identity
Returns the identity.

Specified by:
getId in interface Identity

getName

public java.lang.String getName()
Returns the name for this parameter.

Specified by:
getName in interface Nameable
Returns:
the name

setName

public void setName(java.lang.String newName)
Sets the name.

Specified by:
setName in interface Nameable
Parameters:
newName - the new name

getQuestion

public java.lang.String getQuestion()
Returns the question for this parameter.

Returns:
the question

setQuestion

public void setQuestion(java.lang.String newQuestion)
Sets the question.

Parameters:
newQuestion - the new question

getType

public int getType()
Returns the type of this field. Will be one of TYPE_BOOLEAN, TYPE_STRING, TYPE_NUMERIC, or TYPE_DATE.

Returns:
the type number

setType

public void setType(int newType)
Sets the parameter type. Must be one of TYPE_BOOLEAN, TYPE_STRING, TYPE_NUMERIC, or TYPE_DATE. If the new type is different than the old, we also make sure the arity is appropriate (for example, no boolean lists) and clear the value and default value lists.

Parameters:
newType - the new type; must be one of TYPE_BOOLEAN, TYPE_STRING, TYPE_NUMERIC, or TYPE_DATE

getArity

public int getArity()
Returns the arity of this field. Will be one of ARITY_ONE, ARITY_RANGE, ARITY_LIST_SINGLE, or ARITY_LIST_MULTIPLE.

Returns:
the arity number

isLegal

public boolean isLegal(int aType,
                       int anArity)
Returns true if the specified combination of type and arity are legal.

Parameters:
aType - one of TYPE_BOOLEAN, TYPE_STRING, TYPE_NUMERIC, or TYPE_DATE
anArity - one of ARITY_ONE, ARITY_RANGE, ARITY_LIST_SINGLE, or ARITY_LIST_MULTIPLE
Returns:
true if the specified combination of type and arity are legal

setArity

public void setArity(int newArity)
Sets the parameter arity. Must be one of ARITY_ONE, ARITY_RANGE, ARITY_LIST_SINGLE, or ARITY_LIST_MULTIPLE. We disallow illegal arity values. For example, if our type is boolean we disallow a list arity.

Parameters:
newArity - one of ARITY_ONE, ARITY_RANGE, ARITY_LIST_SINGLE, or ARITY_LIST_MULTIPLE

defaultValues

public java.util.Iterator defaultValues()
Returns an iterator over the default values for this parameter.

Returns:
an interator

getDefaultValue

public java.lang.Object getDefaultValue(int i)
Returns the i'th defaultValue for this parameter. If none has been assigned, create and return -- but do not store -- a reasonable default. The default is obtained by calling getDefaultForType(int).

Parameters:
i - the index
Returns:
the defaultValue

getDefaultForType

public java.lang.Object getDefaultForType(int type)
Returns the default value for a specific parameter type. This is not the same as the i'th default value; it is called when you have a parameter that has no value or default value, or when you have one with a different type and you want to switch types.

Parameters:
type - one of TYPE_BOOLEAN, TYPE_STRING, TYPE_NUMERIC, or TYPE_DATE
Returns:
a new object appropriate for the type

removeDefaultValues

public void removeDefaultValues()
Erases all default values.


addDefaultValue

public void addDefaultValue(java.lang.Object newDefaultValue)
Adds a default value to the list.

Parameters:
newDefaultValue - a new default value

setDefaultValue

public void setDefaultValue(int i,
                            java.lang.Object newDefaultValue)
Sets the i'th defaultValue. If i is out of range, the list of default values grows to fit.

Parameters:
i - the index
newDefaultValue - a value

values

public java.util.Iterator values()
Returns an iterator over the values for this parameter.

Returns:
an interator

getValue

public java.lang.Object getValue()
Returns the parameter value(s) the user has previously specified. If the parameter has one value, return that value or possibly null. Else, return a copy of our list of values.

Returns:
values (see description)

getValue

public java.lang.Object getValue(int i)
Returns the current value or, if that is null, the default value. If the index is out of range, return null.

Parameters:
i - the index
Returns:
the current or default value.

addValue

public void addValue(java.lang.Object newValue)
Adds a value to the list.

Parameters:
newValue - a new value

removeValues

public void removeValues()
Erases all values.


setValue

public void setValue(int i,
                     java.lang.Object newValue)
Sets the i'th value. If i is out of range, the list of values grows to fit. param i the index

Parameters:
newValue - the new value

convertType

protected java.lang.Object convertType(java.lang.Object val)
Converts the specified object to the proper type for this parameter. Whenever we add or set a value or default value, we convert it to the proper type (string, date, etc.)

If our type is boolean and the incoming object is:


typeString

protected java.lang.String typeString()
Returns the string used as the "type" attribute when writing this parameter as XML.

Returns:
the "type" attribute string

dragString

public java.lang.String dragString()
Description copied from interface: Draggable
Returns the string used to identify the object when dragging it.

Specified by:
dragString in interface Draggable
Returns:
a string used to identify the object when dragging it

designLabel

public java.lang.String designLabel()

formulaString

public java.lang.String formulaString()

writeXML

public void writeXML(XMLWriter out)
Writes this parameter as an XML tag.

Specified by:
writeXML in interface Writeable
Parameters:
out - a writer that knows how to write XML

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object