@Test public void testIfPredicateAndSupplier() { IfFunctionsTest.checkTF(Functionals.ifPredicate(Predicates.equalTo(false)).get(Suppliers.ofInstance("F")) .ifEquals(true).value("T").defaultGet(Suppliers.ofInstance("?")).build(), "?"); }
@Test public void testIf() { IfFunctionsTest.checkTF(Functionals.ifEquals(false).value("F").ifEquals(true).value("T").defaultValue("?").build(), "?"); }
public IfFunctionBuilderApplying<I,O> ifPredicate(Predicate<I> test) { return new IfFunctionBuilderApplying<I,O>(this, test); } public IfFunctionBuilderApplying<I,O> ifEquals(I test) {
@Test public void testIfNoBuilder() { IfFunctionsTest.checkTF(Functionals.ifEquals(false).value("F").ifEquals(true).value("T").defaultValue("?"), "?"); }
@Test public void testNoDefault() { checkTF(IfFunctions.ifEquals(false).value("F").ifEquals(true).value("T").build(), null); }
@Test public void testNoBuilder() { checkTF(IfFunctions.ifEquals(false).value("F").ifEquals(true).value("T").defaultValue("?"), "?"); }
@Test public void testIsImmutableAndSupportsReplace() { IfFunctionBuilder<Boolean, String> f = IfFunctions.ifEquals(false).value("F").ifEquals(true).value("T").defaultValue("?"); IfFunctionBuilder<Boolean, String> f2 = f.ifEquals(false).value("false").defaultValue("X"); IfFunctionBuilder<Boolean, String> f3 = f2.ifEquals(false).value("F"); checkTF(f, "?"); checkTF(f3, "X"); Assert.assertEquals(f2.apply(false), "false"); }
@Test public void testCommonUsage() { checkTF(IfFunctions.ifEquals(false).value("F").ifEquals(true).value("T").defaultValue("?").build(), "?"); }
public IfFunctionBuilder<I,O> value(O value) { return apply(new Functionals.ConstantFunction<I,O>(value, value)); } @SuppressWarnings("unchecked")
@Test public void testWithCast() { Function<Boolean, String> f = IfFunctions.ifEquals(false).value("F").ifEquals(true).value("T").defaultValue("?").build(); checkTF(f, "?"); }
@Test public void testPredicateAndSupplier() { // we cannot use checkTF here as an IntelliJ issues causes the project to fail to launch as IntelliJ does not // recognize the return value of IfFunctions.ifPredicate as Function<Boolean, String> Function function = IfFunctions.ifPredicate(Predicates.equalTo(false)).get(Suppliers.ofInstance("F")) .ifEquals(true).value("T").defaultGet(Suppliers.ofInstance("?")).build(); Assert.assertEquals(function.apply(true), "T"); Assert.assertEquals(function.apply(false), "F"); Assert.assertEquals(function.apply(null), "?"); }
@SuppressWarnings("unchecked") public IfFunctionBuilder<I,O> get(Supplier<? extends O> supplier) { return apply(Functions.forSupplier(supplier)); } public IfFunctionBuilder<I,O> apply(Function<? super I,? extends O> function) {