Thursday Feb 06, 2014

Functional Interfaces in the NetBeans Platform

SaveCookie, and the other loosely coupled capabilities in the NetBeans Platform, meet the requirements of the new concept functional interface in JDK 8. They're classes that have a single abstract method.

For example:

SaveCookie s = new SaveCookie() {
    public void save() {
        System.out.println("save to database...");
    }
};

When a change is made somewhere, you can publish the above into the Lookup, causing the SaveAction to be enabled, which means the Save menu item, toolbar button, and keyboard shortcut can be invoked. When invoked, the System.out above is executed.

The Java editor in the IDE lets you convert the above to the below, using the "Use lambda expression" hint:

SaveCookie s = () -> {
    System.out.println("save to database...");
}; 

Cleaned up even further, it can be simplified to this, though it's kind of unlikely you're going to have one statement in the method:

SaveCookie s = () -> System.out.println("save to database...");

On the other hand, you could point to a method, where the actual save functionality is found:

SaveCookie s = () -> save();
About

Geertjan Wielenga (@geertjanw) is a Principal Product Manager in the Oracle Developer Tools group living & working in Amsterdam. He is a Java technology enthusiast, evangelist, trainer, speaker, and writer. He blogs here daily.

The focus of this blog is mostly on NetBeans (a development tool primarily for Java programmers), with an occasional reference to NetBeans, and sometimes diverging to topics relating to NetBeans. And then there are days when NetBeans is mentioned, just for a change.

Search

Archives
« February 2014 »
SunMonTueWedThuFriSat
      
8
9
15
16
19
22
23
27
 
       
Today