User Tools

Site Tools


java_8_functions

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

java_8_functions [2017/06/22 07:48] (current)
mihael created
Line 1: Line 1:
 +====== Functions ======
  
 +===== Function with no Parameter and No Return Value =====
 +
 +You can just use the ''​Runnable''​ interface as a way for a callback. ''​Runnable''​ also has the ''​FunctionalInterface''​ annotation and as such is a valid interface for a Java 8 function.
 +
 +Interface:
 +
 +<sxh java>
 +    void delete(int id, Runnable callback);
 +</​sxh>​
 +
 +Caller:
 +
 +<sxh java>
 +    delete(id, () -> {
 +        // do some stuff like return data from a REST service
 +        response.noContent().build();​
 +    });
 +</​sxh>​
 +
 +Callee:
 +
 +<sxh java>
 +    public void delete(int id, Runnable callback) {
 +      getConnection(connection -> {
 +          connection.delete(id,​ result -> {
 +              if (result.success()) {
 +                  callback.run();​
 +              }
 +              else {
 +                  throw result.cause();​
 +              }
 +          });
 +          ​
 +      });
 +</​sxh>​
 +
 +{{tag> devel java}}
java_8_functions.txt ยท Last modified: 2017/06/22 07:48 by mihael