Class CaseNonLazy

All Implemented Interfaces:
PostfixMathCommandI, Serializable

public class CaseNonLazy extends NaryFunction
A case statement. The argument first argument is evaluated and then checked against the 2nd, 4th, 6th argument if the two are equal then the 3rd, 5th, 7th argument is returned. If no arguments match then the last argument is returned (if the number of args is even) for example case("c","a",5,"b",6,"c",7,8) will return 7.

This is the non-lazy evaluation version, it will evaluate all arguments. Use Case for a lazy version which only evaluates arguments when needed.

Since:
Jep 4.1
Author:
Richard Morris
See Also:
  • Constructor Details

    • CaseNonLazy

      public CaseNonLazy()
      Standard constructor with no default value and errors if nulls are found.
    • CaseNonLazy

      public CaseNonLazy(CaseNonLazy.NullBehaviour nullBehaviour)
      Constructor with defined behaviour for null.
      Parameters:
      nullBehaviour - action to take if first argument is null
      Since:
      Jep 3.5
    • CaseNonLazy

      public CaseNonLazy(Object defaultVal)
      A case statement with a predefined default value and errors if nulls are found.
      Parameters:
      defaultVal - The default value to return when no cases match
      Since:
      Jep 3.5
    • CaseNonLazy

      public CaseNonLazy(Object defaultVal, CaseNonLazy.NullBehaviour nullBehaviour)
      Case statement with given default value and defined null behaviour.
      Parameters:
      defaultVal - The default value to return when no cases match
      nullBehaviour - action to take if first argument is null
      Since:
      Jep 3.5
  • Method Details

    • isHasDefault

      public boolean isHasDefault()
      Whether a default value is set
      Returns:
      true if a default value is set
      Since:
      Jep 3.5
    • getDefaultVal

      public Object getDefaultVal()
      The default value, only meaningful if isHasDefault() is true.
      Returns:
      the default value used.
      Since:
      Jep 3.5
    • getNullBehaviour

      public CaseNonLazy.NullBehaviour getNullBehaviour()
      Behaviour when passed zero arguments.
      Returns:
      code indicating behaviour type
      Since:
      Jep 3.5
    • checkNumberOfParameters

      public boolean checkNumberOfParameters(int n)
      Must have at least 3 parameters.
      Specified by:
      checkNumberOfParameters in interface PostfixMathCommandI
      Overrides:
      checkNumberOfParameters in class PostfixMathCommand
      Parameters:
      n - number of parameters function will be called with.
      Returns:
      false if an illegal number of parameters is supplied, true otherwise.
    • eval

      public Object eval(Object[] vals) throws EvaluationException
      Evaluate with a know set of arguments.
      Specified by:
      eval in class NaryFunction
      Parameters:
      vals - an array of arguments
      Returns:
      the result
      Throws:
      EvaluationException - if the calculation cannot be performed