Skip to main content

Creating Custom Java Simple Functions


Creating Custom Java Simple Functions
Simple custom Java functions can be called in expressions in the context of most StreamBase components, other than the Aggregate operator. Follow these steps to create a custom Java simple function:
·         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:
public static boolean isZero(int i) { return i == 0; }
public static Boolean isZero(Integer i) {
    return i == null ? null : Boolean.valueOf(i.intValue() == 0);
}
If this case occurs, StreamBase throws an error.
Creating Custom Java Aggregate Functions
Follow these steps to create a custom Java aggregate function:
·         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.
·         Observe the guidelines in Method Parameter and Return Types.
·         Observe the guidelines in the annotations to the example below.
Consider the following annotated example:
package com.mycompany;

import com.streambase.sb.operator.AggregateWindow;

public class MyStdev extends AggregateWindow {                                1
    private double sum;
    private double sumOfSquares;
    private int count;

    public void init() {                                                      2
        sum = sumOfSquares = 0.0;
        count = 0;
    }         

    public double calculate() {                                               3
        return Math.sqrt((count * sumOfSquares - sum*sum) / count*(count-1));
    }

    public void accumulate(double value) {                                    4
        sum += value;
        sumOfSquares += value*value;
        ++count;
    }

    public void release() { /* nothing to release in this example */ }        5
}
The following annotations describe points of interest in the preceding example:
1
Declare a public class that extends the AggregateWindow class (as documented in the StreamBase Java Client library).
2
The init() method is called at the start of each new use of the class. Since custom aggregate objects are likely to be reused, perform all initialization ininit() rather than in the constructor. (The constructor is called only once, while init() is called before each use.)
3
Your implementation must contain a calculate() method that takes no arguments and returns a value that is convertible to a StreamBase data type. The calculate() method may be called several times, or not at all.
4
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.
5
The release() method is called at the end of each use of the class.
Method Parameter and Return Types
The method can have any number of parameters, including none. Each parameter must be a Java primitive or object type corresponding to a StreamBase data type as shown in the following table:
StreamBase Data Type
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
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).
·     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 acustom-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)          /* false *
calljava("TheClass", "isZero", 0)          /* true */
calljava("TheClass", "isZero", int(null))  /* null */

Comments

Popular posts from this blog

Hi! I'm Java...

Java is a computer programming language. It enables programmers to write computer instructions using English based commands, instead of having to write in numeric codes. It’s known as a “high-level” language because it can be read and written easily by humans. Like English, Java has a set of rules that determine how the instructions are written. These rules are known as its “syntax”. Once a program has been written, the high-level instructions are translated into numeric codes that computers can understand and execute. Who Created Java? In the early nineties, Java was created by a team led by James Gosling for Sun Microsystems. It was originally designed for use on digital mobile devices, such as cell phones. However, when Java 1.0 was released to the public in 1996, its main focus had shifted to use on the Internet. It provided more interactivity with users by giving developers a way to produce animated webpages . Over the years it has evolved as a successful language for

I'm the Server Operating System

Server Operating System A server operating system is software that was especially developed to serve as a platform for running multi-user computer programs, applications that are networked and programs critical to business computing. This type of operating system (OS) often comes bundled with the most common types of applications deployed in the client-server model, a term used to indicate the exchange of information between computers. For example, a hypertext transfer protocol (HTTP) or web server hosts or "holds" the text files, images files and scripts that work together to form a website. When someone connects to the Internet and types in a web address, the server that holds the files of the site delivers or "serves" the requested pages to the client computer or the machine that made the request. Frequently used applications in the client-server model handle operations for sharing files and printers across a network, hosting and serving web pages via the

A Hit Count Using Java Servlet's Session Tracking

import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class SessionTracker extends HttpServlet {         public void doGet(HttpServletRequest req,HttpServletResponse res)         throws ServletException,IOException         {                res.setContentType("text/html");                PrintWriter out=res.getWriter();                HttpSession session=req.getSession(true);                Integer count=(Integer)session.getValue("tracker.count");                if(count==null)                {                         count=new Integer(1);                }else{                         count=new Integer(count.intValue()+1);                }                session.putValue("tracker.count",count);                out.println("<HTML><BODY><H1>SESSION TRACKING DEMO</H1>");                out.println("You've visited this page "+count+((count.intValue()==1)?" time.":"times."