Example usage for java.util.function Supplier get

List of usage examples for java.util.function Supplier get

Introduction

In this page you can find the example usage for java.util.function Supplier get.

Prototype

T get();

Source Link

Document

Gets a result.

Usage

From source file:com.vmware.photon.controller.common.xenon.ServiceHostUtils.java

/**
 * Generic wait function.//w ww.j a  v  a 2 s.co m
 */
public static <T> T waitForState(Supplier<T> supplier, Predicate<T> predicate, long waitIterationSleepMillis,
        long waitIterationCount, Runnable cleanup, String timeoutMessage) throws Throwable {
    for (int i = 0; i < waitIterationCount; i++) {
        T t = supplier.get();
        if (predicate.test(t)) {
            return t;
        }
        Thread.sleep(waitIterationSleepMillis);
    }

    if (cleanup != null) {
        cleanup.run();
    }

    logger.warn(timeoutMessage);
    throw new TimeoutException(timeoutMessage);
}

From source file:fr.landel.utils.commons.ObjectUtils.java

/**
 * Returns a default value if the object doesn't match the predicate.
 * /*from  w w  w .  j a  v a2 s .com*/
 * <pre>
 * ObjectUtils.defaultIf(Objects::nonNull, null, () -&gt; null)          = null
 * ObjectUtils.defaultIf(Objects::nonNull, null, () -&gt; "")            = ""
 * ObjectUtils.defaultIf(Objects::nonNull, null, () -&gt; "zz")          = "zz"
 * ObjectUtils.defaultIf(Objects::nonNull, "abc", () -&gt; *)            = "abc"
 * ObjectUtils.defaultIf(Objects::nonNull, Boolean.TRUE, () -&gt; *)     = Boolean.TRUE
 * </pre>
 * 
 * @param predicate
 *            the predicate (cannot be {@code null})
 * @param object
 *            the {@code Object} to test, may be {@code null}
 * @param defaultValueSupplier
 *            the default value supplier, cannot be {@code null}, may supply
 *            {@code null}
 * @param <T>
 *            the type of the object
 * @return {@code object} if it matches the predicate, defaultValue
 *         otherwise
 * @throws NullPointerException
 *             if {@code predicate} or {@code defaultValueSupplier} are
 *             {@code null}
 */
public static <T> T defaultIf(final Predicate<T> predicate, final T object,
        final Supplier<? extends T> defaultValueSupplier) {
    Objects.requireNonNull(predicate, PREDICATE_ERROR);
    Objects.requireNonNull(defaultValueSupplier, SUPPLIER_ERROR);

    return predicate.test(object) ? object : defaultValueSupplier.get();
}

From source file:fr.landel.utils.assertor.helper.HelperAssertor.java

public static <T> boolean isValid(final Stream<T> stream, final Predicate<T> predicate, final boolean all,
        final boolean not, final Supplier<Integer> size) {
    if (all && !not) { // ALL
        return stream.allMatch(predicate);
    } else if (!all && !not) { // ANY
        return stream.anyMatch(predicate);
    } else if (!all) { // NOT ANY
        return stream.noneMatch(predicate);
    } else { // NOT ALL
        return isValid(all, not, stream.filter(predicate).count(), size.get());
    }/*w  w w .j  a  va  2 s . c  o m*/
}

From source file:com.wrmsr.wava.transform.Transforms.java

public static Node uniquifyLabels(Node root, Supplier<Name> nameSupplier) {
    Map<Name, Name> remappedNames = new HashMap<>();
    return root.accept(new Visitor<Void, Node>() {
        @Override/*from   w ww  .java2  s  .co  m*/
        protected Node visitNode(Node node, Void context) {
            return reconstructNode(node,
                    node.getChildren().stream().map(c -> c.accept(this, context)).iterator());
        }

        private Node processNamedNode(Node node, Name name, BiFunction<Name, Node, Node> ctor, Void context) {
            Name prevName = remappedNames.get(name);
            Name newName = prevName == null ? name : nameSupplier.get();
            remappedNames.put(name, newName);
            Node child = Iterables.getOnlyElement(node.getChildren());
            Node ret = ctor.apply(newName, child.accept(this, context));
            if (prevName == null) {
                remappedNames.remove(name);
            } else {
                remappedNames.put(name, prevName);
            }
            return ret;
        }

        @Override
        public Node visitBreak(Break node, Void context) {
            return new Break(requireNonNull(remappedNames.get(node.getTarget())),
                    node.getValue().accept(this, context));
        }

        @Override
        public Node visitBreakTable(BreakTable node, Void context) {
            return new BreakTable(
                    node.getTargets().stream().map(remappedNames::get).map(Objects::requireNonNull).collect(
                            toImmutableList()),
                    requireNonNull(remappedNames.get(node.getDefaultTarget())),
                    node.getCondition().accept(this, context));
        }

        @Override
        public Node visitLabel(Label node, Void context) {
            return processNamedNode(node, node.getName(), Label::new, context);
        }

        @Override
        public Node visitLoop(Loop node, Void context) {
            return processNamedNode(node, node.getName(), Loop::new, context);
        }
    }, null);
}

From source file:nu.yona.server.util.TransactionHelper.java

@Transactional(value = TxType.REQUIRES_NEW)
public <T> T executeInNewTransaction(Supplier<T> supplier) {
    return supplier.get();
}

From source file:fi.helsinki.opintoni.web.rest.AbstractResource.java

protected final <T> ResponseEntity<T> response(Supplier<T> resultSupplier) {
    return response(resultSupplier.get());
}

From source file:org.javersion.store.jdbc.SpringTransactions.java

@Override
@Transactional(readOnly = true, isolation = READ_COMMITTED, propagation = REQUIRED)
public <T> T readOnly(Supplier<T> callback) {
    return callback.get();
}

From source file:org.javersion.store.jdbc.SpringTransactions.java

@Override
@Transactional(readOnly = false, isolation = READ_COMMITTED, propagation = REQUIRED)
public <T> T writeRequired(Supplier<T> callback) {
    return callback.get();
}

From source file:org.javersion.store.jdbc.SpringTransactions.java

@Override
@Transactional(readOnly = false, isolation = READ_COMMITTED, propagation = REQUIRES_NEW)
public <T> T writeNewRequired(Supplier<T> callback) {
    return callback.get();
}

From source file:org.javersion.store.jdbc.SpringTransactions.java

@Override
@Transactional(readOnly = false, isolation = READ_COMMITTED, propagation = MANDATORY)
public <T> T writeMandatory(Supplier<T> callback) {
    return callback.get();
}