freemarker.template.expression
Class ExpressionUtils

java.lang.Object
  |
  +--freemarker.template.expression.ExpressionUtils

public final class ExpressionUtils
extends java.lang.Object

Utility methods for evaluating expressions. These are used in various parts of the freemarker.template.expression package.

Since:
1.7.5
Version:
$Id: ExpressionUtils.java,v 1.18 2003/11/10 05:09:18 run2000 Exp $
Author:
Nicholas Cull

Field Summary
static int EXPRESSION_TYPE_CONSTANT
          The expression is a constant.
static int EXPRESSION_TYPE_HASH
          The expression can return type Hash.
static int EXPRESSION_TYPE_LIST
          The expression can return type List.
static int EXPRESSION_TYPE_METHOD
          The expression can return type Method.
static int EXPRESSION_TYPE_NUMBER
          The expression can return type Number.
static int EXPRESSION_TYPE_STRING
          The expression can return type String.
static int EXPRESSION_TYPE_TRANSFORM
          The expression can return type Transform.
static int EXPRESSION_TYPE_VARIABLE
          The expression is a variable, so can return anything other than a constant.
 
Method Summary
static long compareNumbers(TemplateModel leftModel, TemplateModel rightModel)
          Compares two numeric expressions.
static long getAsNumber(TemplateModel model)
          Determines the given TemplateModel's long value.
static java.lang.String getAsString(TemplateModel model)
          Determines the given TemplateModel's String value.
static boolean isEqual(TemplateModel leftModel, TemplateModel rightModel)
          Determines whether both sides of an expression are equal.
static boolean isTrue(TemplateModel model)
          Determines the "truth" of a given template model.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

EXPRESSION_TYPE_CONSTANT

public static final int EXPRESSION_TYPE_CONSTANT
The expression is a constant.

EXPRESSION_TYPE_STRING

public static final int EXPRESSION_TYPE_STRING
The expression can return type String.

EXPRESSION_TYPE_NUMBER

public static final int EXPRESSION_TYPE_NUMBER
The expression can return type Number.

EXPRESSION_TYPE_LIST

public static final int EXPRESSION_TYPE_LIST
The expression can return type List.

EXPRESSION_TYPE_HASH

public static final int EXPRESSION_TYPE_HASH
The expression can return type Hash.

EXPRESSION_TYPE_TRANSFORM

public static final int EXPRESSION_TYPE_TRANSFORM
The expression can return type Transform.

EXPRESSION_TYPE_METHOD

public static final int EXPRESSION_TYPE_METHOD
The expression can return type Method.

EXPRESSION_TYPE_VARIABLE

public static final int EXPRESSION_TYPE_VARIABLE
The expression is a variable, so can return anything other than a constant.
Method Detail

isTrue

public static final boolean isTrue(TemplateModel model)
                            throws TemplateException

Determines the "truth" of a given template model. This is determined as follows:

Parameters:
model - the TemplateModel to be tested, possibly null
Returns:
true if the model evaluates to true, otherwise false
Throws:
TemplateException - the truth of the template model could not be determined

getAsString

public static final java.lang.String getAsString(TemplateModel model)
                                          throws TemplateException

Determines the given TemplateModel's String value. This is a simple case of getting the TemplateScalarModel.getAsString() value from the model, assuming it implements TemplateScalarModel. If it's a TemplateNumberModel, we return a base-10 encoding of the number value. If it's null, return null.

Parameters:
model - the TemplateModel to get the String value from, possibly null
Throws:
TemplateException - the String value of the template model could not be determined

getAsNumber

public static final long getAsNumber(TemplateModel model)
                              throws TemplateException

Determines the given TemplateModel's long value. This is a simple case of getting the TemplateNumberModel.getAsNumber() value from the model, assuming it implements TemplateNumberModel. If it's null, return 0.

Parameters:
model - the TemplateModel to get the long value from, possibly null
Throws:
TemplateException - the String value of the template model could not be determined

isEqual

public static final boolean isEqual(TemplateModel leftModel,
                                    TemplateModel rightModel)
                             throws TemplateException
Determines whether both sides of an expression are equal. Can deal with null values on either side of the test.
Parameters:
leftModel - the left-hand TemplateModel to be compared, possibly null
rightModel - the right-hand TemplateModel to be compared, possibly null
Returns:
true if the models are equal in value, otherwise false
Throws:
TemplateException - the template models could not be compared

compareNumbers

public static final long compareNumbers(TemplateModel leftModel,
                                        TemplateModel rightModel)
                                 throws TemplateException
Compares two numeric expressions. Can deal with null values on either side of the comparison.
Parameters:
leftModel - the left-hand TemplateModel to be compared, possibly null
rightModel - the right-hand TemplateModel to be compared, possibly null
Returns:
<0 if the left model is less than the right model, >0 if the left model is greater than the right model, or == 0 if the models are the same
Throws:
TemplateException - the template models could not be compared