ripper234
ripper234

Reputation: 230156

What is the de facto standard for Action/Func classes?

Because the JDK doesn't contain them, a zillion of 3rd party libraries contain Action or Func interfaces, that look something like:

public interface Action<T> {
  void doSomething(T item);
}

public interface Func<TResult, TInput> {
  TResult doSomething(TInput input);
}

What is the de-facto, or most commonly used standard for this?

Upvotes: 4

Views: 200

Answers (2)

ColinD
ColinD

Reputation: 110054

While I don't know of any interface that matches your Action<T> signature (possibly partly because such a signature mandates that instances of Action produce side effects), Guava has a Function interface that matches the second signature, plus lots of Collection and Iterable related utilities that make use of Functions to do lazy mapping (called transform in Guava). It's also just a really great general-purpose Java library.

Upvotes: 1

Matthew Flaschen
Matthew Flaschen

Reputation: 284927

You can use Runnable (void) or Callable (returning a value). But as you note, packages often create their own. Part of the reason is to have a more meaningful name than run or call. You should note that Java does not have C#-style delegates, nor does it currently have a way to implicitly convert between a method and an instance of an interface.

Note that neither of the above interfaces allows parameters. If you need to, you will need a different solution.

EDIT: I have used Apache Functor in the past, and it has some of these interfaces (e.g. UnaryProcedure and UnaryFunction for the ones in the question). However, I would still consider creating your own, especially if you don't need anything else from Functor (e.g. an algorithm or adapter)

Upvotes: 3

Related Questions