com.im.df.api 5.9

com.im.df.api.support
Class CalculationsScriptBaseClass

java.lang.Object
  extended by groovy.lang.GroovyObjectSupport
      extended by groovy.lang.Script
          extended by com.im.df.api.support.CalculationsScriptBaseClass
All Implemented Interfaces:
groovy.lang.GroovyObject

public abstract class CalculationsScriptBaseClass
extends groovy.lang.Script

Custom base class that allows extra methods to be automagically injected into scripts. Mostly this comprises calculation methods like sum(), avg() etc. that can be called from the script


Constructor Summary
CalculationsScriptBaseClass()
           
 
Method Summary
 Number avg(Collection<Number> args)
          Generate the average of the non-null values.
 Number avg(Number... args)
          Generate the average of the non-null values.
 String concat(String sep, Collection<?> args)
          Concatenates the values in the collection with the specified separator.
 String concat(String sep, Object first, Object... others)
          Concatenates the values with the specified separator.
 Number divide(Number arg1, Number arg2)
          Divide two numbers.
 void each(Collection<?> c1, Collection<?> c2, groovy.lang.Closure closure)
          Helper method to iterate in a coordinated manner over 2 collections.
 void each(Collection<?> c1, Collection<?> c2, Collection<?> c3, groovy.lang.Closure closure)
          Helper method to iterate in a coordinated manner over 3 collections.
 Class<?> getReturnType()
          Get the type of object that this script is expected to return
 Number minus(Number arg1, Number arg2)
          Subtract two numbers.
 Number multiply(Collection<Number> args)
          Generate the product of the non-null values.
 Number multiply(Number... args)
          Generate the product of the non-null values.
 void setReturnType(Class<?> returnType)
          Set the type of object that this script is expected to return
 Number sum(Collection<Number> args)
          Generate the sum of the non-null values.
 Number sum(Number... args)
          Generate the sum of the non-null values.
 
Methods inherited from class groovy.lang.Script
evaluate, evaluate, getBinding, getProperty, invokeMethod, print, printf, printf, println, println, run, run, setBinding, setProperty
 
Methods inherited from class groovy.lang.GroovyObjectSupport
getMetaClass, setMetaClass
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CalculationsScriptBaseClass

public CalculationsScriptBaseClass()
Method Detail

getReturnType

public Class<?> getReturnType()
Get the type of object that this script is expected to return

Returns:
the value of returnType

setReturnType

public void setReturnType(Class<?> returnType)
Set the type of object that this script is expected to return

Parameters:
returnType - new value of returnType

sum

public Number sum(Number... args)
Generate the sum of the non-null values.

Parameters:
args - The values
Returns:
The sum

sum

public Number sum(Collection<Number> args)
Generate the sum of the non-null values.

Parameters:
args - The values
Returns:
The sum

multiply

public Number multiply(Number... args)
Generate the product of the non-null values.

Parameters:
args - The values
Returns:
The product

multiply

public Number multiply(Collection<Number> args)
Generate the product of the non-null values.

Parameters:
args - The values
Returns:
The product

avg

public Number avg(Number... args)
Generate the average of the non-null values.

Parameters:
args - The values
Returns:
The average

avg

public Number avg(Collection<Number> args)
Generate the average of the non-null values.

Parameters:
args - The values
Returns:
The average

minus

public Number minus(Number arg1,
                    Number arg2)
Subtract two numbers. If either is null result is null.

Parameters:
arg1 -
arg2 -
Returns:
arg1 - arg2

divide

public Number divide(Number arg1,
                     Number arg2)
Divide two numbers. If either is null result is null. if arg2 is zero then result is null

Parameters:
arg1 -
arg2 -
Returns:
arg1 / arg2

concat

public String concat(String sep,
                     Object first,
                     Object... others)
Concatenates the values with the specified separator.

Parameters:
sep -
first -
others -

concat

public String concat(String sep,
                     Collection<?> args)
Concatenates the values in the collection with the specified separator.

Parameters:
sep - The separator
args - The values
Returns:
The concatenated values or an empty String, never null.

each

public void each(Collection<?> c1,
                 Collection<?> c2,
                 groovy.lang.Closure closure)
Helper method to iterate in a coordinated manner over 2 collections.

Pass in two collections, and the closure is passed the corresponding values in turn. e.g.

  c1[0], c2[0]
  c1[1], c2[2]
  c1[2], c2[2]
  ...
 

and so on until one of the collections runs out of values (it is assumed that typically the collections will be of the same length.

e.g. to sum the differences in two list of number you can do something like this:

   List vals1 ...
   List vals2 ...
   float result = 0
   each(strings1, strings2) { a, b ->
       result += (a - b)
   }
 

Parameters:
c1 - The first collection of values to iterate through.
c2 - The second collection of values to iterate through.
closure - The code that will be called in each iteration to process the collection values.

each

public void each(Collection<?> c1,
                 Collection<?> c2,
                 Collection<?> c3,
                 groovy.lang.Closure closure)
Helper method to iterate in a coordinated manner over 3 collections. This method is the 3 collection form of each(java.util.Collection, java.util.Collection, groovy.lang.Closure) method.

Parameters:
c1 - The first collection of values to iterate through.
c2 - The second collection of values to iterate through.
closure - The code that will be called in each iteration to process the collection values.

com.im.df.api 5.9