com.codetaco.algebrain.function.FuncStringLength Maven / Gradle / Ivy
package com.codetaco.algebrain.function;
import java.text.ParseException;
import com.codetaco.algebrain.Function;
import com.codetaco.algebrain.ValueStack;
import com.codetaco.algebrain.token.TokVariable;
/**
*
* FuncStringLength class.
*
*
* @author Chris DeGreef [email protected]
* @since 1.3.9
*/
public class FuncStringLength extends Function
{
/**
*
* Constructor for FuncStringLength.
*
*/
public FuncStringLength()
{
super();
}
/**
*
* Constructor for FuncStringLength.
*
*
* @param var a {@link com.codetaco.algebrain.token.TokVariable} object.
*/
public FuncStringLength(final TokVariable var)
{
super(var);
}
/** {@inheritDoc} */
@Override
public void resolve(final ValueStack values) throws Exception
{
if (values.size() < getParameterCount())
throw new Exception("missing operands for " + toString());
try
{
final Object target = values.popStringOrByteArray();
if (target instanceof String)
values.push(new Double(((String) target).length()));
else
values.push(new Double(((byte[]) target).length));
} catch (final ParseException e)
{
e.fillInStackTrace();
throw new Exception(toString() + "; " + e.getMessage(), e);
}
}
/** {@inheritDoc} */
@Override
public String toString()
{
return "function(length)";
}
}