com.davisor.data
Class CalendarFieldData

java.lang.Object
  extended byjava.util.Date
      extended bycom.davisor.data.CalendarFieldData
All Implemented Interfaces:
java.lang.Cloneable, java.lang.Comparable, Data, DataValue, com.davisor.core.Dupable, java.io.Serializable, SerializableData

public class CalendarFieldData
extends java.util.Date
implements DataValue

CalendarFieldData represents the calendar field value. Unlike traditional dates, a calendar field does not represent an exact moment of time, or even a fixed period of time. Instead, calendar dates represent abstract calendar time periods like "five years" or "three weeks". The interpretation of such values depends on which calendar system is used, and what calendar date it is.

Since:
JDK1.2
See Also:
Serialized Form

Field Summary
protected  CalendarField M_field
          Calendar field.
protected  DateFormatType M_type
          The type of this data.
 
Constructor Summary
CalendarFieldData()
          Default constructor.
CalendarFieldData(CalendarFieldData cfd, boolean deep)
          Deep or shallow copy constructor.
CalendarFieldData(DateFormatType type)
          Creates a calendar field data value from a given type and default value (0 ms).
CalendarFieldData(DateFormatType type, CalendarField field)
          Creates a calendar field data value from a given type and a calendar field value.
CalendarFieldData(DateFormatType type, CalendarFieldData data)
          Creates a calendar field data value from a given type and a calendar field data value.
CalendarFieldData(DateFormatType type, int[] fieldValue)
          Creates a calendar field data value from a given type and a calendar field value.
CalendarFieldData(DateFormatType type, java.lang.String stringValue, java.text.ParsePosition status)
          Creates a calendar field data value from a string value.
 
Method Summary
 DataValue abs()
          Returns absolute data value of current value.
 DataValue add(DataValue value)
          Adds given value to current value.
 DataValue ceil(DataValue precision)
          Quantizes the current value up to an even value within given precision.
 int compareTo(java.lang.Object o)
          Compares this timestamp against other Date objects.
 double deinterpolate(DataValue min, DataValue max)
          De-interpolates the current data value to a scalar value, in respect, with given data value range.
 DataValue div(DataValue value)
          Divides the current value with given value.
 com.davisor.core.Dupable dup()
          Makes a deep copy of this object.
 DataValue floor(DataValue precision)
          Quantizes the current value down to an even value within given precision.
 CalendarField getField()
          Gets the calendar field.
 java.lang.String getFormat()
          Gets the type specification optimal for this particular value.
 long getTime()
          Gets the duration of this calendar field in milliseconds.
 Type getType()
          Gets data type.
 DataValue interpolate(double value, DataValue max)
          Interpolates the given scalar value to data value, in respect, with the range defined by this value and the given maximum value.
 boolean isNull()
          Tests if this data object value is undetermined.
 DataValue max(DataValue value)
          Finds the larger of this and the given data value.
 DataValue min(DataValue value)
          Finds the smaller of this and the given data value.
 DataValue mul(DataValue value)
          Multipies the current value with given value.
 DataValue negate()
          Returns the negation of current value.
 void setField(CalendarField field)
          Sets the calendar field.
 void setTime(long time)
          Sets the duration of this calendar date in milliseconds.
 void setValue(int value)
          Sets the calendar field value.
 void setValue(java.lang.String stringValue, java.text.ParsePosition status)
          Sets data value from a string value.
 DataValue sub(DataValue value)
          Substracts given value from current value.
 void toStatement(java.sql.PreparedStatement statement, int parameter)
          Converts data to a prepared statement input parameter.
 java.lang.String toString()
          Gets the string representation of this calendar date.
 java.lang.String toXML()
          Gets the XML string representation of this value.
 DataValue unit(DataValue targetValue, double maxSteps)
          Gets the largest suitable unit step to represent a value change between this value and the given target value.
 
Methods inherited from class java.util.Date
after, before, clone, compareTo, equals, getDate, getDay, getHours, getMinutes, getMonth, getSeconds, getTimezoneOffset, getYear, hashCode, parse, setDate, setHours, setMinutes, setMonth, setSeconds, setYear, toGMTString, toLocaleString, UTC
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

