freemarker.ext.misc
Class AsNumber

java.lang.Object
  |
  +--freemarker.ext.misc.AsNumber
All Implemented Interfaces:
TemplateMethodModel2, TemplateModel

public final class AsNumber
extends java.lang.Object
implements TemplateMethodModel2

Performs a type conversion from a String model to a number model. This is useful when we want to perform numeric operations on a value that was introduced as a scalar value, or when we only want a number from a multimodel.

Usage:
From java:

 TemplateModelRoot root = new SimpleHash();

 root.put( "asNumber", freemarker.ext.misc.AsNumber.getInstance() );

 ...
 

From your FM-Classic template:

 <assign var1 = "5">
 <assign var2 = "3">
 <assign var3 = asNumber( var1 ) + asNumber( var2 )>
 <p>The sum of ${var1} and ${var2} is: ${var3}</p>

 ...
 

Note:
AsNumber is a singleton instance. Use the getInstance() method to retrieve instances of this model.

Since:
1.8
Version:
$Id: AsNumber.java,v 1.6 2003/11/17 13:06:24 run2000 Exp $
Author:
Nicholas Cull

Method Summary
 TemplateModel exec(java.util.List arguments)
          Performs a String to number conversion using the rules that Long.parseLong() uses.
static AsNumber getInstance()
          Retrieve a singleton instance of this class.
 boolean isEmpty()
          Is this model empty?
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getInstance

public static AsNumber getInstance()
Retrieve a singleton instance of this class. Since there is no state information held between calls, there is only a need for one instance.
Returns:
an instance of this AsNumber class

exec

public TemplateModel exec(java.util.List arguments)
                   throws TemplateModelException
Performs a String to number conversion using the rules that Long.parseLong() uses.
Specified by:
exec in interface TemplateMethodModel2
Parameters:
arguments - a List of exactly one TemplateScalarModel to be converted
Returns:
a new TemplateNumberModel of the converted String
Throws:
TemplateModelException - More than one argument is supplied, or the model is neither a number nor a string model.

isEmpty

public boolean isEmpty()
                throws TemplateModelException
Is this model empty?
Specified by:
isEmpty in interface TemplateModel
Returns:
false to indicate that this model is non-empty