Class PostfixMathCommand

java.lang.Object
com.singularsys.jep.functions.PostfixMathCommand
All Implemented Interfaces:
PostfixMathCommandI, Serializable
Direct Known Subclasses:
AbstractInc, AlternateFunctions, ArrayFunctionBase, Assign, BinaryFunction, Case, If, JepTest.AddFractionsStack, JepTest.Diff, JepTest.PiFun, JepTest.Product, JepTest.Square, LazyLogical, List, MacroFunction, NaryBinaryFunction, NaryFunction, NullaryFunction, NullWrappedLazyLogical, NullWrappedPfmc, NullWrapTest.MyPfmc, OpEquals, OverloadedFunctionByStructure, OverloadedNaryFunctionTest.StringSelect, Sum, Switch, SwitchDefault, TernaryConditional, UnaryFunction

public abstract class PostfixMathCommand extends Object implements PostfixMathCommandI
Abstract base classes for most function classes. It is an implementation of the PostfixMathCommandI interface.

It includes a numberOfParameters member, that is checked when parsing the expression. This member should be initialized to an appropriate value for all classes extending this class. If an arbitrary number of parameters should be allowed, initialize this member to -1.

The asString(int, java.lang.Object) and similar method are convenience methods for use by subclasses to ease conversion of arguments to particular types.

