Class ArgumentExpander

java.lang.Object
com.singularsys.extensions.statistical.ArgumentExpander

public abstract class ArgumentExpander extends Object
Base class for methods which expand Lists, arrays and other collections in their argument list. Subclasses can be used by PostfixMathCommands to expand their arguments.

The following pattern can be used which allows thread safe use.

 public class Sum extends PostfixMathCommand {
 class Processor extends ArgumentExpander {
        double total; // stores intermediate results

        @Override
        protected void doSingleArg(Object arg) throws EvaluationException {
            if(total==null)
                total= ((Double)arg).doubleValue();
            else {
                total += ((Double)arg).doubleValue();
            }
        }
  }

  public void run(Stack<Object> stack) throws EvaluationException {
        Processor p = new Processor();
        p.doStack(stack, this.curNumberOfParameters);
        stack.push(p.total);
  }
 }
 
Author:
Richard Morris
  • Constructor Details

    • ArgumentExpander

      public ArgumentExpander()
  • Method Details

    • doStack

      public void doStack(Stack<Object> stack, int neles) throws EvaluationException
      Pop neles elements off the stack and then call doArg on each.
      Parameters:
      stack - input arguments to function
      neles - number of elements to pop
      Throws:
      EvaluationException - on error
    • doArg

      public void doArg(Object arg) throws EvaluationException
      Process an argument, arrays and Iterable objects are expanded. doSingleArg is called for each array element and each member of an iterable. If the argument is not an array/iterable doSingleArg is called on the argument.
      Parameters:
      arg - an argument to the PFMC, may be a vector or matrix
      Throws:
      EvaluationException - on error
    • doSingleArg

      protected abstract void doSingleArg(Object arg) throws EvaluationException
      Process an individual element after Lists/Arrays have been expanded.
      Parameters:
      arg - one data item
      Throws:
      EvaluationException - on error