LazyFunctionStream { public Stream<Supplier<R>> of (Function<T,R> f0 ) { List<Function<T,R>> functions = Lists.newArrayList(); functions.add( f0 ); registerFunctions(functions); return suppliers.stream(); } LazyFunctionStream(T input0);  LazyFunctionStream(T input0, T input1);  LazyFunctionStream(T input0, T input1, T input2);  LazyFunctionStream(List<T> inputs); Stream<Supplier<R>> of(Function<T,R> f0 ); Stream<Supplier<R>> of(Function<T,R> f0, Function<T,R> f1); Stream<Supplier<R>> of(Function<T,R> f0, Function<T,R> f1, Function<T,R> f2); Stream<Supplier<R>> of(Function<T,R> f0, Function<T,R> f1, Function<T,R> f2, Function<T,R> f3); Stream<Supplier<R>> of(Function<T,R> f0, Function<T,R> f1, Function<T,R> f2, Function<T,R> f3, Function<T,R> f4); Stream<Supplier<R>> of(Function<T,R> f0, Function<T,R> f1, Function<T,R> f2, Function<T,R> f3, Function<T,R> f4, Function<T,R> f5); Stream<Supplier<R>> of(Function<T,R> f0, Function<T,R> f1, Function<T,R> f2, Function<T,R> f3, Function<T,R> f4, Function<T,R> f5, Function<T,R> f6); Stream<Supplier<R>> of(Function<T,R> f0, Function<T,R> f1, Function<T,R> f2, Function<T,R> f3, Function<T,R> f4, Function<T,R> f5, Function<T,R> f6, Function<T,R> f7); Stream<Supplier<R>> of(Function<T,R> f0, Function<T,R> f1, Function<T,R> f2, Function<T,R> f3, Function<T,R> f4, Function<T,R> f5, Function<T,R> f6, Function<T,R> f7, Function<T,R> f8); Stream<Supplier<R>> of(Function<T,R> f0, Function<T,R> f1, Function<T,R> f2, Function<T,R> f3, Function<T,R> f4, Function<T,R> f5, Function<T,R> f6, Function<T,R> f7, Function<T,R> f8, Function<T,R> f9);  }
@Test public void testFirstSuccessful() throws Exception { TestFunction<String,String> f0 = new TestFunction<>( "0" ); TestFunction<String,String> f1 = new TestFunction<>( "1" ); LazyFunctionStream<String,String> lazyFunctionStream = new LazyFunctionStream<>( "hello" ); Stream<Supplier<String>> of = lazyFunctionStream.of( f0, f1 ); Optional<String> result = of.map( Supplier::get ) .filter( Objects::nonNull ) .findFirst(); assertEquals( "0", result.get() ); assertTrue( f0.called ); assertFalse( f1.called ); }
@Test public void testFirstFailed() throws Exception { TestFunction<String,String> f0 = new TestFunction<>( null ); TestFunction<String,String> f1 = new TestFunction<>( "1" ); LazyFunctionStream<String,String> lazyFunctionStream = new LazyFunctionStream<>( "hello" ); Stream<Supplier<String>> of = lazyFunctionStream.of( f0, f1 ); Optional<String> result = of.map( Supplier::get ) .filter( Objects::nonNull ) .findFirst(); assertEquals( "1", result.get() ); assertTrue( f0.called ); assertTrue( f1.called ); }
@Test public void testAllFailed() throws Exception { TestFunction<String,String> f0 = new TestFunction<>( null ); TestFunction<String,String> f1 = new TestFunction<>( null ); LazyFunctionStream<String,String> lazyFunctionStream = new LazyFunctionStream<>( "hello" ); Stream<Supplier<String>> of = lazyFunctionStream.of( f0, f1 ); Optional<String> result = of.map( Supplier::get ) .filter( Objects::nonNull ) .findFirst(); assertFalse( result.isPresent() ); assertTrue( f0.called ); assertTrue( f1.called ); }
@Test public void testWithNullInput() throws Exception { TestFunction<String,String> f0 = new TestFunction<>( "0" ); TestFunction<String,String> f1 = new TestFunction<>( "1" ); String input = null; LazyFunctionStream<String,String> lazyFunctionStream = new LazyFunctionStream<>(input); Stream<Supplier<String>> of = lazyFunctionStream.of( f0, f1 ); Optional<String> result = of.map( Supplier::get ) .filter( Objects::nonNull ) .findFirst(); assertTrue( ! result.isPresent() ); }