Академический Документы
Профессиональный Документы
Культура Документы
Contents
Custom Java Function Samples
Creating Custom Java Simple Functions
Creating Custom Java Aggregate Functions
Method Parameter and Return Types
Related Topics
You can call custom Java functions directly in StreamBase expressions by an alias defined in the server
configuration file, or by using the calljava() function. The StreamBase expression language provides
two forms of thecalljava() function, to be used in StreamBase simple and aggregate expressions.
This topic provides guidelines for creating StreamBase custom functions in Java.
Note
StreamBase Studio ships with new projects configured by default to build with Java 6. As of
release 7.4, Java 5 is no longer supported within Studio or for developing Custom Extensions,
including Java functions.
Described in
custom-java-function
custom-java-aggregate
Use the New StreamBase Java Function wizard to create the base code, as described in Using
the StreamBase Java Function Wizard.
Implement a public static in a public Java class.
Observe the guidelines in Method Parameter and Return Types.
For example, Hypotenuse.java in the sample application declares the following class and method:
public class Hypotenuse {
public static double calculate(double a, double b) {
return Math.sqrt(a*a + b*b);
}
}
At compile time, the calljava() implementation looks for only a single method matching the exact
types of the function call in the StreamBase expression. But there can be multiple matching methods,
such as these two functionally equivalent ones:
Use the New StreamBase Java Function wizard to create the base code, as described in Using
the StreamBase Java Function Wizard.
Define a Java class that extends
the com.streambase.sb.operator.AggregateWindow class.
sum += value;
sumOfSquares += value*value;
++count;
a value that is convertible to a StreamBase data type. The calculate() method may be called
several times, or not at all.
Your implementation must provide at least one accumulate() method, and can optionally
provide several overloaded accumulate() methods, one per data
type. calljava() determines which one to call based on type. The argument types
for accumulate() and the return type for calculate() can be any of the types described in
the table in the next section.
The release() method is called at the end of each use of the class.
Java Primitive
Java Object
blob
com.streambase.sb.ByteArrayView
bool
boolean
java.lang.Boolean
double
double
java.lang.Double
int
int
java.lang.Integer
long
long
java.lang.Long
list
primitive_type[]
java.util.List
string
byte[]
java.lang.String
timestamp
com.streambase.sb.Timestamp
tuple
com.streambase.sb.Tuple
Notes
For simple functions, the return type cannot be void, and must be one of the Java primitive or
Java Object types shown above.
You can use java.lang.String anywhere a byte[] is acceptable as an argument or return
value. In this case, the StreamBase string is transparently converted to or from
a java.lang.String using the system default encoding.
If a parameter's type is byte[] and its value is null, it is represented as a Java null. Likewise,
if a Java method with a byte[] return type returns a null, the calling StreamBase expression will
see the return value as string(null).
You can pass a list of lists to functions expecting multi-dimensional array arguments. You can
mix list and array notations when doing this. That is, a two-dimensional list of doubles can be
passed to functions accepting any of the following arguments:
o
double[][]
o
list<double[]>
o
list<list<double>>
Note
Only Java primitive arrays and string arrays are compatible with lists, not complex data types
such as timestamps or tuples. Data type coercion is not supported.
Note
When an array argument has more than two dimensions, you cannot use a function alias. You
must invoke the function via calljava().
For better performance, Java functions can return arrays in place of lists. For example, the
following return types are equivalent:
double[]
foo() {}
// ==
list(double)
double[][][] foo() {}
// == list(list(list(double)))
If the parameter or return type is list or tuple, you must either provide a custom function
resolver, or must define the argument types in a custom-function element in the server
configuration file. See Custom Functions with Complex Data Types for details.
If any value of a parameter with a primitive type is null at runtime, the method that
implements the custom function is not invoked. However, Java Object parameter types can be used
to pass in null parameter values.
For example, if a StreamBase custom function call would involve converting a
StreamBase int(null) or bool(null) value to a primitive Java int or boolean, the method is
not called, and null is assumed as the return value.
public static boolean isZero(int i) { return i == 0; }
calljava("TheClass", "isZero", 1)
calljava("TheClass", "isZero", 0)
calljava("TheClass", "isZero", int(null))
Related Topics
Creating Method:
Considering the following example to explain the syntax of a method:
public static int funcName(int a, int b) {
// body
}
Here,
a, b: formal parameters
/* false *
/* true */
/* null */
Method definition consists of a method header and a method body. The same is shown below:
modifier returnType nameOfMethod (Parameter List) {
// method body
}
The syntax shown above includes:
modifier: It defines the access type of the method and it is optional to use.
parameter list.
Parameter List: The list of parameters, it is the type, order, and number of parameters of a method. These
Example:
Here is the source code of the above defined method called max(). This method takes two parameters num1 and
num2 and returns the maximum between the two:
/** the snippet returns the minimum between two numbers */
public static int minFunction(int n1, int n2) {
int min;
if (n1 > n2)
min = n2;
else
min = n1;
return min;
}
Method Calling:
For using a method, it should be called. There are two ways in which a method is called i.e. method returns a value or
returning nothing (no return value).
The process of method calling is simple. When a program invokes a method, the program control gets transferred to
the called method. This called method then returns control to the caller in two conditions, when:
The methods returning void is considered as call to a statement. Lets consider an example:
System.out.println("This is tutorialspoint.com!");
The method returning value can be understood by the following example:
int result = sum(6, 9);
Example:
Following is the example to demonstrate how to define a method and how to call it:
public class ExampleMinNumber{
return min;
}
}
This would produce the following result:
Minimum value = 6
Example:
Example:
The following program shows an example of passing parameter by value. The values of the arguments remains the
same even after the method invocation.
public class swappingExample {
swapFunction(a, b);
System.out.println("\n**Now, Before and After swapping values will be same
here**:");
System.out.println("After swapping, a = " +
a + " and b is " + b);
}
Method Overloading:
When a class has two or more methods by same name but different parameters, it is known as method overloading. It
is different from overriding. In overriding a method has same method name, type, number of parameters etc.
Lets consider the example shown before for finding minimum numbers of integer type. If, lets say we want to find
minimum number of double type. Then the concept of Overloading will be introduced to create two or more methods
with the same name but different parameters.
The below example explains the same:
public class ExampleOverloading{
// for integer
public static int minFunction(int n1, int n2) {
int min;
if (n1 > n2)
min = n2;
else
min = n1;
return min;
}
// for double
public static double minFunction(double n1, double n2) {
double min;
if (n1 > n2)
min = n2;
else
min = n1;
return min;
}
}
This would produce the following result:
Minimum Value = 6
Minimum Value = 7.3
Overloading methods makes program readable. Here, two methods are given same name but with different
parameters. The minimum number from integer and double types is the result.
Example:
The following program displays all of the command-line arguments that it is called with:
public class CommandLine {
The Constructors:
A constructor initializes an object when it is created. It has the same name as its class and is syntactically similar to a
method. However, constructors have no explicit return type.
Typically, you will use a constructor to give initial values to the instance variables defined by the class, or to perform
any other startup procedures required to create a fully formed object.
All classes have constructors, whether you define one or not, because Java automatically provides a default
constructor that initializes all member variables to zero. However, once you define your own constructor, the default
constructor is no longer used.
Example:
Here is a simple example that uses a constructor:
// A simple constructor.
class MyClass {
int x;
Example:
Here is a simple example that uses a constructor:
// A simple constructor.
class MyClass {
int x;
Variable Arguments(var-args):
JDK 1.5 enables you to pass a variable number of arguments of the same type to a method. The parameter in the
method is declared as follows:
typeName... parameterName
In the method declaration, you specify the type followed by an ellipsis (...) Only one variable-length parameter may be
specified in a method, and this parameter must be the last parameter. Any regular parameters must precede it.
Example:
public class VarargsDemo {
numbers.length; i++)
result)
result = numbers[i];
System.out.println("The max value is " + result);
}
}
This would produce the following result:
The max value is 56.5
The max value is 3.0
It is possible to define a method that will be called just before an object's final destruction by the garbage collector.
This method is called finalize( ), and it can be used to ensure that an object terminates cleanly.
For example, you might use finalize( ) to make sure that an open file owned by that object is closed.
To add a finalizer to a class, you simply define the finalize( ) method. The Java runtime calls that method whenever it
is about to recycle an object of that class.
Inside the finalize( ) method, you will specify those actions that must be performed before an object is destroyed.
The finalize( ) method has this general form:
protected void finalize( )
{
// finalization code here
}
Here, the keyword protected is a specifier that prevents access to finalize( ) by code defined outside its class.
This means that you cannot know when or even if finalize( ) will be executed. For example, if your program ends
before garbage collection occurs, finalize( ) will not execute.
Method Headers
We summarize method headers, define the signature of a
method, and discuss its use. This material is covered in
more detail in the course text, Gries/Gries.
A method declaration consists of a specification (as a
comment), a method header, and a body. Here is a method
declaration:
/** = "x is in the range 1..50". */
public boolean isLess(int x) {
return x >= 1 && x <= 50;
}
Kind of
method
Format of header
A call on a method:
procedure
Is a statement.
( <parameter-list> ) ;
function
( <parameter-list> )
constructo
public <class-name>
( <parameter-list> )
statement of a constructor.
Notes
Method names
A method name is often written as a sequence of one or
more words, like toString, setName,
andisLess. The first letter of each word, except the
first, is capitalized.
A procedure name is usually a command to do something,
e.g. setName, printArea, fixName.
A function name is usually one of:
isLess, areRed;
Method signature
The signature of a method consists of its name and the
types of its parameters (enclosed in parentheses and
separated by commas). For example, the signature of the
method declared near the beginning of this webpage is
isLess(int)
isLess and