Package com.im.df.api.support
Class CalculationsScriptBaseClass
- java.lang.Object
-
- groovy.lang.GroovyObjectSupport
-
- groovy.lang.Script
-
- 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- Author:
- Tim Dudgeon
-
-
Constructor Summary
Constructors Constructor Description CalculationsScriptBaseClass()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description java.lang.Number
avg(java.lang.Number... args)
Generate the average of the non-null values.java.lang.Number
avg(java.util.Collection<java.lang.Number> args)
Generate the average of the non-null values.java.lang.String
concat(java.lang.String sep, java.lang.Object first, java.lang.Object... others)
Concatenates the values with the specified separator.java.lang.String
concat(java.lang.String sep, java.util.Collection<?> args)
Concatenates the values in the collection with the specified separator.java.lang.Number
divide(java.lang.Number arg1, java.lang.Number arg2)
Divide two numbers.void
each(java.util.Collection<?> c1, java.util.Collection<?> c2, groovy.lang.Closure<?> closure)
Helper method to iterate in a coordinated manner over 2 collections.void
each(java.util.Collection<?> c1, java.util.Collection<?> c2, java.util.Collection<?> c3, groovy.lang.Closure<?> closure)
Helper method to iterate in a coordinated manner over 3 collections.java.lang.Class<?>
getReturnType()
Get the type of object that this script is expected to return.java.lang.Number
minus(java.lang.Number arg1, java.lang.Number arg2)
Subtract two numbers.java.lang.Number
multiply(java.lang.Number... args)
Generate the product of the non-null values.java.lang.Number
multiply(java.util.Collection<java.lang.Number> args)
Generate the product of the non-null values.void
setReturnType(java.lang.Class<?> returnType)
Set the type of object that this script is expected to return.java.lang.Number
sum(java.lang.Number... args)
Generate the sum of the non-null values.java.lang.Number
sum(java.util.Collection<java.lang.Number> args)
Generate the sum of the non-null values.
-
-
-
Method Detail
-
getReturnType
public java.lang.Class<?> getReturnType()
Get the type of object that this script is expected to return.- Returns:
- the value of returnType
-
setReturnType
public void setReturnType(java.lang.Class<?> returnType)
Set the type of object that this script is expected to return.- Parameters:
returnType
- new value of returnType
-
sum
public java.lang.Number sum(java.lang.Number... args)
Generate the sum of the non-null values.- Parameters:
args
- The values- Returns:
- The sum
-
sum
public java.lang.Number sum(java.util.Collection<java.lang.Number> args)
Generate the sum of the non-null values.- Parameters:
args
- The values- Returns:
- The sum
-
multiply
public java.lang.Number multiply(java.lang.Number... args)
Generate the product of the non-null values.- Parameters:
args
- The values- Returns:
- The product
-
multiply
public java.lang.Number multiply(java.util.Collection<java.lang.Number> args)
Generate the product of the non-null values.- Parameters:
args
- The values- Returns:
- The product
-
avg
public java.lang.Number avg(java.lang.Number... args)
Generate the average of the non-null values.- Parameters:
args
- The values- Returns:
- The average
-
avg
public java.lang.Number avg(java.util.Collection<java.lang.Number> args)
Generate the average of the non-null values.- Parameters:
args
- The values- Returns:
- The average
-
minus
public java.lang.Number minus(java.lang.Number arg1, java.lang.Number arg2)
Subtract two numbers. If either is null result is null.- Parameters:
arg1
-arg2
-- Returns:
- arg1 - arg2
-
divide
public java.lang.Number divide(java.lang.Number arg1, java.lang.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 java.lang.String concat(java.lang.String sep, java.lang.Object first, java.lang.Object... others)
Concatenates the values with the specified separator.- Parameters:
sep
-first
-others
-
-
concat
public java.lang.String concat(java.lang.String sep, java.util.Collection<?> args)
Concatenates the values in the collection with the specified separator.- Parameters:
sep
- The separatorargs
- The values- Returns:
- The concatenated values or an empty String, never
null
.
-
each
public void each(java.util.Collection<?> c1, java.util.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[1] 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(java.util.Collection<?> c1, java.util.Collection<?> c2, java.util.Collection<?> c3, groovy.lang.Closure<?> closure)
Helper method to iterate in a coordinated manner over 3 collections. This method is the 3 collection form ofeach(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.c3
- The third collection of values to iterate through.closure
- The code that will be called in each iteration to process the collection values.
-
-