M_type

protected DateFormatType M_type
The type of this data.


M_field

protected CalendarField M_field
Calendar field.

Constructor Detail

CalendarFieldData

public CalendarFieldData()
Default constructor.

Assosiates this data with the default type, and sets the current calendar field to zero milliseconds.

See Also:
CalendarField

CalendarFieldData

public CalendarFieldData(CalendarFieldData cfd,
                         boolean deep)
Deep or shallow copy constructor. If it is shallow, then the new object shares an underlying type with the original object. If it is deep, then the type is dupped.

Parameters:
cfd - source calendar field data (may be null)
deep - determines if the copying is deep or shallow
See Also:
getType(), Type.dup()

CalendarFieldData

public CalendarFieldData(DateFormatType type)
Creates a calendar field data value from a given type and default value (0 ms).

Parameters:
type - calendar field type

CalendarFieldData

public CalendarFieldData(DateFormatType type,
                         java.lang.String stringValue,
                         java.text.ParsePosition status)
                  throws InvalidDataException
Creates a calendar field data value from a string value.

Parameters:
type - calendar field type
stringValue - string value to be parsed
status - parse status (may be null)
Throws:
InvalidDataException - if string value could not be parser
See Also:
setValue(String,ParsePosition), DateFormatType.dateValue(String,ParsePosition)

CalendarFieldData

public CalendarFieldData(DateFormatType type,
                         int[] fieldValue)
Creates a calendar field data value from a given type and a calendar field value.

Parameters:
type - calendar field type
fieldValue - Java Calendar field code and value

CalendarFieldData

public CalendarFieldData(DateFormatType type,
                         CalendarFieldData data)
Creates a calendar field data value from a given type and a calendar field data value.

Parameters:
type - calendar field type
data - calendar field data

CalendarFieldData

public CalendarFieldData(DateFormatType type,
                         CalendarField field)
Creates a calendar field data value from a given type and a calendar field value.

Parameters:
type - calendar field type (may be null)
field - Java Calendar field (may be null)
Method Detail

compareTo

public int compareTo(java.lang.Object o)
Compares this timestamp against other Date objects.

Specified by:
compareTo in interface java.lang.Comparable
Parameters:
o - a Date object to compare this timestamp against with
Returns:
-1, 0, or 1 as this object is less than, equal to, or greater than the specified object
Throws:
java.lang.ClassCastException - if the given object is not a Date
See Also:
TimestampData.compareTo(java.util.Date,Object)

getType

public Type getType()
             throws InvalidDataException
Gets data type.

Specified by:
getType in interface Data
Returns:
the type associated with this data object
Throws:
InvalidDataException - if type retrieval fails

isNull

public boolean isNull()
Tests if this data object value is undetermined.

Specified by:
isNull in interface SerializableData
See Also:
Type.isNull(com.davisor.data.SerializableData)

abs

public DataValue abs()
              throws InvalidDataException
Returns absolute data value of current value.

Absolute values must satisfy a requirement that when added to any other value, the result must be greater than or equal to the original value.

Specified by:
abs in interface DataValue
Returns:
a new data object, sharing the current type of this object
Throws:
InvalidDataException - if absolute value can not be computed

add

public DataValue add(DataValue value)
              throws InvalidDataException
Adds given value to current value. The nature of the addition operation depends on given value type. In each case, the result will be returned in a time data object type best suited for to represent the result.

If the value is an instance of CalendarData, the current calendar field value is added to it.

If the value is an instance of CalendarFieldData, and the field type is equal to current field, the field values are added. If the field types are not equal, a new calendar containing the two fields is created.

If the value is an instance of Date, it is converted to a calendar value, to which the current field value is then added.

In any other case, the operation fails.

Specified by:
add in interface DataValue
Parameters:
value - a data value to be added to this data value
Returns:
a new data object, sharing the current type of this object
Throws:
InvalidDataException - if the data values cannot be added
See Also:
sub(com.davisor.data.DataValue), DateFormatType.toData(java.util.Date)

