Java Code Examples for com.googlecode.totallylazy.Sequences#sequence()

The following examples show how to use com.googlecode.totallylazy.Sequences#sequence() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GroupByTransducer.java    From totallylazy with Apache License 2.0 5 votes vote down vote up
@Override
default Receiver<T> apply(Receiver<Group<K, T>> receiver) {
    return new Receiver<T>() {
        private final Map<K, Group<K, T>> groups = new ConcurrentHashMap<>();

        @Override
        public State start() {
            receivers().each(Receiver::start);
            return receiver.start();
        }

        @Override
        public State next(T item) {
            groups.computeIfAbsent(keyExtractor().apply(item), k -> {
                Group<K, T> group = new Group<>(k);
                receiver.next(group);
                return group;
            }).next(item);
            return State.Continue;
        }

        @Override
        public State error(Throwable throwable) {
            receivers().each(o -> o.error(throwable));
            return receiver.error(throwable);
        }

        @Override
        public void finish() {
            receivers().each(Receiver::finish);
            receiver.finish();
        }

        private Sequence<Receiver<T>> receivers() {
            return Sequences.sequence(groups.values());
        }
    };
}
 
Example 2
Source File: Xml.java    From totallylazy with Apache License 2.0 5 votes vote down vote up
private static Sequence<Node> internalSelectNodes(final Node node, final String expression) {
    try {
        return sequence((NodeList) xpathExpression(expression).evaluate(node, XPathConstants.NODESET));
    } catch (XPathExpressionException e) {
        try {
            String nodeAsString = (String) xpathExpression(expression).evaluate(node, XPathConstants.STRING);
            return Sequences.<Node>sequence(documentFor(node).createTextNode(nodeAsString));
        } catch (XPathExpressionException ignore) {
            throw new IllegalArgumentException(String.format("Failed to compile xpath '%s'", expression), e);
        }
    }
}
 
Example 3
Source File: NumbersTest.java    From totallylazy with Apache License 2.0 5 votes vote down vote up
/**
 * Test case for issue #14
 */
@Test
public void supportsShort() {
    assertThat(numbers((short)0, (short)0 ,(short)1).reduceLeft(Numbers.maximum), NumberMatcher.is((short) 1));

    Sequence<Short> s = Sequences.sequence((short)0, (short)0 ,(short)1);
    Number result = s.reduceLeft(Numbers.maximum());
    assertEquals(Short.class, result.getClass());
    assertThat(result, NumberMatcher.is(Short.valueOf((short) 1)));
}
 
Example 4
Source File: ListParser.java    From totallylazy with Apache License 2.0 4 votes vote down vote up
static <A> ListParser<A> list(final Iterable<? extends Parser<? extends A>> parsers) {
    return new ListParser<A>(Sequences.sequence(parsers));
}
 
Example 5
Source File: PredicatesParser.java    From totallylazy with Apache License 2.0 4 votes vote down vote up
static Parser<String> string(Iterable<? extends Predicate<? super Character>> predicates) {
    return new PredicatesParser(Sequences.sequence(predicates));
}
 
Example 6
Source File: PredicatesParser.java    From totallylazy with Apache License 2.0 4 votes vote down vote up
@SafeVarargs
static Parser<String> string(Predicate<? super Character>... predicates) {
    return new PredicatesParser(Sequences.sequence(predicates));
}
 
Example 7
Source File: Regex.java    From totallylazy with Apache License 2.0 4 votes vote down vote up
public Sequence<String> split(CharSequence value) {
    return Sequences.sequence(pattern.split(value));
}
 
Example 8
Source File: AbstractMap.java    From totallylazy with Apache License 2.0 4 votes vote down vote up
@Override
public Sequence<Pair<K, V>> toSequence() {
    return Sequences.sequence(this);
}