Class UnaryFunction

java.lang.Object
com.singularsys.jep.functions.PostfixMathCommand
com.singularsys.jep.functions.UnaryFunction
All Implemented Interfaces:
PostfixMathCommandI, Serializable
Direct Known Subclasses:
Abs, ArcCosine, ArcCosineH, ArcSine, ArcSineH, ArcTangent, ArcTanH, Arg, BitComp, Ceil, Conjugate, Cosecant, Cosine, CosineH, Cotangent, Exp, Factorial, Floor, Identity, Imaginary, IsInfinite, IsNaN, IsNull, JepTest.MyUnary, Length, Logarithm, LogBase2, LowerCase, NaturalLogarithm, Not, NullWrappedUnary, Real, Secant, Signum, Sine, SineH, SquareRoot, Str, StrictNaturalLogarithm, Tangent, TanH, Trim, UMinus, UpperCase

public abstract class UnaryFunction extends PostfixMathCommand
Convenient base class for unary functions. Defines an Object eval(Object arg) method for calculating the result.
Author:
Richard Morris
See Also:
  • Constructor Details

    • UnaryFunction

      public UnaryFunction()
      Default constructor. Sets the function to have a single argument.
  • Method Details

    • run

      public void run(Stack<Object> stack) throws EvaluationException
      Used for compatibility with StandardEvaluator Simple calls eval(Object) placing result on stack.
      Specified by:
      run in interface PostfixMathCommandI
      Overrides:
      run in class PostfixMathCommand
      Parameters:
      stack - arguments for function
      Throws:
      EvaluationException - if function cannot be evaluated
    • eval

      public abstract Object eval(Object arg) throws EvaluationException
      Evaluate the function
      Parameters:
      arg - the single argument passed in
      Returns:
      the result of the function
      Throws:
      EvaluationException
    • instanceOf

      public static UnaryFunction instanceOf(Function<Object,? super Object> fun)
      Create a UnaryFunction from a lambda expression where the argument can be any Object. For example UnaryFunction.instanceOf(x -> 1.0 / ((Number) x).doubleValue())
      Parameters:
      fun - the lambda function
      Returns:
      a new UnaryFunction instance
      Since:
      Jep 4.0
    • instanceOf

      public static <T> UnaryFunction instanceOf(Class<T> type, Function<T,? super Object> fun)
      Create a UnaryFunction from a lambda expression where the argument is of a specified type. For example UnaryFunction.instanceOf(Integer.class, x -> -x ) or using a method reference UnaryFunction.instanceOf(Double.class,Math::cbrt). The eval(Object) method will throw an IllegalParameterException if the argument is not of the correct type.
      Type Parameters:
      T - type of arguments and return value
      Parameters:
      type - type of the arguments
      fun - the lambda function
      Returns:
      a new UnaryFunction instance
      Since:
      Jep 4.0