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:Main.java

/**
 * Create a {@link Collection} of {@link E} and add {@code elements} to the {@link Collection}.
 *
 * @param factory  Collection factory method.
 * @param elements Elements to add to collection.
 * @param <E>      Element type.//from  w  w  w  .j  av a2s  . c  o m
 * @param <T>      Collection type.
 * @return {@link T Collection} of {@link E} with {@code elements}.
 */
@SafeVarargs
public static <E, T extends Collection<E>> T collectionOf(Supplier<T> factory, E... elements) {
    T collection = factory.get();

    Collections.addAll(collection, elements);

    return collection;
}

From source file:Main.java

public static <T> T get(String key, Supplier<T> supplierOnNull) {
    return ((T) local.get().computeIfAbsent(key, k -> supplierOnNull.get()));
}

From source file:com.diffplug.gradle.ConfigMisc.java

/** Creates an XML string from a groovy.util.Node. */
public static Supplier<byte[]> xml(Supplier<Node> node) {
    return () -> {
        Node root = node.get();
        return XmlUtil.serialize(root).getBytes(StandardCharsets.UTF_8);
    };/*from ww  w .j  a  v  a  2 s.  co m*/
}

From source file:enumj.NanoTimer.java

public static <R> long nanos(Supplier<R> action, Supplier<String> msg) {
    print(msg.get());
    final long t0 = System.nanoTime();
    final R result = action.get();
    return System.nanoTime() - t0;
}

From source file:Main.java

/**
 * Create a {@link T Sequence} of {@link E}.
 *
 * @param factory  Factory method to create sequence.
 * @param adder    Sequence adding method.
 * @param elements Array Elements to add to sequence.
 * @param <E>      Element type.// w  ww.j a  va 2 s . co  m
 * @param <T>      Sequence type (non-java)
 * @return Created {@link T Sequence} with {@code elements}.
 */
@SafeVarargs
public static <E, T> T sequenceOf(Supplier<T> factory, BiConsumer<T, E> adder, E... elements) {
    T sequence = factory.get();

    for (E element : elements) {
        adder.accept(sequence, element);
    }

    return sequence;
}

From source file:Main.java

public static <K, V> V getOrAdd(Map<K, V> map, K key, Supplier<V> constructor) {
    V value = map.get(key);/*from  w w w  . j  a v a  2s  .co m*/
    if (value == null) {
        value = constructor.get();
        map.put(key, value);
    }
    return value;
}

From source file:onl.area51.httpd.rest.JsonEntity.java

public static Supplier<HttpEntity> encodeArray(Supplier<JsonArrayBuilder> s) {
    return () -> new JsonEntity(s.get().build());
}

From source file:onl.area51.httpd.rest.JsonEntity.java

public static Supplier<HttpEntity> encodeObject(Supplier<JsonObjectBuilder> s) {
    return () -> new JsonEntity(s.get().build());
}

From source file:enumj.NanoTimer.java

public static <U, R> long buildNanos(Supplier<U> construction, Function<U, R> action, Supplier<String> msg) {
    print("Construction: " + msg.get());
    final U input = construction.get();
    print("Action: " + msg.get());
    final long t0 = System.nanoTime();
    final R result = action.apply(input);
    return System.nanoTime() - t0;
}

From source file:io.github.carlomicieli.footballdb.starter.mapping.Height.java

private static void check(Supplier<Boolean> op, String msg) {
    if (!op.get())
        throw new IllegalArgumentException("Invalid height: " + msg);
}