Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JDK interface for processing a generic parameter

Is there an interface from the JDK that looks something like this:

public interface Callback<T> {
    public void process(T t);
}

The requirement is to implement a callback that runs code, but doesn't return anything.

I could write my own (by simply using the example code here), but I'd like to use an existing wheel if one exists, rather that reinventing one.

like image 415
Bohemian Avatar asked Dec 13 '11 01:12

Bohemian


People also ask

What is a generic type in JDK?

See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases. A generic type is a generic class or interface that is parameterized over types. The following Box class will be modified to demonstrate the concept.

How do you implement a generic interface in Java?

Another way is to create a generic class that implements the generic interface. The class definition uses the same formal type parameters twice – one after the class name and another after the interface name it implements. Consider the following codes. They use <U> twice in the two locations. 3.

Do all generic classes have the same type parameters?

generic class All instances of a generic class have the same run-time class, regardless of their actual type parameters. A class generic has the same behavior for all of its possible type parameters; "...

What is generics in Java?

Generics enable non-primitive types (classes and interfaces) to be parameters when defining: and methods. Class was generified in release 1.5. The type of a class literal is no longer simply Class, but Class<T>. For example: and Integer.class is of type Class<Integer>. JPA - How to define a @One-to-Many relationship ? More ... Why ?


2 Answers

So you need something like

interface Foo<T>
    bar(T)

Only 3 interfaces in JDK are like that

java.nio.file.DirectoryStream$Filter<T>

    boolean accept(T entry) throws IOException;


java.lang.Comparable<T>

    int compareTo(T o);


javax.xml.ws.Provider<T>

    T invoke(T request);

Obviously you won't like them.

Async IO has a callback interface, but it's a bit more complicated:

java.nio.channels.CompletionHandler<V,A>

    void completed(V result, A attachment);

    void failed(Throwable exc, A attachment);
like image 146
irreputable Avatar answered Oct 13 '22 00:10

irreputable


No, I don't believe there's such an interface currently. There's currently slated to be such an interface, called Block (with an apply method, I think), in JDK 8... though the name could well change between now and then.

like image 34
ColinD Avatar answered Oct 13 '22 00:10

ColinD