RFR [8037866] Replace the Fun class in tests with lambdas (original) (raw)

Ivan Gerasimov ivan.gerasimov at oracle.com
Mon Jun 2 11:58:07 UTC 2014


Hi Remi!

On 02.06.2014 14:46, Remi Forax wrote:

On 06/02/2014 06:36 AM, Martin Buchholz wrote: Thanks! Looks good. There might be more common infrastructure to take advantage of later. @FunctionalInterface is not really in the spirit of my super-compact stylistically odd test code, but OK. Another question is why not to use a functional interface with one parameter (Fun2?) or keep Fun and introduce Fun2. //interface Fun {void f() throws Throwable;} interface Fun2 {void f(T value) throws Throwable;} THROWS(BrokenBarrierException.class, barrier, x ->x.await(100, MILISECOND)); THROWS(IllegalStateException.class, it, Iterator::remove); THROWS accepts an array of Funs. Personally, I find more handy and less error-prone than providing pairs argument + lambda: If we have only one pair, we'll need to repeat THROWS several times, If we accept even-sized array, it can be messed up.

with the nice advantage that most of the time the lambda will be constant (a lambda that's not capture value from the outer context). In case it's needed, some helper class that transforms Fun2 into Fun could be used:

class Fun2Impl implements Fun { final T val; Fun2 fun2; public Fun2Impl(T val, Fun2 fun2) { this.val = val; this.fun2 = fun2; } public void f() { fun2.f(val); } }

...

THROWS(BrokenBarrierException.class, new Fun2Impl(barrier, x ->x.await(100, MILISECOND))); THROWS(IllegalStateException.class, new Fun2Impl(it, Iterator::remove));

It can be slightly beautified, of course. However, I don't think there would be much use of it, as it can already be achieved through Fun, making 'x' and 'it' final.

Sincerely yours, Ivan

Rémi

On Sun, Jun 1, 2014 at 5:57 PM, Ivan Gerasimov <ivan.gerasimov at oracle.com> wrote: Hello! Would you please help review the fix, which replaces the auxiliary Fun class with the same named functional interface? The anonymous classes instantiations are replaced with lambdas. BUGURL: https://bugs.openjdk.java.net/browse/JDK-8037866 WEBREV: http://cr.openjdk.java.net/~igerasim/8037866/0/webrev/ Sincerely yours, Ivan



More information about the core-libs-dev mailing list