I would like to emulate the following interface:
interface MultiSideEffectFunction<T> {
void action(T first, T second);
}
(it would save me from having to introduce a new interface)
is there a preferred Guava approach to doing this?
Note: I thought about doing it with
Function<Pair<T>, Void>
but there are two problems which makes the creation of a new interface preferable:
- One must define a class called
Pair<T>, which looks a lot like aMap.Entry<T, T>with more appropriate getter names (as a second question: is there a Guava type like this?). - The
Voidreturn type is always a pain – it would be better to have a genuinevoidsignature.
Guava contributor here.
The preferred Guava approach is to write your own interface.
In particular, do not use
Function;Functionshould only be used when the function doesn’t have side effects.Guava deliberately lacks a
Pairtype; we advise that any time you need a pair, you create your own class that attaches useful names to the two values, rather than the utterly uninformative “first” and “second”. (The prototypical example is for GPS coordinates; a class entitledLatLongis much more informative than aPair<Double, Double>.)