/** * Creates a lazy Flow from the {@link Iterator} obtained from the iterable. The Flow * will be threadsafe as long as the iterable yields a new Iterator on each invocation <em>and</em> the underlying * iterable object is not modified while the Flow is evaluating. * In other words, not extremely threadsafe. */ public static <T> Flow<T> flow(Iterable<T> iterable) { assert iterable != null; return flow(iterable.iterator()); }
/** Does nothing; returns this empty list (as a Flow<X>). */ @Override public <X> Flow<X> map(Mapper<T, X> mapper) { return F.emptyFlow(); }
/** * As with {@link #endsWith(String)} but ignores case. * * @since 5.3 */ public static Predicate<String> endsWithIgnoringCase(String suffix) { return endsWith(suffix, true); }
@Test public void select_and_filter() { Predicate<String> combinedp = F.toPredicate(F.combine(stringToLength, toEven)); Mapper<String, String> identity = F.identity(); Predicate<String> isNull = F.isNull(); // Converting to null and then filtering out nulls is the hard way to do filter or remove, // but exercises the code we want to test. List<String> filtered = F.flow("Mary", "had", "a", "little", "lamb").map(F.select(combinedp, identity)) .remove(isNull).toList(); assertListsEquals(filtered, "Mary", "little", "lamb"); }
@Test public void numeric_comparison() { List<Integer> input = Arrays.asList(1, 2, 3, 4, 5, 6, 7); assertEquals(F.flow(input).filter(F.eq(3)).toList(), Arrays.asList(3)); assertEquals(F.flow(input).filter(F.neq(3)).toList(), Arrays.asList(1, 2, 4, 5, 6, 7)); assertEquals(F.flow(input).filter(F.lt(3)).toList(), Arrays.asList(1, 2)); assertEquals(F.flow(input).filter(F.lteq(3)).toList(), Arrays.asList(1, 2, 3)); assertEquals(F.flow(input).filter(F.gteq(3)).toList(), Arrays.asList(3, 4, 5, 6, 7)); }
@Test public void toString_mapper() { Flow<Integer> flow = F.flow(1, 2, 3); assertFlowValues(flow.map(F.<Integer> stringValueOf()), "1", "2", "3"); }
@Test public void case_insensitive_suffix_predicate() { Flow<String> names = F.flow("Ted", "Charly", "Fred", "Anna"); assertFlowValues(names.filter(F.endsWithIgnoringCase("RED")), "Fred"); }
/** * Creates a lazy Flow that returns integers in the given range. The range starts * with the lower value and counts by 1 up to the upper range (which is not part of * the Flow). If lower equals upper, the Flow is empty. If upper is less than lower, * the Flow counts down instead. * * @param lower * start of range (inclusive) * @param upper * end of range (exclusive) */ public static Flow<Integer> range(int lower, int upper) { if (lower == upper) return F.emptyFlow(); if (lower < upper) return lazy(new LazyRange(lower, upper, 1)); return lazy(new LazyRange(lower, upper, -1)); }
@Override public Flow<T> removeNulls() { return remove(F.isNull()); }