5

I'm looking for a standard Javafx or java interface (if it exists) that acts like a Callback, except that it does not return a value.

The standard Callback from javafx.util package is as follows:

public interface Callback<P,R> {
    public R call(P param);
}

This is useful when you need to return the value, but I don't. I've looked into Callable<T>:

public interface Callable<V> {
    V call() throws Exception;
}

But this doesn't actually pass in a value into call. What I'm looking for is basically this:

public interface Callable<V> {
    void call(V value) throws Exception;
}

Is there a standard Java interface, or should I just create my own?

user2914191
  • 877
  • 1
  • 8
  • 21

2 Answers2

6

What are you looking for is Consumer. That's added since java 8.

Represents an operation that accepts a single input argument and returns no result. Unlike most other functional interfaces, Consumer is expected to operate via side-effects.

@FunctionalInterface
public interface Consumer<T> {

/**
 * Performs this operation on the given argument.
 *
 * @param t the input argument
 */
void accept(T t);
Schidu Luca
  • 3,897
  • 1
  • 12
  • 27
0

Runnable.run() may be another solution like here, but the Consumer actually does not handle Exception like Callable (see also here).

I could not find a standard interface supporting Exception and thus maybe one has to create such standard interfaces himself, like with some interface Code here.

Andreas Covidiot
  • 4,286
  • 5
  • 51
  • 96