ceil

public DataValue ceil(DataValue precision)
               throws InvalidDataException
Quantizes the current value up to an even value within given precision. The returned value is more than or equal with the current value, but not more than the current value added by given precision.

Specified by:
ceil in interface DataValue
Parameters:
precision - the precision within quantization occurs
Returns:
a new data object, sharing the current type of this object
Throws:
InvalidDataException - if precision was of unacceptable type
See Also:
floor(com.davisor.data.DataValue)

deinterpolate

public double deinterpolate(DataValue min,
                            DataValue max)
                     throws InvalidDataException
De-interpolates the current data value to a scalar value, in respect, with given data value range. This gives scalar values between [0,1] when the current data value in within the range, negative scalar values when the data value falls below the range and scalar values greater than 1.0 otherwise.

Timestamp values are de-interpolated with the equation:

(thisValue - minValue) / (maxValue - minValue)

Specified by:
deinterpolate in interface DataValue
Parameters:
min - a data value representing a range's lower limit
max - a data value representing a range's higher limit
Returns:
de-interpolated scalar value
Throws:
InvalidDataException - if the de-interpolation fails
See Also:
DataValue.interpolate(double, com.davisor.data.DataValue), DeinterpolationException, TupleData.deinterpolate(com.davisor.data.DataValue, com.davisor.data.DataValue)

div

public DataValue div(DataValue value)
              throws InvalidDataException
Divides the current value with given value.

Specified by:
div in interface DataValue
Parameters:
value - the divider
Returns:
a new data object, sharing the current type of this object.
Throws:
InvalidDataException - if divider is not a number.

floor

public DataValue floor(DataValue precision)
                throws InvalidDataException
Quantizes the current value down to an even value within given precision. The returned value is less than or equal to the current value, but not less than the current value substracted by given precision.

Specified by:
floor in interface DataValue
Parameters:
precision - the precision within quantization occurs
Returns:
a new data object, sharing the current type of this object
Throws:
InvalidDataException - if precision was of unacceptable type
See Also:
ceil(com.davisor.data.DataValue), DateFormatType.toData(Object)

getFormat

public java.lang.String getFormat()
Gets the type specification optimal for this particular value. The specification returned may differ from the current type specification. If so, setting the current type specification equal with the value returned by this method will ensure optimal value formatting and parsing for values of the same magnitude as this value.

This method is currently unimplemented.

Specified by:
getFormat in interface DataValue
See Also:
Data.getType(), Type.getFormat(), Type.setFormat(java.lang.String)

interpolate

public DataValue interpolate(double value,
                             DataValue max)
                      throws InvalidDataException
Interpolates the given scalar value to data value, in respect, with the range defined by this value and the given maximum value. Scalar values between [0,1] result in data values between the current value and the given maximum value. Negative scalar values result in values smaller than this value, and scalar values higher than 1.0 return data values above the given maximum.

All number values can interpolate with this simple equation:

minValue + value * (maxValue - minValue)

Specified by:
interpolate in interface DataValue
Parameters:
value - the scalar value to be interpolated
max - a data value representing a range's higher limit
Returns:
a new data object containing the result
Throws:
InvalidDataException - if the interpolation fails
See Also:
DateFormatType.toData(Object)

max

public DataValue max(DataValue value)
              throws InvalidDataException
Finds the larger of this and the given data value.

This method is currently unimplemented.

Specified by:
max in interface DataValue
Parameters:
value - a data value to be compared with this data value
Returns:
the larger of the compared data values
Throws:
InvalidDataException - if the data value cannot be compared.
See Also:
min(com.davisor.data.DataValue)

min

public DataValue min(DataValue value)
              throws InvalidDataException
Finds the smaller of this and the given data value.

This method is currently unimplemented.

Specified by:
min in interface DataValue
Parameters:
value - a data value to be compared with this data value
Returns:
the smaller of the compared data values
Throws:
InvalidDataException - if the data value cannot be compared.
See Also:
max(com.davisor.data.DataValue)

mul

public DataValue mul(DataValue value)
              throws InvalidDataException
