Differences

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

Link to this comparison view

java_8_functions [2017/06/22 09:48]
java_8_functions [2021/04/05 11:23] (current)
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}}