See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected int
    Number of parameters to be used for the next run() invocation.
    protected String
    The description of the function.
    The name of the function.
    static final Double
    Double.valueOf(Double.NaN)
    protected int
    Number of parameters a function requires.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a new PostfixMathCommand class.
    PostfixMathCommand(int nParam)
    Creates a new PostfixMathCommand class.
  • Method Summary

    Modifier and Type
    Method
    Description
    protected Object[]
    Pop the arguments off the stack and put them in an array
    protected boolean
    asBool(int pos, Object value, boolean allowNumbers)
    Attempt to convert argument to a boolean
    protected double
    asDouble(int position, Object value)
    Converts an argument to a double, with error checking, floating point values will be rounded.
    protected int
    asInt(int position, Object value)
    Converts an argument to an int, floating point values will be rounded.
    protected long
    asLong(int position, Object value)
    Converts an argument to a long, with error checking, floating point values will be rounded.
    protected int
    asStrictInt(int position, Object value)
    Converts an argument to an int, floating point values must represent integers.
    protected String
    asString(int position, Object value)
    Converts an argument to a string, with error checking.
    protected <T> T
    asType(Class<T> type, int position, Object value)
    Converts an argument to a particular type, with error checking.
    boolean
    Checks the number of parameters of the function.
    Return a description of the function from the properties file.
    protected String
    Return a description of the function from the resource bundle using JepMessages.getStringFromAdditionalBundles(String) and a property name.
    Get a descriptions from a property with the format class.name.type.description
    The name of the function used in error messages
    int
    Return the required number of parameters.
    void
    Throws an exception because this method should never be called under normal circumstances.
    void
    Sets the number of current number of parameters used in the next call of run().
    void
    setDescription(String description)
    Set the description of the function.
    void
    Sets the name of the function used for error messages
    Return a string representation of the function.
    protected String
    toString(Object... args)
    Return a string representation of the function with its arguments.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Field Details

    • NaN

      public static final Double NaN
      Double.valueOf(Double.NaN)
    • numberOfParameters

      protected int numberOfParameters
      Number of parameters a function requires. Initialize this value to -1 if any number of parameters should be allowed.
    • curNumberOfParameters

      protected transient int curNumberOfParameters
      Number of parameters to be used for the next run() invocation. Applies only if the required number of parameters is variable (numberOfParameters = -1).
    • name

      public String name
      The name of the function. This is set in the FunctionTable#addFunction(String, PostfixMathCommandI) method.
    • description

      protected String description
      The description of the function. If set by setDescription(String) overrides the default method.
  • Constructor Details

    • PostfixMathCommand

      public PostfixMathCommand()
      Creates a new PostfixMathCommand class.
    • PostfixMathCommand

      public PostfixMathCommand(int nParam)
      Creates a new PostfixMathCommand class.
      Parameters:
      nParam - number of parameters for the function
  • Method Details

    • getName

      public String getName()
      Description copied from interface: PostfixMathCommandI
      The name of the function used in error messages
      Specified by:
      getName in interface PostfixMathCommandI
      Returns:
      the function name
    • setName

      public void setName(String name)
      Description copied from interface: PostfixMathCommandI
      Sets the name of the function used for error messages
      Specified by:
      setName in interface PostfixMathCommandI
      Parameters:
      name - the function name
    • getNumberOfParameters

      public int getNumberOfParameters()
      Return the required number of parameters.
      Specified by:
      getNumberOfParameters in interface PostfixMathCommandI
      Returns:
      allowable number of parameters, -1 if a variable number is allowed
    • setCurNumberOfParameters

      public void setCurNumberOfParameters(int n)
      Sets the number of current number of parameters used in the next call of run(). This method is only called when the reqNumberOfParameters is -1.
      Specified by:
      setCurNumberOfParameters in interface PostfixMathCommandI
      Parameters:
      n - number of parameter for next call of function
    • checkNumberOfParameters

      public boolean checkNumberOfParameters(int n)
      Checks the number of parameters of the function. Functions which set numberOfParameter=-1 should overload this method
      Specified by:
      checkNumberOfParameters in interface PostfixMathCommandI
      Parameters:
      n - number of parameters function will be called with.
      Returns:
      false if an illegal number of parameters is supplied, true otherwise.
    • run

      public void run(Stack<Object> s) throws EvaluationException
      Throws an exception because this method should never be called under normal circumstances. Each function should use its own run() method for evaluating the function. This includes popping off the parameters from the stack, and pushing the result back on the stack.
      Specified by:
      run in interface PostfixMathCommandI
      Parameters:
      s - arguments for function
      Throws:
      EvaluationException - if function cannot be evaluated
    • asString

      protected String asString(int position, Object value) throws IllegalParameterException
      Converts an argument to a string, with error checking.
      Parameters:
      position - argument number (starting from 0) used in error reporting
      value - the argument to convert
      Returns:
      value cast as a String
      Throws:
      IllegalParameterException - if the argument cannot be cast to a String
    • asType

      protected <T> T asType(Class<T> type, int position, Object value) throws IllegalParameterException
      Converts an argument to a particular type, with error checking. For example, to convert an argument to a String, use String s = asType(String.class, 0, arg);.
      Type Parameters:
      T - the type to cast to
      Parameters:
      type - the class of the type
      position - argument number (starting from 0) used in error reporting
      value - the argument to convert
      Returns:
      value cast as the specified type
      Throws:
      IllegalParameterException - if the argument cannot be cast to the specified type
      Since:
      Jep 4.1
    • asInt

      protected int asInt(int position, Object value) throws IllegalParameterException
      Converts an argument to an int, floating point values will be rounded.
      Parameters:
      position - argument number (starting from 0) used in error reporting
      value - the argument to convert
      Returns:
      value as a int
      Throws:
      IllegalParameterException - if the argument cannot be cast to a Number
    • asStrictInt

      protected int asStrictInt(int position, Object value) throws IllegalParameterException
      Converts an argument to an int, floating point values must represent integers.
      Parameters:
      position - argument number (starting from 0) used in error reporting
      value - the argument to convert
      Returns:
      value as a int
      Throws:
      IllegalParameterException - if the argument cannot be cast to a Number or intValue() != doubleValue()
    • asLong

      protected long asLong(int position, Object value) throws EvaluationException
      Converts an argument to a long, with error checking, floating point values will be rounded.
      Parameters:
      position - argument number (starting from 0) used in error reporting
      value - the argument to convert
      Returns:
      value as a long
      Throws:
      IllegalParameterException - if the argument cannot be cast to a Number
      EvaluationException
    • asDouble

      protected double asDouble(int position, Object value) throws EvaluationException
      Converts an argument to a double, with error checking, floating point values will be rounded.
      Parameters:
      position - argument number (starting from 0) used in error reporting
      value - the argument to convert
      Returns:
      value as a long
      Throws:
      IllegalParameterException - if the argument cannot be cast to a Number
      EvaluationException
    • asBool

      protected boolean asBool(int pos, Object value, boolean allowNumbers) throws EvaluationException
      Attempt to convert argument to a boolean
      Parameters:
      pos - position in argument list
      value - actual value of object
      allowNumbers - if true non-zero numbers are true and zero numbers are false
      Returns:
      value as a boolean
      Throws:
      EvaluationException - if value cannot be converted to a boolean, strings, fractional numbers
    • asArray

      protected Object[] asArray(Stack<Object> stack)
      Pop the arguments off the stack and put them in an array
      Parameters:
      stack - input
      Returns:
      an array of the arguments of the function in correct order
    • toString

      protected String toString(Object... args)
      Return a string representation of the function with its arguments.
      Parameters:
      args - the arguments to the function
      Returns:
      string representation of the function with its arguments
    • toString

      public String toString()
      Return a string representation of the function. If the name is not null, return the name, otherwise return the class name.
      Overrides:
      toString in class Object
    • getDescription

      public String getDescription()
      Return a description of the function from the properties file. The property name is the class name followed by ".description". The property is found using the JepMessages class, which can be configured to add additional properties files. If setDescription(String) has been called, return the description set by that method instead.
      Specified by:
      getDescription in interface PostfixMathCommandI
      Returns:
      the description of the function
      Since:
      Jep 4.1
    • getDescription

      protected String getDescription(String property)
      Return a description of the function from the resource bundle using JepMessages.getStringFromAdditionalBundles(String) and a property name.
      Parameters:
      property - the property to look up in the resource bundle
      Returns:
      the description of the function
      Since:
      Jep 4.1
      See Also:
    • setDescription

      public void setDescription(String description)
      Set the description of the function. This override the default description from the resource bundle.
      Parameters:
      description - the description of the function
      Since:
      Jep 4.1
    • getDescriptionWithType

      public String getDescriptionWithType(String type)
      Get a descriptions from a property with the format class.name.type.description
      Parameters:
      type - sub type of the function to include.
      Returns:
      the description of the function
      Since:
      Jep 4.1