Multipies the current value with given value.

Specified by:
mul in interface DataValue
Parameters:
value - the multiplier
Returns:
a new data object, sharing the current type of this object.
Throws:
InvalidDataException - if multiplier is not a number.

negate

public DataValue negate()
                 throws InvalidDataException
Returns the negation of current value.

Specified by:
negate in interface DataValue
Returns:
a new data object, sharing the current type of this object
Throws:
InvalidDataException - if absolute value can not be computed

setValue

public void setValue(java.lang.String stringValue,
                     java.text.ParsePosition status)
              throws InvalidDataException
Sets data value from a string value.

Specified by:
setValue in interface DataValue
Parameters:
stringValue - string value to be parsed (may be null)
status - parse status (may be null)
Throws:
InvalidDataException - if string value could not be parser
See Also:
DateFormatType.dateValue(String,ParsePosition)

sub

public DataValue sub(DataValue value)
              throws InvalidDataException
Substracts given value from current value. The nature of the subtraction operation depends on given value type. In each case, the result will be returned in a time data object type best suited for to represent the result.

If the value is an instance of CalendarData, the current calendar field is converted to a calendar value, from which the other calendar value is then substracted from.

If the value is an instance of CalendarFieldData, and the field type is equal to current field, the field values are subtracted. If the field types are not equal, the current calendar field is converted to a calendar value, from which the other calendar value is then substracted from.

If the value is an instance of Date, the current calendar field is converted to a calendar value, from which the other calendar value is then substracted from.

In any other case, the operation fails.

Specified by:
sub in interface DataValue
Parameters:
value - a data value to be substracted from this data value
Returns:
a new data object, sharing the current type of this object
Throws:
InvalidDataException - if the data values cannot be substracted
See Also:
add(com.davisor.data.DataValue), DateFormatType.toData(java.util.Date)

toStatement

public void toStatement(java.sql.PreparedStatement statement,
                        int parameter)
                 throws java.sql.SQLException
Converts data to a prepared statement input parameter.

Specified by:
toStatement in interface DataValue
Throws:
java.sql.SQLException - if a SQL error occurs.

toXML

public java.lang.String toXML()
Gets the XML string representation of this value.

Specified by:
toXML in interface DataValue
Returns:
complete string representing the data.
See Also:
toString(), XMLUnicodeEncoder.encode(java.lang.String, boolean)

unit

public DataValue unit(DataValue targetValue,
                      double maxSteps)
               throws InvalidDataException
Gets the largest suitable unit step to represent a value change between this value and the given target value. If the values are equal, a type specific default unit step is returned. If the values are not equal, then the given target value must be reached from this value by adding the returned unit step more than once to this value.

Calendar field data objects are designed to represent time steps. Therefore, the result will be a new calendar field, equal to to the current value of this object. The given targetValue and maxSteps is ignored.

Specified by:
unit in interface DataValue
Parameters:
targetValue - target value to be reached (ignored)
maxSteps - maximum number of steps (zero leaves the choise open)
Returns:
a new data object, sharing the current type of this object
Throws:
InvalidDataException - if target value is unsuitable
See Also:
DateFormatType.toData(Object)

getTime

public long getTime()
Gets the duration of this calendar field in milliseconds.

See Also:
DateFormatType.createCalendar(CalendarField)

setTime

public void setTime(long time)
Sets the duration of this calendar date in milliseconds.


dup

public com.davisor.core.Dupable dup()
Makes a deep copy of this object.

Specified by:
dup in interface com.davisor.core.Dupable

toString

public java.lang.String toString()
Gets the string representation of this calendar date.

Specified by:
toString in interface DataValue

getField

public CalendarField getField()
Gets the calendar field.

Returns:
calendar field
See Also:
setField(com.davisor.data.CalendarField)

setField

public void setField(CalendarField field)
Sets the calendar field.

Parameters:
field - calendar field
See Also:
getField()

setValue

public void setValue(int value)
Sets the calendar field value.

Parameters:
value - calendar field value
See Also:
getField()


Copyright © 2001-2004 Davisor Oy. All Rights Reserved.