at.spardat.xma.mdl
Class Atom

java.lang.Object
  extended byat.spardat.xma.mdl.Atom
All Implemented Interfaces:
java.lang.Comparable, Descriptive, IAtomic

public class Atom
extends java.lang.Object
implements java.lang.Comparable, Descriptive, IAtomic

This class is an immutable implementation of the IAtomic interface. Sinces is immutable, this class provides a set of type specific constructors and a set of static objects that may be used for empty Atom of the various types.


Field Summary
static Atom EMPTY_BCD
          Empty Atom of type T_BCD
static Atom EMPTY_BOOLEAN
          Empty Atom of type T_BOOLEAN
static Atom EMPTY_DATE
          Empty Atom of type T_DATE
static Atom EMPTY_DOM
          Empty Atom of type T_DOM
static Atom EMPTY_STRING
          Empty Atom of type T_STRING
static Atom EMPTY_TIMESTAMP
          Empty Atom of type T_TIMESTAMP
protected  byte type_
          One of the type codes in enterprise.util.Types.
protected  java.lang.String value_
          A string encoded value of a particular type.
 
Constructor Summary
Atom()
          Constructor for internal use.
Atom(java.math.BigDecimal value)
          Constructs an T_BCD Atom from an BigDecimal.
Atom(boolean value)
          Constructs a T_BOOLEAN Atom with the provided value.
Atom(byte type, java.util.Date date)
          Constructs a T_DATE or T_TIMESTAMP Atom.
Atom(double value)
          Constructs a T_BCD Atom from a double.
Atom(double value, int numNK)
          Constructs a T_BCD Atom.
Atom(int value)
          Constructs a T_BCD Atom from an int.
Atom(long value)
          Constructs a T_BCD Atom from a long.
Atom(java.lang.String s)
          Constructs an Atom with type T_STRING and the provided value.
Atom(java.sql.Timestamp timeStamp)
          Constructs a T_TIMESTAMP Atom.
 
Method Summary
 Atom clear()
          Returns an empty Atom having the same type as this.
 int compareTo(java.lang.Object o)
           
 void describe(DNode n)
          describe puts all relevant information about this into the provided node.
 boolean equals(java.lang.Object obj)
           
 int estimateMemory()
          Estimates the number of bytes this object consumes in memory.
static void externalize(Atom a, XmaOutput o)
          Writes the state to an output stream.
 java.lang.String getEncodedValue()
          Returns the String encoded value of this.
 byte getType()
          Returns the type constant.
 int hashCode()
           
 boolean hasValue()
          Returns true if this Atom holds a value.
static Atom internalize(XmaInput i)
          Reads an Atom from an input stream and returns it.
 boolean isTrue()
          Returns true if getType() equals T_BOOLEAN and the stored value equals TRUE.
static Atom newInstance(byte type, java.lang.String internalValue)
          Constructs an atom by providing its instance variables.
static Atom newInstance(java.lang.Object o)
          Constructs an atom from various java types.
static Atom newRandomInstance()
          Randomly contructs an Atom and returns it.
static Atom newTransportInstance(java.lang.String encoded)
          Constructs an Atom from a String that has been produced using toTransportString.
static int streamedSize(Atom a)
          Returns the estimated length of the byte stream resulting from streaming an Atom.
 java.math.BigDecimal toBigDecimal()
          If the type of this is T_BCD, the numeric value is returned as a BigDecimal.
 byte toByte()
          Returns the value of this as byte.
 java.lang.Byte toBYTE()
          Returns the value of this as Byte or null if !
 java.util.Date toDate()
          Returns a newly constructed java.util.Date object representing the value of this.
static java.util.Date toDateImpl(byte type, java.lang.String internal)
          Helper method to convert date- and timestamp-atomics to java.util.Date.
 double toDouble()
          Extracts a double from this.
 java.lang.Double toDOUBLE()
          Returns the value of this as Double or null if !
 float toFloat()
          Extracts a float from this.
 java.lang.Float toFLOAT()
          Returns the value of this as Float or null if !
 int toInt()
          Requires that this is either empty or contains an integer T_BCD value.
 java.lang.Integer toINTEGER()
          Returns the value of this as Integer or null if !
 long toLong()
          Returns the value of this as long.
 java.lang.Long toLONG()
          Returns the value of this as Long or null if !
 short toShort()
          Returns the value of this as short.
 java.lang.Short toSHORT()
          Returns the value of this as Short or null if !
 java.lang.String toString()
          Returns a String representing the value of this.
 java.lang.String toString(at.spardat.enterprise.fmt.IFmt formatter)
          Maps this Atom to string representation using a IFmt object.
