DZone
Java Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Java Zone > JR Operations and Capabilities

JR Operations and Capabilities

Baptiste Wicht user avatar by
Baptiste Wicht
·
Jul. 01, 10 · Java Zone · Interview
Like (0)
Save
Tweet
3.77K Views

Join the DZone community and get the full member experience.

Join For Free

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/

Message passing POST (HTTP) Concept (generic programming) Pointer (computer programming) Form (document) Pass (software) Semaphore (programming) Monitor (synchronization)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How Java Apps Litter Beyond the Heap
  • 12 Modern CSS Techniques For Older CSS Problems
  • DZone's Article Submission Guidelines
  • Biometric Authentication: Best Practices

Comments

Java Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo