@Test public void test_of_iterator() { List<Integer> list = new ArrayList<Integer>(); list.add(1); list.add(2); list.add(3); int result = Flow.of(list.iterator()).first().get(); Assert.assertEquals(1, result); }
@Test public void test_of_iterable_2() { List<Integer> list = new ArrayList<Integer>(); list.add(1); list.add(2); list.add(3); int result = Flow.of(list).first().get(); Assert.assertEquals(1, result); }
@Test public void test_stop_after_first() { final AtomicInteger counter = new AtomicInteger(); Assert.assertEquals(Integer.valueOf(1), Flow.of(1, 2, 3).each(new Consumer<Integer>() { @Override public void consume(Integer integer) { counter.incrementAndGet(); } }).first().get()); Assert.assertEquals(1, counter.get()); }
@Test public void test_each_with_index_3() { final StringBuilder sb = new StringBuilder(); Flow.of(2, 4, 6) .each(new ConsumerWithInt<Integer>() { @Override public void consume(int i, Integer integer) { if (sb.length() != 0) sb.append(", "); sb.append(i); } }) .first() .get(); Assert.assertEquals("0", sb.toString()); }
@Test public void test_first() { Assert.assertEquals(Integer.valueOf(1), Flow.of(1, 2, 3).first().get()); }
@Test public void test_each_3() { final AtomicInteger counter = new AtomicInteger(); Flow.of(1, 2, 3) .groupBy(new Mapper<Integer, Boolean>() { @Override public Boolean map(Integer integer) { return integer % 2 == 0; } }) .each(new Consumer<FlowGroup<Boolean, Integer>>() { @Override public void consume(FlowGroup<Boolean, Integer> group) { counter.incrementAndGet(); } }) .first() .get(); Assert.assertEquals(1, counter.get()); }
@Test public void test_grouping_4() { final AtomicInteger counter = new AtomicInteger(); Integer result = Flow.of(1, 2, 3) .groupBy(new Mapper<Integer, Boolean>() { @Override public Boolean map(Integer it) { counter.incrementAndGet(); return it % 2 == 0; } }) .flatMap(new Mapper<FlowGroup<Boolean, Integer>, FlowProcessOnEnd<?, Integer>>() { @Override public FlowProcessOnEnd<?, Integer> map(FlowGroup<Boolean, Integer> group) { return group.first(); } }) .first() .get(); Assert.assertEquals(Integer.valueOf(1), result); Assert.assertEquals(1, counter.get()); }
@Test public void test_each_with_index_4() { final StringBuilder sb = new StringBuilder(); Flow.of(2, 4, 6) .groupBy(new Mapper<Integer, Integer>() { @Override public Integer map(Integer integer) { return integer; } }) .each(new ConsumerWithInt<FlowGroup<Integer, Integer>>() { @Override public void consume(int i, FlowGroup<Integer, Integer> integerIntegerFlowGroup) { if (sb.length() != 0) sb.append(", "); sb.append(i); } }) .first() .get(); Assert.assertEquals("0", sb.toString()); }
@Test public void test_or() { Assert.assertEquals(Integer.valueOf(-1), Flow.of(1, 2, 3) .filter(new Filter<Integer>() { @Override public boolean allow(Integer integer) { return integer > 10; } }) .or(-1) .first() .get()); }
@Test public void test_or_3() { Assert.assertEquals(Integer.valueOf(1), Flow.of(1, 2, 3) .filter(new Filter<Integer>() { @Override public boolean allow(Integer integer) { return integer > 0; } }) .or(1) .first() .get()); }
@Test public void test_of_flows_2() { Assert.assertEquals(Integer.valueOf(1), Flow.of(Flow.of(1), Flow.of(2, 3)).first().get()); }
@Test public void test_or_4() { Assert.assertEquals(Integer.valueOf(1), Flow.of(1, 2, 3) .filter(new Filter<Integer>() { @Override public boolean allow(Integer integer) { return integer > 0; } }) .or(new Supplier<Integer>() { @Override public Integer supply() { return -1; } }) .first() .get()); }
@Test public void test_or_2() { Assert.assertEquals(Integer.valueOf(-1), Flow.of(1, 2, 3) .filter(new Filter<Integer>() { @Override public boolean allow(Integer integer) { return integer > 10; } }) .or(new Supplier<Integer>() { @Override public Integer supply() { return -1; } }) .first() .get()); }
@Test public void test_of_single() { Assert.assertEquals("abc", Flow.ofSingle(new char[]{'a', 'b', 'c'}) .map(new Mapper<char[], String>() { @Override public String map(char[] chars) { return new String(chars); } }) .first() .get() ); } }
@Test public void test_flow_of_one() { Assert.assertEquals("A", Flow.of("a").map(new Mapper<String, String>() { @Override public String map(String s) { return s.toUpperCase(); } }).first().get()); }
@Test public void test_or_9() { Assert.assertEquals(Integer.valueOf(-1), Flow.of(1, 2, 3) .filter(new Filter<Integer>() { @Override public boolean allow(Integer integer) { return integer > 10; } }) .first() .or(-1) .first() .get()); }
@Test public void test_max_3() { Assert.assertEquals(Integer.valueOf(6), Flow.of(1, 2, 3) .max() .map(new Mapper<Integer, Integer>() { @Override public Integer map(Integer integer) { return integer * 2; } }).first().get() ); Assert.assertEquals(Integer.valueOf(6), Flow.of(3, 2, 1) .max() .map(new Mapper<Integer, Integer>() { @Override public Integer map(Integer integer) { return integer * 2; } }).first().get()); }
@Test public void test_or_10() { Assert.assertEquals(Integer.valueOf(1), Flow.of(1, 2, 3) .filter(new Filter<Integer>() { @Override public boolean allow(Integer integer) { return integer > 0; } }) .first() .or(-1) .first() .get()); }
@Test public void test_or_5() { Assert.assertEquals(Integer.valueOf(-2), Flow.of(1, 2, 3) .filter(new Filter<Integer>() { @Override public boolean allow(Integer integer) { return integer > 10; } }) .or(-1) .map(new Mapper<Integer, Integer>() { @Override public Integer map(Integer integer) { return integer * 2; } }) .first() .get()); }
@Test public void test_or_8() { Assert.assertEquals(null, Flow.of(1, 2, 3) .filter(new Filter<Integer>() { @Override public boolean allow(Integer integer) { return integer > 10; } }) .or((Integer) null) .first() .get()); }