csheets.core
Class Value

java.lang.Object
  extended by csheets.core.Value
All Implemented Interfaces:
Serializable, Comparable<Value>

public class Value
extends Object
implements Comparable<Value>, Serializable

A typed value that a cell can contain.

Author:
Einar Pehrson
See Also:
Serialized Form

Nested Class Summary
static class Value.Type
          The recognized types of values
 
Constructor Summary
Value()
          Creates a null value.
Value(Boolean booleanValue)
          Creates a boolean value.
Value(Date date)
          Creates a date value.
Value(Number number)
          Creates a numeric value.
Value(String text)
          Creates a text value.
Value(Throwable error)
          Creates an error value.
Value(Value[] matrix)
          Creates a one-dimensional matrix value (vector).
Value(Value[][] matrix)
          Creates a two-dimensional matrix value.
 
Method Summary
 int compareTo(Value otherValue)
          Compares this value with the given value for order.
 boolean equals(Object other)
          Returns whether the other object is an identical value .
 Value.Type getType()
          Returns the type of the value.
 boolean isOfType(Value.Type type)
          Returns whether the value is of the given type.
static Value parseBooleanValue(String value)
          Attempts to parse a boolean from the given string.
static Value parseDateValue(String value)
          Attempts to parse a date, time or date/time from the given string.
static Value parseNumericValue(String value)
          Attempts to parse a number from the given string.
static Value parseValue(String value, Value.Type... types)
          Attempts to parse a value from the given string.
 Object toAny()
          Returns the value in untyped form.
 Boolean toBoolean()
          Returns a boolean representation of the value.
 Date toDate()
          Returns a date representation of the value.
 double toDouble()
          Returns a primitive numeric representation of the value.
 Throwable toError()
          Returns an error representation of the value.
 Value[][] toMatrix()
          Returns a matrix representation of the value.
 Number toNumber()
          Returns a numeric representation of the value.
 String toString()
          Returns a string representation of the value.
 String toString(Format format)
          Returns a string representation of the value, using the given date or number format.
 String toText()
          Returns a text representation of the value.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Value

public Value()
Creates a null value.


Value

public Value(Number number)
Creates a numeric value.

Parameters:
number - the number of the value

Value

public Value(String text)
Creates a text value.

Parameters:
text - the text of the value

Value

public Value(Boolean booleanValue)
Creates a boolean value.

Parameters:
booleanValue - the boolean of the value

Value

public Value(Date date)
Creates a date value.

Parameters:
date - the date of the value

Value

public Value(Value[] matrix)
Creates a one-dimensional matrix value (vector).

Parameters:
matrix - the value vector

Value

public Value(Value[][] matrix)
Creates a two-dimensional matrix value.

Parameters:
matrix - the value matrix

Value

public Value(Throwable error)
Creates an error value.

Parameters:
error - the error of the value
Method Detail

toAny

public final Object toAny()
Returns the value in untyped form.

Returns:
the value

getType

public final Value.Type getType()
Returns the type of the value.

Returns:
the type of the value

isOfType

public final boolean isOfType(Value.Type type)
Returns whether the value is of the given type.

Parameters:
type - the type of value to check against
Returns:
whether the value is of the given type

toNumber

public Number toNumber()
                throws IllegalValueTypeException
Returns a numeric representation of the value.

Returns:
a numeric representation of the value
Throws:
IllegalValueTypeException - if the value cannot be converted to this type

toDouble

public double toDouble()
                throws IllegalValueTypeException
Returns a primitive numeric representation of the value.

Returns:
a primitive numeric representation of the value
Throws:
IllegalValueTypeException - if the value cannot be converted to this type

toText

public String toText()
              throws IllegalValueTypeException
Returns a text representation of the value.

Returns:
a text representation of the value
Throws:
IllegalValueTypeException - if the value cannot be converted to this type

toBoolean

public Boolean toBoolean()
                  throws IllegalValueTypeException
Returns a boolean representation of the value.

Returns:
a boolean representation of the value
Throws:
IllegalValueTypeException - if the value cannot be converted to this type

toDate

public Date toDate()
            throws IllegalValueTypeException
Returns a date representation of the value.

Returns:
a date representation of the value
Throws:
IllegalValueTypeException - if the value cannot be converted to this type

toMatrix

public Value[][] toMatrix()
                   throws IllegalValueTypeException
Returns a matrix representation of the value.

Returns:
a matrix representation of the value
Throws:
IllegalValueTypeException - if the value cannot be converted to this type

toError

public Throwable toError()
                  throws IllegalValueTypeException
Returns an error representation of the value.

Returns:
an error representation of the value
Throws:
IllegalValueTypeException - if the value cannot be converted to this type

compareTo

public int compareTo(Value otherValue)
Compares this value with the given value for order.

Specified by:
compareTo in interface Comparable<Value>
Parameters:
otherValue - the value to compare to
Returns:
a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.

equals

public boolean equals(Object other)
Returns whether the other object is an identical value .

Overrides:
equals in class Object
Parameters:
other - the object to check for equality
Returns:
true if the objects are equal

toString

public String toString()
Returns a string representation of the value.

Overrides:
toString in class Object
Returns:
a string representation of the value

toString

public String toString(Format format)
Returns a string representation of the value, using the given date or number format.

Parameters:
format - the format to use when converting the value
Returns:
a string representation of the value

parseValue

public static Value parseValue(String value,
                               Value.Type... types)
Attempts to parse a value from the given string. The value is matched against the given types in order. If no types are supplied, conversion will be attempted to boolean, date and numeric values. If no other type matches, the value will be used as a string.

Parameters:
value - the value
types - the types for which parsing should be attempted

parseNumericValue

public static Value parseNumericValue(String value)
                               throws ParseException
Attempts to parse a number from the given string.

Parameters:
value - the value
Returns:
the numeric value that was found
Throws:
IllegalValueTypeException - if no numeric value was found
ParseException

parseBooleanValue

public static Value parseBooleanValue(String value)
                               throws ParseException
Attempts to parse a boolean from the given string.

Parameters:
value - the value
Returns:
the boolean value that was found
Throws:
IllegalValueTypeException - if no boolean value was found
ParseException

parseDateValue

public static Value parseDateValue(String value)
                            throws ParseException
Attempts to parse a date, time or date/time from the given string.

Parameters:
value - the value
Returns:
the date value that was found
Throws:
IllegalValueTypeException - if no date value was found
ParseException