edu.upc.dama.dex.core
Class Value

java.lang.Object
  extended by edu.upc.dama.dex.core.Value

public class Value
extends java.lang.Object

Typed value representation.

It stores a value and its type. Also, there is a special case which is the NULL value.

Author:
Sparsity Technologies

Field Summary
static short BOOL
          Boolean data type representation.
static short DOUBLE
          Double data type representation.
static short INT
          Integer data type representation.
static short LONG
          Long data type representation.
static short NULL
          NULL data type representation.
static short STRING
          String data type representation.
static int STRING_MAX_LENGTH
          Maximum length for String values.
static short TEXT
          Text data type representation.
static short TIMESTAMP
          Timestamp data type representation.
 
Constructor Summary
Value()
          Creates a new instance of Value.
Value(boolean v)
          Creates a new instance of Value.
Value(java.util.Calendar v)
          Creates a new instance of Value.
Value(double v)
          Creates a new instance of Value.
Value(int v)
          Creates a new instance of Value.
Value(long v)
          Creates a new instance of Value.
Value(java.lang.Object object)
          Creates a new instance of Value.
Value(java.lang.String v)
          Creates a new instance of Value.
Value(TextStream v)
          Creates a new instance of Value.
 
Method Summary
 short compare(Value value)
          Compares with the given value.
 boolean equals(java.lang.Object obj)
          Value value equals comparisson.
 java.lang.Object get()
          Gets the value.
 boolean getBool()
          Gets the value as a Boolean.
 double getDouble()
          Gets the value as a Double.
 int getInt()
          Gets the value as an Integer.
 long getLong()
          Gets the value as a Long.
 java.lang.String getString()
          Gets the value as a String.
 TextStream getTextStream()
          Gets the value as a TextStream.
 java.util.Calendar getTimestamp()
          Gets the value as a Calendar.
 long getTimestampValue()
          Gets the value as a Timestamp.
 short getType()
          Gets the data type.
static short getType(java.lang.String typename)
          Gets the short representation of a data type.
static java.lang.String getTypeName(short type)
          Gets a String representation of a data type.
 int hashCode()
          Gets the hash code of the Value
 boolean isNull()
          Gets whether the data type is Value.NULL or not.
 Value setBool(boolean value)
          Sets a Boolean value.
 Value setDouble(double value)
          Sets a Double value.
 Value setInt(int value)
          Sets an Integer value.
 Value setLong(long value)
          Sets a Long value.
 Value setNull()
          Sets a NULL value.
 Value setString(java.lang.String value)
          Sets a String value.
 Value setTextStream(TextStream tstrm)
          Sets a Text value.
 Value setTimestamp(java.util.Calendar value)
          Sets a Timestamp value.
 java.lang.String toString()
          Gets a string representation.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

STRING_MAX_LENGTH

public static final int STRING_MAX_LENGTH
Maximum length for String values.

See Also:
Constant Field Values

NULL

public static final short NULL
NULL data type representation.

See Also:
Constant Field Values

INT

public static final short INT
Integer data type representation.

See Also:
Constant Field Values

STRING

public static final short STRING
String data type representation.

See Also:
Constant Field Values

DOUBLE

public static final short DOUBLE
Double data type representation.

See Also:
Constant Field Values

BOOL

public static final short BOOL
Boolean data type representation.

See Also:
Constant Field Values

TIMESTAMP

public static final short TIMESTAMP
Timestamp data type representation.

See Also:
Constant Field Values

LONG

public static final short LONG
Long data type representation.

See Also:
Constant Field Values

TEXT

public static final short TEXT
Text data type representation.

See Also:
Constant Field Values
Constructor Detail

Value

public Value()
Creates a new instance of Value.

It creates a NULL value.

See Also:
Value.isNull()

Value

public Value(int v)
Creates a new instance of Value.

It creates an Integer value.

Parameters:
v - Assigned value.

Value

public Value(long v)
Creates a new instance of Value.

It creates a Long value.

Parameters:
v - Assigned value.

Value

public Value(java.lang.String v)
Creates a new instance of Value.

It creates a String value.

Parameters:
v - Assigned value.

Value

public Value(boolean v)
Creates a new instance of Value.

It creates a Boolean value.

Parameters:
v - Assigned value.

Value

public Value(double v)
Creates a new instance of Value.

It creates a Double value.

Parameters:
v - Assigned value.

Value

public Value(java.util.Calendar v)
Creates a new instance of Value.

It creates a Timestamp value.

Parameters:
v - Assigned value.

Value

public Value(TextStream v)
Creates a new instance of Value.

It creates a Text value.

Parameters:
v - Assgined value.

Value

public Value(java.lang.Object object)
Creates a new instance of Value.

It creates an instance from a Java Object. If object is an instance of Integer then it creates an Integer value, if it is an instance of Long then it creates a Long value, if it is an instance of Calendar then it creates a Timestamp value, and so on.

Parameters:
object - Assigned value.
Throws:
java.lang.IllegalArgumentException - if object is not a valid type
java.lang.IllegalArgumentException - if object is a string and ((String)object).length() grater than STRING_MAX_LENGTH
Method Detail