static java.lang.String toStringImpl(byte type, java.lang.String internal)
          Returns a string representation of an IAtomic given its type and its internal encoding.
 java.lang.String toTransportString()
          Converts this to a transport encoding string s that has the property that an Atom created with newTransportInstance(s) is equal to this.
static java.lang.String type2String(byte t)
          Returns a describing short string for a type code.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

EMPTY_STRING

public static final Atom EMPTY_STRING
Empty Atom of type T_STRING


EMPTY_BCD

public static final Atom EMPTY_BCD
Empty Atom of type T_BCD


EMPTY_DATE

public static final Atom EMPTY_DATE
Empty Atom of type T_DATE


EMPTY_TIMESTAMP

public static final Atom EMPTY_TIMESTAMP
Empty Atom of type T_TIMESTAMP


EMPTY_BOOLEAN

public static final Atom EMPTY_BOOLEAN
Empty Atom of type T_BOOLEAN


EMPTY_DOM

public static final Atom EMPTY_DOM
Empty Atom of type T_DOM


type_

protected byte type_
One of the type codes in enterprise.util.Types.


value_

protected java.lang.String value_
A string encoded value of a particular type. This instance is never null.

Constructor Detail

Atom

public Atom()
Constructor for internal use. Do not call it!!!!


Atom

public Atom(java.lang.String s)
Constructs an Atom with type T_STRING and the provided value.

Parameters:
s - the value to set
Throws:
java.lang.IllegalArgumentException - if argument null

Atom

public Atom(double value,
            int numNK)
Constructs a T_BCD Atom.

Parameters:
value - the numeric value to set.
numNK - number of digits after the comma to be used when the provided double is converted to a String (which has to be done since Atoms are string encoded). If numNK equals -1, the number of the resulting places after the comma is unknown.

Atom

public Atom(double value)
Constructs a T_BCD Atom from a double. The resulting value of this is empty if value is NaN or Infinity. If the provided value cannot trivially be converted to a string, because its numeric (binary coded) value would result in a very long (decimally coded) String, digits after the comma are reduced so that the total number of significant digits does not exceed 15 digits.

Parameters:
value - the input double value

Atom

public Atom(java.math.BigDecimal value)
Constructs an T_BCD Atom from an BigDecimal.

Parameters:
value - the BigDecimal providing the numeric value.

Atom

public Atom(int value)
Constructs a T_BCD Atom from an int. The resulting value of this is never empty.

Parameters:
value -

Atom

public Atom(long value)
Constructs a T_BCD Atom from a long. The resulting value of this is never empty.

Parameters:
value -

Atom

public Atom(byte type,
            java.util.Date date)
Constructs a T_DATE or T_TIMESTAMP Atom.

Parameters:
type - may be T_DATE or T_TIMESTAMP
date - the value of the Atom
Throws:
java.lang.IllegalArgumentException - if argument null

Atom

public Atom(java.sql.Timestamp timeStamp)
Constructs a T_TIMESTAMP Atom.

Parameters:
timeStamp - the value of the Atom
Throws:
java.lang.IllegalArgumentException - if argument null

Atom

public Atom(boolean value)
Constructs a T_BOOLEAN Atom with the provided value.

Parameters:
value - the boolean value to construct this with.
Method Detail

newInstance

public static Atom newInstance(byte type,
                               java.lang.String internalValue)
Constructs an atom by providing its instance variables.

You may only use this method if you are working with formatters at.spardat.enterprise.fmt.IFmt and need to construct an Atom from an formatter-internal encoding.

Parameters:
type - the type code
internalValue - the internally stored string encoding of the type.
Returns:
newly created Atom

newInstance

public static Atom newInstance(java.lang.Object o)
Constructs an atom from various java types.

Parameters:
o - must be of the types String, Boolean, Folat, Double, Date, Byte, Short, Integer, Long
Returns:
Atom of type T_STRING, T_BOOLEAN, T_BCD or T_DATE
Throws:
java.lang.IllegalArgumentException - if Object is of unsupported type

