Search code examples
javashared-librariesjava-6

Standard interface for no argument function


Is there in Java 6 (or any compatible library) standard interface for no argument function and generic return type.

Something like:

interface Foo<T> {
      T call();
}

Solution

  • It's a Supplier<T> - it takes nothing, and supplies with T. And the abstract method it defines is nice to be called get()

    interface Supplier<T> {
       T get();
    }
    

    Note that in Java8, this (@FunctionalInterface) already exists (it's called Supplier), so if you run your code under Java8, there's no need to define a custom interface.

    Also, if you run your code under some pre-Java8 version, then you can use the Guava's Supplier interface.