Example usage for org.apache.commons.functor.core.collection FilteredIterable of

List of usage examples for org.apache.commons.functor.core.collection FilteredIterable of

Introduction

In this page you can find the example usage for org.apache.commons.functor.core.collection FilteredIterable of.

Prototype

public static <T> FilteredIterable<T> of(Iterable<T> iterable) 

Source Link

Document

Get a FilteredIterable of iterable.

Usage

From source file:therian.OperatorsTest.java

@Test
public void testSupporting() {
    @SuppressWarnings("unchecked")
    final List<Surgeon<? extends Surgery>> surgeons = Arrays.asList(new Surgeon<Appendectomy>() {
    }, new Surgeon<Tonsillectomy>() {
    });/*from w w w. ja  v a2  s.com*/
    assertEquals(1, IteratorToGeneratorAdapter
            .adapt(FilteredIterable.of(surgeons).retain(Operators.supporting(new Appendectomy())).iterator())
            .toCollection().size());
    assertEquals(1, IteratorToGeneratorAdapter
            .adapt(FilteredIterable.of(surgeons).retain(Operators.supporting(new Tonsillectomy())).iterator())
            .toCollection().size());
}

From source file:therian.TherianContext.java

/**
 * Performs the specified operation by invoking any compatible operator until the operation is marked as successful,
 * then returns the result./* www  .  j  a v a  2s . c  o m*/
 * 
 * @param operation
 * @param <RESULT>
 * @param <OPERATION>
 * @return result if available
 * @throws IllegalArgumentException
 *             if no operators are successful
 */
public final synchronized <RESULT, OPERATION extends Operation<RESULT>> RESULT eval(OPERATION operation) {
    final TherianContext originalContext = getCurrentInstance();
    if (originalContext != this) {
        CURRENT_INSTANCE.set(this);
    }
    try {
        if (operations.contains(operation)) {
            // TODO handle better, e.g. copy of recursive graph
            throw new OperationException(operation, "recursive operation detected");
        }
        operations.push(operation);

        try {
            operation.init();
            if (!operation.isSuccessful()) {
                for (Operator<?> operator : FilteredIterable.of(getTypedContext(Therian.class).getOperators())
                        .retain(Operators.supporting(operation))) {
                    // already determined that operator supports operation:
                    evalRaw(operation, operator);
                    if (operation.isSuccessful()) {
                        break;
                    }
                }
            }
            return operation.getResult();
        } finally {
            final Operation<?> opOnPop = operations.pop();
            Validate.validState(opOnPop == operation,
                    "operation stack out of whack; found %s where %s was expected", opOnPop, operation);
        }
    } finally {
        // javadoc not clear on whether set(null) is truly equivalent to
        // remove():
        if (originalContext == null) {
            CURRENT_INSTANCE.remove();
        } else if (originalContext != this) {
            // restore original context in the unlikely event that multiple
            // contexts are being used on the same
            // thread:
            CURRENT_INSTANCE.set(originalContext);
        }
    }
}