com.google.template.soy.data
Class SoyData

java.lang.Object
  extended by com.google.template.soy.data.SoyData
Direct Known Subclasses:
com.google.template.soy.data.restricted.CollectionData, SanitizedContent

public abstract class SoyData
extends Object

Abstract base class for all nodes in a Soy data tree.


Constructor Summary
SoyData()
           
 
Method Summary
 boolean booleanValue()
          Precondition: Only call this method if you know that this SoyData object is a boolean.
static SoyData createFromExistingData(Object obj)
          Creation function for creating a SoyData object out of any existing primitive, data object, or data structure.
static SoyData createFromExistingData(String str)
          A special case of createFromExistingData(Object).
abstract  boolean equals(Object other)
          Compares this data object against another for equality in the sense of the operator '==' for Soy expressions.
 double floatValue()
          Precondition: Only call this method if you know that this SoyData object is a float.
 int integerValue()
          Precondition: Only call this method if you know that this SoyData object is an integer.
 double numberValue()
          Precondition: Only call this method if you know that this SoyData object is a number.
 String stringValue()
          Precondition: Only call this method if you know that this SoyData object is a string.
abstract  boolean toBoolean()
          Converts this data object into a boolean (e.g.
abstract  String toString()
          Converts this data object into a string (e.g.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

SoyData

public SoyData()
Method Detail

createFromExistingData

public static SoyData createFromExistingData(Object obj)
Creation function for creating a SoyData object out of any existing primitive, data object, or data structure.

Important: Avoid using this function if you know the type of the object at compile time. For example, if the object is a primitive, it can be passed directly to methods such as SoyMapData.put() or SoyListData.add(). If the object is a Map or an Iterable, you can directly create the equivalent SoyData object using the constructor of SoyMapData or SoyListData.

If the given object is already a SoyData object, then it is simply returned. Otherwise a new SoyData object will be created that is equivalent to the given primitive, data object, or data structure (even if the given object is null!).

Note that in order for the conversion process to succeed, the given data structure must correspond to a valid SoyData tree. Some requirements include: (a) all Maps within your data structure must have string keys that are identifiers, (b) all non-leaf nodes must be Maps or Lists, (c) all leaf nodes must be null, boolean, int, double, or String (corresponding to Soy primitive data types null, boolean, integer, float, string).

Parameters:
obj - The existing object or data structure to convert.
Returns:
A SoyData object or tree that corresponds to the given object.
Throws:
SoyDataException - If the given object cannot be converted to SoyData.

createFromExistingData

public static SoyData createFromExistingData(String str)
A special case of createFromExistingData(Object).


toString

public abstract String toString()
Converts this data object into a string (e.g. when used in a string context).

Overrides:
toString in class Object
Returns:
The value of this data object if coerced into a string.

toBoolean

public abstract boolean toBoolean()
Converts this data object into a boolean (e.g. when used in a boolean context). In other words, this method tells whether this object is truthy.

Returns:
The value of this data object if coerced into a boolean. I.e. true if this object is truthy, false if this object is falsy.

equals

public abstract boolean equals(Object other)
Compares this data object against another for equality in the sense of the operator '==' for Soy expressions.

Overrides:
equals in class Object
Parameters:
other - The other data object to compare against.
Returns:
True if the two objects are equal.

booleanValue

public boolean booleanValue()
Precondition: Only call this method if you know that this SoyData object is a boolean. This method gets the boolean value of this boolean object.

Returns:
The boolean value of this boolean object.
Throws:
SoyDataException - If this object is not actually a boolean.

integerValue

public int integerValue()
Precondition: Only call this method if you know that this SoyData object is an integer. This method gets the integer value of this integer object.

Returns:
The integer value of this integer object.
Throws:
SoyDataException - If this object is not actually an integer.

floatValue

public double floatValue()
Precondition: Only call this method if you know that this SoyData object is a float. This method gets the float value of this float object.

Returns:
The float value of this float object.
Throws:
SoyDataException - If this object is not actually a float.

numberValue

public double numberValue()
Precondition: Only call this method if you know that this SoyData object is a number. This method gets the float value of this number object (converting integer to float if necessary).

Returns:
The float value of this number object.
Throws:
SoyDataException - If this object is not actually a number.

stringValue

public String stringValue()
Precondition: Only call this method if you know that this SoyData object is a string. This method gets the string value of this string object.

Returns:
The string value of this string object.
Throws:
SoyDataException - If this object is not actually a string.