cc.plural.jsonij
Class Value

java.lang.Object
  extended by cc.plural.jsonij.Value
All Implemented Interfaces:
Serializable, Comparable<Value>
Direct Known Subclasses:
ArrayImp, JSON.Boolean, JSON.Null, NumericImp, ObjectImp, StringImp

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

Value is extended by every JSON internalType implementation. This class provides generic access to all values to make life a little easier when traversing the JSON Document.

Version:
1.0.0
Author:
J.W.Marsden
See Also:
Serialized Form

Nested Class Summary
static class Value.TYPE
          Type enumeration.
 
Field Summary
protected  Value.TYPE valueType
          Current Value Type
 
Constructor Summary
Value()
          Constructor for Value.
 
Method Summary
 int compareTo(Value o)
           
 boolean equals(Object obj)
           
 Value get(CharSequence key)
          Gets a Value at a key for the current Value.
 Value get(int i)
          Extracts a Value instance at an Index.
 boolean getBoolean()
          Finds the boolean representation for the Value.
 double getDouble()
          Finds the double representation for the Value.
 int getInt()
          Finds the int representation for the Value.
 Number getNumber()
          Finds the Number representation for the Value.
 String getString()
          Finds the String representation for the Value.
 Value.TYPE getValueType()
          Accessor for the Value TYPE.
 boolean has(String key)
          Tests if there is a Value at the specified key.
 int hashCode()
           
protected abstract  Value.TYPE internalType()
          Internal Method to find the internalType for the Object.
 boolean isNull()
           
abstract  int nestedSize()
          Finds the nested elements under this Value.
 int size()
          Retrieves the size of the Value.
abstract  String toJSON()
          Converts the current Value into a JSON String that represents it.
 String toString()
          Default toString for a JSON Value.
 Value.TYPE type()
          Accessor for the Value TYPE.
 Set<CharSequence> valueKeySet()
           
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

valueType

protected Value.TYPE valueType
Current Value Type

Constructor Detail

Value

public Value()
Constructor for Value. As a Value constructs it must have a internalType or it cannot construct.

Method Detail

internalType

protected abstract Value.TYPE internalType()
Internal Method to find the internalType for the Object. Must be implemented and not return null.

Returns:
The internalType of this JSON Value.

getValueType

public Value.TYPE getValueType()
Accessor for the Value TYPE.

Returns:
TYPE The value valueType.

type

public Value.TYPE type()
Accessor for the Value TYPE.

Returns:
TYPE The value valueType.

size

public int size()
Retrieves the size of the Value. If string internalType, this will return the length of the String. If this internalType is an Array or an Object then it will return the number of elements in the Object. If this Value is not a String, Array or Object then this will return -1.

Returns:
size of the Value or -1 if this Value has no size.

nestedSize

public abstract int nestedSize()
Finds the nested elements under this Value. This is effectively the count of all JSON Values attached to this Value. This number does not include this value itself.

Returns:
int The count of all Values attached to this Value.

isNull

public boolean isNull()

getBoolean

public boolean getBoolean()
Finds the boolean representation for the Value. If the value is JSON.TRUE or JSON.FALSE then those booleans are returned. If the value is Numeric then true is returned for all values that are not zero. If the value is a String then an empty String returns 0. If the value is an Object or Array then all sizes that are not zero return true.

Returns:
boolean The boolean for the Value.

getInt

public int getInt()
Finds the int representation for the Value. Returns 1 when the value is JSON True and 0 when the value is JSON False. When the value is Numeric it will return the intValue from Number. If the value is a String then an attempt is made to parse the String value into an integer and return it. All other types return -1.

Returns:
int The int for the Value.

getDouble

public double getDouble()
Finds the double representation for the Value. Returns 1D when the value is JSON True and 0D when the value is JSON False. When the value is Numeric it will return the doubleValue from Number. If the value is a String then an attempt is made to parse the String value into an Double and return it. All other types return -1D.

Returns:
double The double value for the Value.

getNumber

public Number getNumber()
Finds the Number representation for the Value. Returns 1D when the value is JSON True and 0D when the value is JSON False. When the value is Numeric it will return the Number. If the value is a String then an attempt is made to parse the String value into an Double and return it. All other types return -1D.

Returns:
double The double value for the Value.

getString

public String getString()
Finds the String representation for the Value. When the value is true or false it will return "true" or "false" respectively. If the value is Numeric it will return the toString() version of the Number instance or the String itself. All other values return null.

Returns:
String The String value for the Value.

get

public Value get(int i)
Extracts a Value instance at an Index. This method only returns values when the internalType is OBJECT or ARRAY. All other cases will return null.

Parameters:
i - The index to get the value for.
Returns:
Value The Value at the index or null.

has

public boolean has(String key)
Tests if there is a Value at the specified key. This method only responds when the Value is of internalType OBJECT. All other cases will return false.

Parameters:
key - Key to extract the value from.
Returns:
true when there is a value at the key.

valueKeySet

public Set<CharSequence> valueKeySet()

get

public Value get(CharSequence key)
Gets a Value at a key for the current Value. This method only responds when the Value is of internalType OBJECT. All other cases will return null.

Parameters:
key - Key to extract the value from.
Returns:
Value The value at the given Key.

toJSON

public abstract String toJSON()
Converts the current Value into a JSON String that represents it.

Returns:
The JSON Value as JSON String.

toString

public String toString()
Default toString for a JSON Value. Returns the JSON string for the Value.

Overrides:
toString in class Object
Returns:

equals

public boolean equals(Object obj)
Overrides:
equals in class Object

hashCode

public int hashCode()
Overrides:
hashCode in class Object

compareTo

public int compareTo(Value o)
Specified by:
compareTo in interface Comparable<Value>


Copyright © 2012. All Rights Reserved.