get

public java.lang.Object get()
Gets the value.

If it is a NULL value, then a null pointer is is returned.

Returns:
The value or null.

toString

public java.lang.String toString()
Gets a string representation.

If the type is Value.NULL, then a "null" pointer is returned.

Overrides:
toString in class java.lang.Object
Returns:
The string representation.

getType

public short getType()
Gets the data type.

The data type can be Value.NULL, Value.INT, Value.STRING, Value.DOUBLE, Value.BOOL, Value.TIMESTAMP, Value.LONG or Value.TEXT.

Returns:
The data type.

setNull

public Value setNull()
Sets a NULL value.

It changes the data type to Value.NULL and the value to null.

Returns:
The calling instance reference (this).

isNull

public boolean isNull()
Gets whether the data type is Value.NULL or not.

Returns:
true if the data type is Value.NULL, false otherwise.

setInt

public Value setInt(int value)
Sets an Integer value.

It changes the data type to Value.INT and the value to value.

Returns:
The calling instance reference (this).

getInt

public int getInt()
Gets the value as an Integer.

Returns:
An Integer value.
Throws:
java.lang.ClassCastException - if value type is not Value.INT

setString

public Value setString(java.lang.String value)
Sets a String value.

It changes the data type to Value.STRING and the value to value.

Returns:
The calling instance reference (this).
Throws:
java.lang.IllegalArgumentException - if value.length() grater than STRING_MAX_LENGTH

getString

public java.lang.String getString()
Gets the value as a String.

Unpredicted result if the data type of the value is not Value.STRING.

Returns:
A String value.

setDouble

public Value setDouble(double value)
Sets a Double value.

It changes the data type to Value.DOUBLE and the value to value.

Returns:
The calling instance reference (this).

getDouble

public double getDouble()
Gets the value as a Double.

Returns:
A Double value.
Throws:
java.lang.ClassCastException - if Value type is not Value.DOUBLE

setBool

public Value setBool(boolean value)
Sets a Boolean value.

It changes the data type to Value.BOOL and the value to value.

Returns:
The calling instance reference (this).

getBool

public boolean getBool()
Gets the value as a Boolean.

Returns:
A Boolean value.
Throws:
java.lang.ClassCastException - if type is not Value.BOOL

setTimestamp

public Value setTimestamp(java.util.Calendar value)
Sets a Timestamp value.

It changes the data type to Value.TIMESTAMP and the value to value.

Returns:
The calling instance reference (this).
See Also:
Calendar

getTimestamp

public java.util.Calendar getTimestamp()
Gets the value as a Calendar.

Returns:
A Calendar value.
Throws:
java.lang.ClassCastException - if Value type is not Value.TIMESTAMP
See Also:
Calendar

getTimestampValue

public long getTimestampValue()
Gets the value as a Timestamp.

Returns:
A Timestamp value.
Throws:
java.lang.ClassCastException - if Value type is not Value.TIMESTAMP
See Also:
Calendar.getTimeInMillis()

setLong

public Value setLong(long value)
Sets a Long value.

It changes the data type to Value.LONG and the value to value.

Returns:
The calling instance reference (this).

getLong

public long getLong()
Gets the value as a Long.

Returns:
A Long value.
Throws:
java.lang.ClassCastException - if type is not Value.LONG

setTextStream

public Value setTextStream(TextStream tstrm)
Sets a Text value.

It changes the data type to Value.TEXT and the value to value.

Returns:
The calling instance reference (this).
See Also:
TextStream

getTextStream

public TextStream getTextStream()
Gets the value as a TextStream.

Returns:
A TextStream value.
Throws:
java.lang.ClassCastException - if Value type is not Value.TEXT

compare

public short compare(Value value)
Compares with the given value.

It works if and only if two values have the same data type.

For boolean types, false is considered smaller than true. For text types, this operation has no sense so. Thus, if two values with a text data type are set to be compared, an IllegalStateException will be thrown.

Parameters:
value - Value to be compared to.
Returns:
-1 if the value of the calling instance is smaller than the given value, 0 if they are equal to each other and 1 if the value of the calling instance is greater than the given value.

getTypeName

public static java.lang.String getTypeName(short type)
Gets a String representation of a data type.

Parameters:
type - It must be Value.NULL, Value.INT, Value.STRING, Value.DOUBLE, Value.BOOL, Value.TIMESTAMP, or Value.LONG.
Returns:
A String representation of a data type.

getType

public static short getType(java.lang.String typename)
Gets the short representation of a data type.

The comparision of the given String is case insensitive.

Parameters:
typename - String data type representation.
Returns:
The short value of the data type.

hashCode

public int hashCode()
Gets the hash code of the Value

Overrides:
hashCode in class java.lang.Object
Returns:
The hash code of the value

equals

public boolean equals(java.lang.Object obj)
Value value equals comparisson.

Overrides:
equals in class java.lang.Object
Parameters:
obj - object to compare with
Returns:
true if both objects have the same type and valeu, false otherwise