newRandomInstance

public static Atom newRandomInstance()
Randomly contructs an Atom and returns it.


toTransportString

public java.lang.String toTransportString()
Converts this to a transport encoding string s that has the property that an Atom created with newTransportInstance(s) is equal to this.

Returns:
a string in a transport encoding. If the type of this is T_STRING, the value is delimited with double quotes. Other types than T_STRING start with a single character indicating the type and a type specific string encoding. The returned string (within the quotes) does not contain quotes, commas or semicolons.

newTransportInstance

public static Atom newTransportInstance(java.lang.String encoded)
Constructs an Atom from a String that has been produced using toTransportString.

Parameters:
encoded - as defined in method toTransportString
Returns:
newly created Atom.
Throws:
java.lang.IllegalArgumentException - if the argument was not created by toTransportString.

clear

public Atom clear()
Returns an empty Atom having the same type as this.


hasValue

public boolean hasValue()
Returns true if this Atom holds a value.

Specified by:
hasValue in interface IAtomic
Returns:
true if this has a non empty value.

getType

public byte getType()
Returns the type constant.

Specified by:
getType in interface IAtomic
Returns:
the type constant.

toString

public java.lang.String toString()
Description copied from interface: IAtomic
Returns a String representing the value of this. Depending on the type of this, the following formats are returned:
 T_BOOLEAN     "J" or "N"
 T_STRING      the string itself
 T_BCD         -12000.32
 T_DATE        yyyyMMdd
               example: 20001231, the last day in the year 2000
 T_TIMESTAMP   yyyyMMdd HH:mm:ss.SSS z
               example: 20001231 23:59:59.999 CET denoting the last millisecond in the year 2000, central european time.
 T_DOM         key of the domain value
 
The empty string is returned if this does not store a value.

Specified by:
toString in interface IAtomic
See Also:
IAtomic.toString()

toStringImpl

public static java.lang.String toStringImpl(byte type,
                                            java.lang.String internal)
Returns a string representation of an IAtomic given its type and its internal encoding.

This method may not be called from outside the framework.


toString

public java.lang.String toString(at.spardat.enterprise.fmt.IFmt formatter)
Maps this Atom to string representation using a IFmt object. The provided formatter is only used if it is type compatible with the type of this Atom. Otherwise, or if formatter is null, the internal String encoding is returned.

Specified by:
toString in interface IAtomic
Parameters:
formatter - the IFmt formatter. May be null.
Returns:
non null String.

toDouble

public double toDouble()
Extracts a double from this. Returns 0.0 if getType() is not equal to T_BCD or !hasValue().

Specified by:
toDouble in interface IAtomic
Returns:
the double value of this

toFloat

public float toFloat()
Description copied from interface: IAtomic
Extracts a float from this. Returns 0.0 if getType() is not equal to T_BCD or !hasValue().

Specified by:
toFloat in interface IAtomic
Returns:
the float value of this
See Also:
IAtomic.toFloat()

toInt

public int toInt()
Requires that this is either empty or contains an integer T_BCD value. If !hasValue() or the type of this is not T_BCD, zero is returned.

Specified by:
toInt in interface IAtomic
Returns:
the contained integer
Throws:
java.lang.NumberFormatException - if this does not fit into an int

toLong

public long toLong()
Description copied from interface: IAtomic
Returns the value of this as long. Requires that this is either empty or contains an T_BCD value. If !hasValue() or the type of this is not T_BCD, zero is returned.

Specified by:
toLong in interface IAtomic
Returns:
the contained value as long
See Also:
IAtomic.toLong()

toByte

public byte toByte()
Description copied from interface: IAtomic
Returns the value of this as byte. Requires that this is either empty or contains an T_BCD value. If !hasValue() or the type of this is not T_BCD, zero is returned.

Specified by:
toByte in interface IAtomic
Returns:
the contained value as long
See Also:
IAtomic.toByte()

toShort

public short toShort()
Description copied from interface: IAtomic
Returns the value of this as short. Requires that this is either empty or contains an T_BCD value. If !hasValue() or the type of this is not T_BCD, zero is returned.

Specified by:
toShort in interface IAtomic
Returns:
the contained value as long
See Also:
IAtomic.toShort()

toBigDecimal

