Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an interface similar to Callable but with arguments?

Tags:

java

interface

Is there an interface in Java similar to the Callable interface, that can accept an argument to its call method?

Like so:

public interface MyCallable<V> {   V call(String s) throws Exception; } 

I would rather avoid creating a new type if there already exists something that I can use. Or is there a better strategy to having multiple clients implement and plug in a callable routine?

Copied from here http://www.programmingforums.org/thread27905.html

like image 916
raisercostin Avatar asked Jun 18 '12 13:06

raisercostin


People also ask

Can we pass parameter in Callable?

The first way we can send a parameter to a thread is simply providing it to our Runnable or Callable in their constructor. Note that the reason this works is that we've handed our class its state before launching the thread.

What is the difference between Java's Callable and runnable interfaces?

A callable interface throws the checked exception and returns the result. A runnable interface, on the other hand, shows the result or throws an exception, but does not do both.

Is Callable a functional interface?

Interface Callable<V>This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference. A task that returns a result and may throw an exception. Implementors define a single method with no arguments called call .

Is Callable an interface?

Interface Callable<V> Implementors define a single method with no arguments called call. The Callable interface is similar to Runnable , in that both are designed for classes whose instances are potentially executed by another thread. A Runnable, however, does not return a result and cannot throw a checked exception.


2 Answers

Since Java 8 there is a whole set of Function-like interfaces in the java.util.function package. The one you're asking for specifically is simply Function.

Prior to Java 8, there was no general-purpose, built-in interface for this, but some libraries provided it.

For example Guava has the Function<F,T> interface with the method T apply(F input). It also makes heavy use of that interface in several places.

like image 175
Joachim Sauer Avatar answered Sep 22 '22 06:09

Joachim Sauer


at first it thought that this is done with an interface but then i found that it should be done using an abstract class.

i have solved it this way:


edit: lately i just use this:

    public static abstract class callback1<T>{          public abstract  void run(T value);     }      public static abstract class callback2<T,J>{          public abstract  void run(T value,J value2);     }      public static abstract class callback3<T,J,Z>{          public abstract  void run(T value,J value2,Z value3);     }       public static abstract class callbackret1<R,T>{          public abstract  R run(T value);     }      public static abstract class callbackret2<R,T,J>{          public abstract  R run(T value,J value2);     }      public static abstract class callbackret3<R,T,J,Z>{          public abstract  R run(T value,J value2,Z value3);     } 

CallBack.java

public abstract class CallBack<TRet,TArg> {     public abstract TRet call(TArg val); } 

define method:

class Sample2  {     CallBack<Void,String> cb;     void callcb(CallBack<Void,String> CB)     {      cb=CB; //save the callback      cb.call("yes!"); // call the callback     } } 

use method:

sample2.callcb(new CallBack<Void,String>(){         @Override         public Void call(String val) {             // TODO Auto-generated method stub             return null;         }     }); 

two arguments sample: CallBack2.java

public abstract class CallBack2<TRet,TArg1,TArg2> {     public abstract TRet call(TArg1 val1,TArg2 val2); } 

notice that when you use Void return type you have to use return null; so here is a variation to fix that because usually callbacks do not return any value.

void as return type: SimpleCallBack.java

public abstract class SimpleCallBack<TArg> {     public abstract void call(TArg val); } 

void as return type 2 args: SimpleCallBack2.java

public abstract class SimpleCallBack<TArg1,TArg2> {     public abstract void call(TArg1 val1,TArg2 val2); } 

interface is not useful for this.

interfaces allow multiple types match same type. by having a shared predefined set of functions.

abstract classes allow empty functions inside them to be completed later. at extending or instantiation.

like image 33
Shimon Doodkin Avatar answered Sep 19 '22 06:09

Shimon Doodkin