Is there a bi-directional function in Guava?

So I need a function with an API like

interface BiFunction<A, B> {
    B aToB(A input);
    A bToA(B input);
}

Did Guava provide such smt. If not, what names would you suggest for the aToB/ methods bToA?

+5
source share
3 answers

No, there is nothing like that in Guava. Maybe something like this (see this question , as well as this question for some related discussions).

For names, I do not know what would be better, but I would prefer something like applyand applyInverseover aToBand bToA.

+6
source

As of the end of 2014, Guava 19.0 has:

https://google.imtqy.com/guava/releases/19.0/api/docs/com/google/common/base/Converter.html

B b = Converter.convert(a);
A a = Converter.reverse().convert(b);

:

protected abstract A    doBackward(B b)
protected abstract B    doForward(A a)
+4

As for the suggested names, it depends on how you want to go. Some existing examples:

interface Codec <I, O> {
    public O encode(I in);
    public I decode(O out);
}

interface Format <R, F> {
    public F format(R raw);
    public R parse(F formatted);
}

If you want it to be super generic, I would just use aToBand bToA, as you suggested. Do not overload them since you use Generics, and do not use toA, because you do not convert this function, you convert the argument.

+2
source

All Articles