public java.math.BigDecimal toBigDecimal()
Description copied from interface: IAtomic
If the type of this is T_BCD, the numeric value is returned as a BigDecimal. If the type is another than T_BCD or !hasValue() is true, then null is returned.

Specified by:
toBigDecimal in interface IAtomic
See Also:
IAtomic.toBigDecimal()

toBYTE

public java.lang.Byte toBYTE()
Description copied from interface: IAtomic
Returns the value of this as Byte or null if !hasValue() or the type is not T_BCD.

Specified by:
toBYTE in interface IAtomic
See Also:
IAtomic.toByte()

toSHORT

public java.lang.Short toSHORT()
Description copied from interface: IAtomic
Returns the value of this as Short or null if !hasValue() or the type is not T_BCD.

Specified by:
toSHORT in interface IAtomic
See Also:
IAtomic.toSHORT()

toINTEGER

public java.lang.Integer toINTEGER()
Description copied from interface: IAtomic
Returns the value of this as Integer or null if !hasValue() or the type is not T_BCD.

Specified by:
toINTEGER in interface IAtomic
See Also:
IAtomic.toINTEGER()

toLONG

public java.lang.Long toLONG()
Description copied from interface: IAtomic
Returns the value of this as Long or null if !hasValue() or the type is not T_BCD.

Specified by:
toLONG in interface IAtomic
See Also:
IAtomic.toLONG()

toFLOAT

public java.lang.Float toFLOAT()
Description copied from interface: IAtomic
Returns the value of this as Float or null if !hasValue() or the type is not T_BCD.

Specified by:
toFLOAT in interface IAtomic
See Also:
IAtomic.toFLOAT()

toDOUBLE

public java.lang.Double toDOUBLE()
Description copied from interface: IAtomic
Returns the value of this as Double or null if !hasValue() or the type is not T_BCD.

Specified by:
toDOUBLE in interface IAtomic
See Also:
IAtomic.toDOUBLE()

toDate

public java.util.Date toDate()
Returns a newly constructed java.util.Date object representing the value of this.

Specified by:
toDate in interface IAtomic
Returns:
null if the type is not equal to T_DATE or T_TIMESTAMP or !hasValue().

toDateImpl

public static java.util.Date toDateImpl(byte type,
                                        java.lang.String internal)
Helper method to convert date- and timestamp-atomics to java.util.Date. This is a framework-internal method and is not intended to be called from outside.


isTrue

public boolean isTrue()
Returns true if getType() equals T_BOOLEAN and the stored value equals TRUE. If there is no value stored or the stored value is not TRUE, false is returned.

Specified by:
isTrue in interface IAtomic
Returns:
boolean indicating the value in this.

streamedSize

public static int streamedSize(Atom a)
Returns the estimated length of the byte stream resulting from streaming an Atom.


internalize

public static Atom internalize(XmaInput i)
                        throws java.io.IOException
Reads an Atom from an input stream and returns it. The returned Atom may be null.

Throws:
java.io.IOException

externalize

public static void externalize(Atom a,
                               XmaOutput o)
                        throws java.io.IOException
Writes the state to an output stream. a may be null.

Throws:
java.io.IOException

compareTo

public int compareTo(java.lang.Object o)
Specified by:
compareTo in interface java.lang.Comparable
See Also:
Comparable.compareTo(java.lang.Object)

type2String

public static java.lang.String type2String(byte t)
Returns a describing short string for a type code.


describe

public void describe(DNode n)
Description copied from interface: Descriptive
describe puts all relevant information about this into the provided node. The implementing method may construct subnodes of node.

Specified by:
describe in interface Descriptive
Parameters:
n - the node going to hold information about this.
See Also:
Descriptive.describe(at.spardat.xma.mdl.util.DNode)

equals

public boolean equals(java.lang.Object obj)
See Also:
Object.equals(java.lang.Object)

estimateMemory

public int estimateMemory()
Estimates the number of bytes this object consumes in memory.


getEncodedValue

public java.lang.String getEncodedValue()
Description copied from interface: IAtomic
Returns the String encoded value of this. Returns the empty String if !hasValue().

Specified by:
getEncodedValue in interface IAtomic
See Also:
IAtomic.getEncodedValue()

hashCode

public int hashCode()
See Also:
Object.hashCode()