Functions { public static <T> Supplier<T> synchronizedSupplier(final Supplier<T> function) { if (isImmutable(function) && !(function instanceof AutoCloseable)) return function; return new ThreadSafeSupplier<T>(function); }  @SuppressWarnings("unchecked") static UnaryOperator<T> identity(); @SuppressWarnings("unchecked") static Function<T,Integer> zero(); static Function<T,Integer> unpartitioned(); static void closeFunction(Object function); static Function<T,R> synchronizedFunction(final Function<T,R> function); static Supplier<T> synchronizedSupplier(final Supplier<T> function); static Consumer<T> synchronizedConsumer(final Consumer<T> function); static BiFunction<T,U,R> synchronizedBiFunction(final BiFunction<T,U,R> function); static boolean isImmutable(Object function); static boolean isImmutableClass(Class<?> clazz); static Runnable delayedConsume(Consumer<T> consumer, T value); static Runnable runWithFinal(Runnable action, Runnable finalAction); @SuppressWarnings("unchecked") static Consumer<T> discard(); @SuppressWarnings("unchecked") static Predicate<T> alwaysTrue(); @SuppressWarnings("unchecked") static Predicate<T> alwaysFalse();  }
@Test public void testSynchronizedSupplier() { Supplier<Integer> f1 = () -> 3; assertSame(f1, Functions.synchronizedSupplier(f1)); int a = 7; Supplier<Integer> f2 = () -> a; assertSame(f2, Functions.synchronizedSupplier(f2)); int r2 = f2.get(); assertEquals(7, r2); int[] aa = new int[1]; Supplier<Integer> f3 = () -> aa[0]; assertNotSame(f3, Functions.synchronizedSupplier(f3)); aa[0] = 7; int r3 = f3.get(); assertEquals(7, r3); aa[0] = 13; r3 = f3.get(); assertEquals(13, r3); Supplier<Integer> f4 = new S4(); add = 127; try { f4.get(); fail("Expected IllegalMonitorStateException"); } catch (IllegalMonitorStateException e) { } Supplier<Integer> f4s = Functions.synchronizedSupplier(f4); assertNotSame(f4, f4s); int r4s = f4s.get(); assertEquals(127, r4s); Supplier<Integer> f5 = new S5(); assertNotSame(f5, Functions.synchronizedSupplier(f5)); }