@Override public List map(FlowGroup flowGroup) { return (List) flowGroup.toList().get(); } };
public <C extends Collection<B>> FlowCollect<B, C> collect(C collection) { return then(new FlowCollect<B, C>(collection)); }
@Override protected void onEnd() { Collections.sort((List<Comparable>) result); super.onEnd(); } }
@Override public List map(FlowGroup flowGroup) { return (List) flowGroup.toList().get(); } };
@Override protected void onEnd() { Collections.sort(result, comparator); super.onEnd(); } }
public <C extends Collection<B>> FlowCollect<B, C> collect(C collection) { return then(new FlowCollect<B, C>(collection)); }
@Test public void test_of_iterator_2() { List<Integer> list = new ArrayList<Integer>(); list.add(1); list.add(2); list.add(3); List<Integer> result = Flow.of(list.iterator()).toList().get(); Assert.assertEquals(3, result.size()); Assert.assertEquals(Integer.valueOf(1), result.get(0)); Assert.assertEquals(Integer.valueOf(2), result.get(1)); Assert.assertEquals(Integer.valueOf(3), result.get(2)); }
@Override protected void onEnd() { Collections.sort((List<Comparable>) result); super.onEnd(); } }
@Override public List<String> map(FlowGroup<Boolean, Integer> group) { return group.map(new Mapper<Integer, String>() { @Override public String map(Integer integer) { return integer.toString(); } }).toList().get(); } })
@Override protected void onEnd() { Collections.sort(result, comparator); super.onEnd(); } }
@Test public void test_of_flows() { List<Integer> result = Flow.of(Flow.of(1), Flow.of(2, 3)).toList().get(); Assert.assertEquals(3, result.size()); Assert.assertEquals(Integer.valueOf(1), result.get(0)); Assert.assertEquals(Integer.valueOf(2), result.get(1)); Assert.assertEquals(Integer.valueOf(3), result.get(2)); }
@Test public void test_async_executor_service_2() { long time = System.currentTimeMillis(); List<String> result = Flow.of("a", "b", "c") .async(Executors.newFixedThreadPool(2), 1, new Mapper<String, Flow<String>>() { @Override public Flow<String> map(String s) { try { Thread.sleep(20); } catch (InterruptedException e) { e.printStackTrace(); } return Flow.of(s.toUpperCase()); } }).toList().get(); time = System.currentTimeMillis() - time; Assert.assertTrue(time > 40); Assert.assertEquals(3, result.size()); Assert.assertTrue(result.containsAll(Arrays.asList("A", "B", "C"))); }
@Test public void test_filter() { List<Integer> result = Flow.of(1, 2, 3, 4) .filter(new Filter<Integer>() { @Override public boolean allow(Integer integer) { return integer % 2 == 0; } }).toList().get(); Assert.assertEquals(2, result.size()); Assert.assertEquals(Integer.valueOf(2), result.get(0)); Assert.assertEquals(Integer.valueOf(4), result.get(1)); }
@Test public void test_async_limit_queue() { List<String> result = Flow.of("a", "b", "c").async(1, new Mapper<String, Flow<String>>() { @Override public Flow<String> map(String s) { return Flow.of(s.toUpperCase()); } }).toList().get(); Assert.assertEquals(3, result.size()); Assert.assertTrue(result.containsAll(Arrays.asList("A", "B", "C"))); }
@Test public void test_async_executor_service() { long time = System.currentTimeMillis(); List<String> result = Flow.of("a", "b", "c") .async(Executors.newFixedThreadPool(1), 1, new Mapper<String, Flow<String>>() { @Override public Flow<String> map(String s) { try { Thread.sleep(20); } catch (InterruptedException e) { e.printStackTrace(); } return Flow.of(s.toUpperCase()); } }).toList().get(); time = System.currentTimeMillis() - time; Assert.assertTrue(time > 60); Assert.assertEquals(3, result.size()); Assert.assertTrue(result.containsAll(Arrays.asList("A", "B", "C"))); }
@Test public void test_flatMap_3() { ArrayList<Integer> integers = Flow.of(1, 2, 3) .flatMap(new Mapper<Integer, Flow<Integer>>() { @Override public Flow<Integer> map(Integer i) { return Flow.of(i, i); } }) .toList().get(); Assert.assertEquals(6, integers.size()); Assert.assertEquals(Integer.valueOf(1), integers.get(0)); Assert.assertEquals(Integer.valueOf(1), integers.get(1)); Assert.assertEquals(Integer.valueOf(2), integers.get(2)); Assert.assertEquals(Integer.valueOf(2), integers.get(3)); Assert.assertEquals(Integer.valueOf(3), integers.get(4)); Assert.assertEquals(Integer.valueOf(3), integers.get(5)); }
@Test public void test_async() { List<String> result = Flow.of("a", "b", "c").async(new Mapper<String, Flow<String>>() { @Override public Flow<String> map(String s) { return Flow.of(s.toUpperCase()); } }).toList().get(); Assert.assertEquals(3, result.size()); Assert.assertTrue(result.containsAll(Arrays.asList("A", "B", "C"))); }
@Test public void test_merge() { List<Integer> result = Flow.of(1, 2, 3, 4, 5, 6) .groupBy(new Mapper<Integer, Boolean>() { @Override public Boolean map(Integer integer) { return integer % 2 == 0; } }) .merge() .toList() .get(); Assert.assertEquals(6, result.size()); Assert.assertEquals(Integer.valueOf(1), result.get(0)); Assert.assertEquals(Integer.valueOf(2), result.get(1)); Assert.assertEquals(Integer.valueOf(3), result.get(2)); Assert.assertEquals(Integer.valueOf(4), result.get(3)); Assert.assertEquals(Integer.valueOf(5), result.get(4)); Assert.assertEquals(Integer.valueOf(6), result.get(5)); }
@Test public void test_map_to_null() { List<String> result = Flow.of(1, 2, 3) .map(new Mapper<Integer, String>() { @Override public String map(Integer integer) { return null; } }).toList().get(); Assert.assertEquals(0, result.size()); }
@Test public void test_map() { List<String> result = Flow.of(1, 2, 3) .map(new Mapper<Integer, String>() { @Override public String map(Integer integer) { return integer.toString(); } }).toList().get(); Assert.assertEquals(3, result.size()); Assert.assertEquals("1", result.get(0)); Assert.assertEquals("2", result.get(1)); Assert.assertEquals("3", result.get(2)); }