X

Geertjan's Blog

  • February 6, 2014

Functional Interfaces in the NetBeans Platform

Geertjan Wielenga
Product Manager

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();

Be the first to comment

Comments ( 0 )
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.