Java Code Examples for com.googlecode.totallylazy.Sequence#isEmpty()

The following examples show how to use com.googlecode.totallylazy.Sequence#isEmpty() . 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: Reflection.java    From totallylazy with Apache License 2.0 6 votes vote down vote up
/**
 * Finds a static method on actualType that takes a parameter of the same type as value and return
 * an instance of actualType.
 * <p>
 * Calls that static method with value and returns the result.
 **/
public static <T> T valueOf(Class<T> actualType, Object value) {
    Sequence<Method> candidates = allMethods(actualType)
            .filter(and(modifier(STATIC),
                    where(m -> m.getParameterTypes().length, is(1)))).
                    filter(and(
                            where(returnType(), matches(actualType)),
                            where(m -> m.getParameterTypes()[0], (t) -> t.isAssignableFrom(value.getClass()))));

    if (candidates.isEmpty()) {
        throw new NoSuchElementException(format(
                "Cannot create %s from '%s' (%s). You need to add a static constructor method to %s that accepts a %s",
                actualType.getCanonicalName(), value, value.getClass().getCanonicalName(),
                actualType.getSimpleName(), value.getClass().getSimpleName()));
    }

    return actualType.cast(candidates.pick(optional(invokeOn(null, value))));
}
 
Example 2
Source File: CommandCompleter.java    From java-repl with Apache License 2.0 6 votes vote down vote up
public CompletionResult call(String expression) throws Exception {
    Sequence<String> parts = sequence(expression.split(" "));

    if (parts.isEmpty()) {
        return new CompletionResult(expression, 0, empty(CompletionCandidate.class));
    }

    if (command.equals(parts.head())) {
        String nextCommandPart = parts.tail().headOption().getOrElse("");
        return new CompletionResult(expression, candidates.isEmpty() ? 0 : command.length() + 1, candidates.filter(startsWith(nextCommandPart)).map(asCompletionCandidate()));
    }

    if (command.startsWith(parts.head())) {
        return new CompletionResult(expression, 0, one(asCompletionCandidate().apply(command)));
    }

    return new CompletionResult(expression, 0, empty(CompletionCandidate.class));
}
 
Example 3
Source File: Main.java    From java-repl with Apache License 2.0 6 votes vote down vote up
/**
 * Returns a root that matches all the {@link String} elements of the specified {@link List},
 * or null if there are no commonalities. For example, if the list contains
 * <i>foobar</i>, <i>foobaz</i>, <i>foobuz</i>, the method will return <i>foob</i>.
 */
private String getUnambiguousCompletions(final Sequence<CompletionCandidate> candidates) {
    if (candidates == null || candidates.isEmpty()) {
        return null;
    }

    // convert to an array for speed
    String[] strings = candidates.map(CompletionCandidate::value).toArray(new String[candidates.size()]);

    String first = strings[0];
    StringBuilder candidate = new StringBuilder();

    for (int i = 0; i < first.length(); i++) {
        if (startsWith(first.substring(0, i + 1), strings)) {
            candidate.append(first.charAt(i));
        } else {
            break;
        }
    }

    return candidate.toString();
}
 
Example 4
Source File: Types.java    From totallylazy with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public static boolean withInUpperBounds(Type concrete, Sequence<Type> upperBounds) {
    if (upperBounds.isEmpty()) {
        return true;
    }
    if (Numbers.equalTo(upperBounds.size(), 1)) {
        return (classOf(upperBounds.first())).isAssignableFrom(classOf(concrete));
    }
    throw new UnsupportedOperationException();
}
 
Example 5
Source File: Types.java    From totallylazy with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public static boolean withInLowerBounds(Type concrete, Sequence<Type> lowerBounds) {
    if (lowerBounds.isEmpty()) {
        return true;
    }
    if (Numbers.equalTo(lowerBounds.size(), 1)) {
        return (classOf(concrete)).isAssignableFrom(classOf(lowerBounds.first()));
    }
    throw new UnsupportedOperationException();
}
 
Example 6
Source File: AndPredicate.java    From totallylazy with Apache License 2.0 5 votes vote down vote up
public static <T> LogicalPredicate<T> and(Iterable<? extends Predicate<? super T>> predicates) {
    Sequence<Predicate<T>> sequence = Sequences.sequence(predicates).<Predicate<T>>unsafeCast().
            flatMap(AndPredicate.<T>asPredicates());
    if (sequence.exists(instanceOf(AlwaysFalse.class))) return Predicates.alwaysFalse();

    Sequence<Predicate<T>> collapsed = sequence.
            filter(instanceOf(AlwaysTrue.class).not());
    if (collapsed.isEmpty()) return Predicates.alwaysTrue();
    if (collapsed.size() == 1) return logicalPredicate(collapsed.head());
    if (collapsed.forAll(instanceOf(Not.class)))
        return Predicates.not(Predicates.<T>or(sequence.<Not<T>>unsafeCast().map(Not.functions.<T>predicate())));
    return new AndPredicate<T>(collapsed);
}
 
Example 7
Source File: OrPredicate.java    From totallylazy with Apache License 2.0 5 votes vote down vote up
public static <T> LogicalPredicate<T> or(Iterable<? extends Predicate<? super T>> predicates) {
    Sequence<Predicate<T>> sequence = Sequences.sequence(predicates).<Predicate<T>>unsafeCast().
            flatMap(OrPredicate.<T>asPredicates());
    if (sequence.exists(instanceOf(AlwaysTrue.class))) return Predicates.alwaysTrue();

    Sequence<Predicate<T>> collapsed = sequence.
            filter(instanceOf(AlwaysFalse.class).not());
    if (collapsed.isEmpty()) return Predicates.alwaysFalse();
    if (collapsed.size() == 1) return logicalPredicate(collapsed.head());
    if (collapsed.forAll(instanceOf(Not.class)))
        return Predicates.not(Predicates.<T>and(sequence.<Not<T>>unsafeCast().map(Not.functions.<T>predicate())));
    return new OrPredicate<T>(sequence);
}
 
Example 8
Source File: DomConverter.java    From totallylazy with Apache License 2.0 5 votes vote down vote up
static Node children(Node parent, Sequence<Context> contexts) throws XMLStreamException {
    if(contexts.isEmpty()) return parent;
    for (Context child : contexts.filter(xpath(XPath.child(node())))) {
        if(child.isText()) parent.appendChild(parent.getOwnerDocument().createTextNode(child.text()));
        if(child.isElement()) children(child(parent, child), child.relative());
    }
    return parent;
}
 
Example 9
Source File: ShowHistory.java    From java-repl with Apache License 2.0 5 votes vote down vote up
public void execute(String expression) {
    Integer limit = parseNumericCommand(expression).second().getOrElse(history.items().size());
    Sequence<String> numberedHistory = numberedHistory(history).reverse().take(limit).reverse();

    if (!numberedHistory.isEmpty()) {
        logger.success(listValues("History", numberedHistory));
    } else {
        logger.success("No history.");
    }
}
 
Example 10
Source File: ValidationResult.java    From totallylazy with Apache License 2.0 4 votes vote down vote up
public ValidationResult add(String key, Iterable<String> messages) {
    Sequence<String> newMessages = messages(key).join(messages);
    if(newMessages.isEmpty())
        return this;
    return new ValidationResult(this.messages.insert(key, newMessages));
}