Over a million developers have joined DZone.

JR Operations and Capabilities

· Java Zone

Discover how powerful static code analysis and ergonomic design make development not only productive but also an enjoyable experience, brought to you in partnership with JetBrains

Now that we’ve seen how to use semaphores and monitors in JR, we must go to message passing. But first, we must learn how to use operations and capabilities in JR. These concepts are used in JR for message passing, so we must learn them first.

An operation (or op-method) has the same form as a method declaration but is declared with a op keyword. An operation can be invoked the same way as a normal method or can be invoked with a call statement (also with a send statement as we’ll see in next post about JR).

A little example :

public class Operations1 {
public static op int sum(int i1, int i2){
System.out.println("sum()");

return i1 + i2;
}

public static void main(String... args){
System.out.println(sum(2, 2));
call sum(3, 3);
}
}

You can also write the operation in two parts :

public static op int sum(int i1, int i2);
public static int sum(int i1, int i2){
System.out.println("sum()");

return i1 + i2;
}

This is exactly the same. As you will see in the next post, we’ll also use operations only declared with no method body.

When you’ve operations, you can refer to them using capabilities. The capabilities are a kind of function pointer. You can declare a capability like that :

cap operation_signature cap_name

You can invoke a capability like a normal method. Here is a little example :

public class Operations3 {
public static op int sum(int i1, int i2){
System.out.println("sum()");

return i1 + i2;
}

public static void main(String... args){
cap int (int, int) cap_sum; //Declare the capability

cap_sum = sum; //Make a reference to sum operations

System.out.println(cap_sum(2, 2));
}
}

And you can also pass them as arguments, that can be useful to generify a code. Here is a little example that uses this approach:

import java.util.Random;

public class Operations3 {
public static op int square(int x){
return x * x;
}

public static op int dec(int x){
return x - 3;
}

public static op int inc(int x){
return x + 11;
}

public static op int add(cap int (int) f1, cap int (int) f2, int result){
return f1(result) + f2(result);
}

public static op int sub(cap int (int) f1, cap int (int) f2, int result){
return f1(result) + f2(result);
}

public static void main(String... args){
cap int (int) functions[] = new cap int (int)[3];
functions[0] = square;
functions[1] = dec;
functions[2] = inc;

cap int (cap int (int), cap int (int), int)[] double_functions = new cap int (cap int (int), cap int (int), int)[2]
double_functions[0] = add;
double_functions[1] = sub;

int result = 100;
Random random = new Random();

for(int i = 0; i < 10; i++){
int index1 = random.nextInt(double_functions.length);
int index2 = random.nextInt(functions.length);

result = double_functions[index1](functions[index2], functions[index2], result);
}

System.out.println(result);
}
}

Now you know everything about operations and capabilities. In the next post, we’ll learn asynchronous message passing in JR.

From http://www.baptiste-wicht.com/2010/06/jr-operations-and-capabilities/

Learn more about Kotlin, a new programming language designed to solve problems that software developers face every day brought to you in partnership with JetBrains.

Topics:

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}