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

/**
 * Given a stream, and a function for creating default values, constructs a map from keys to elements of
 * type T built using the producer./*from www . j  av  a2  s .  c  o  m*/
 * @param keyStream  The stream of keys for the map
 * @param valuesSupplier  The function that produces default values for the map
 * @param <T>  The type of the keys of the map
 * @param <U>  The type of the values of the map
 */
public static <T, U> LinkedHashMap<T, U> buildMap(Stream<T> keyStream, Supplier<U> valuesSupplier) {
    LinkedHashMap<T, U> map = new LinkedHashMap<>();
    keyStream.forEach(key -> map.put(key, valuesSupplier.get()));
    return map;
}

From source file:Main.java

public static Collection<Thread> getThreads(int count, Supplier<Thread> threadSupplier) {
    Collection<Thread> threads = new ArrayList<>(count);
    for (int i = 0; i < count; i++) {
        threads.add(threadSupplier.get());
    }/* w  w  w.  j a  v a 2  s.co  m*/
    return threads;
}

From source file:com.github.horrorho.inflatabledonkey.chunk.store.disk.DiskChunkStoreTest.java

private static byte[] digest(Supplier<Digest> digests, byte[] data) {
    Digest digest = digests.get();
    byte[] out = new byte[digest.getDigestSize()];
    digest.update(data, 0, data.length);
    digest.doFinal(out, 0);//www  .ja  v a2 s.c o m
    return out;
}

From source file:co.runrightfast.core.utils.LoggingUtils.java

/**
 * Helper for logging JSON messages// w w w.  j  a v  a2 s  .  c  o m
 *
 * @param logger
 * @param level
 * @param className
 * @param method
 * @param message
 */
static void log(@NonNull final Logger logger, final Level level, final String className, final String method,
        @NonNull final Supplier<JsonObject> message) {
    logger.logp(level, className, method, () -> message.get().toString());
}

From source file:org.graylog2.indexer.cluster.jest.JestUtils.java

private static FieldTypeException buildFieldTypeException(Supplier<String> errorMessage, String reason) {
    return new FieldTypeException(errorMessage.get(), reason);
}

From source file:io.github.retz.web.feign.Retz.java

static Response tryOrErrorResponse(Supplier<Response> supplier) throws IOException {
    try {/*from   w w w .  j  a v  a2s.  c om*/
        return supplier.get();
    } catch (ErrorResponseException ex) {
        return ex.getErr();
    } catch (Exception e) {
        throw new IOException(e.getMessage(), e);
    }
}

From source file:org.graylog2.indexer.cluster.jest.JestUtils.java

private static IndexNotFoundException buildIndexNotFoundException(Supplier<String> errorMessage, String index) {
    return new IndexNotFoundException(errorMessage.get(), Collections
            .singletonList("Index not found for query: " + index + ". Try recalculating your index ranges."));
}

From source file:ch.ethz.inf.vs.hypermedia.client.Utils.java

public static <V extends Future> V or(Supplier<V> s, Iterable<V> options) {
    V item = s.get();
    item.setPreProcess(() -> {/*from   w ww . j  a  va2s  . c o m*/
        for (V option : options) {
            try {
                item.addDependency(option);
                option.get();
                item.link(option);
                return;
            } catch (Exception ex) {

            }
        }
        item.setException(new RuntimeException("All options Failed"));
    });
    return item;
}

From source file:Main.java

/**
 * Returns a {@link Map}&lt;V,{@link Set}&lt;K&gt;&gt; build by exchange
 * key and value of original {@code map}. All couple (K,V) are kept by
 * this operation.//  ww  w  .java2s .  c o  m
 * <p>
 * Original map is not modify by this operation.
 *
 * @param <K>
 *            The type of the key of the initial map, the type of the value
 *            in result {@link Map}{@link Set}
 * @param <V>
 *            The type of the value of the initial map, the type of the key
 *            in result {@link Map}{@link Set}
 * @param map
 *            The map
 * @param mapSupplier
 *            The {@link Map} {@link Supplier}
 * @param setSupplier
 *            The {@link Set} {@link Supplier}
 * @return a {@link Map} build using {@link HashMap} {@link Set}
 */
public static <K, V> Map<V, Set<K>> reverseMap(@Nonnull final Map<K, V> map,
        @Nonnull final Supplier<Map<V, Set<K>>> mapSupplier, @Nonnull final Supplier<Set<K>> setSupplier) {
    final Map<V, Set<K>> reverseMap = mapSupplier.get();

    for (final Map.Entry<K, V> entry : map.entrySet()) {
        final K oldKey = entry.getKey();
        final V oldValue = entry.getValue();
        final Set<K> set;

        if (reverseMap.containsKey(oldValue)) {
            set = reverseMap.get(oldValue);
        } else {
            set = setSupplier.get();

            reverseMap.put(oldValue, set);
        }
        set.add(oldKey);
    }

    return reverseMap;
}

From source file:com.simiacryptus.mindseye.test.PCAUtil.java

/**
 * Forked from Apache Commons Math/*from w  w w  .java2  s  .  c  o  m*/
 *
 * @param stream the stream
 * @return covariance covariance
 */
@Nonnull
public static RealMatrix getCovariance(@Nonnull final Supplier<Stream<double[]>> stream) {
    final int dimension = stream.get().findAny().get().length;
    final List<DoubleStatistics> statList = IntStream.range(0, dimension * dimension)
            .mapToObj(i -> new DoubleStatistics()).collect(Collectors.toList());
    stream.get().forEach(array -> {
        for (int i = 0; i < dimension; i++) {
            for (int j = 0; j <= i; j++) {
                statList.get(i * dimension + j).accept(array[i] * array[j]);
            }
        }
        RecycleBin.DOUBLES.recycle(array, array.length);
    });
    @Nonnull
    final RealMatrix covariance = new BlockRealMatrix(dimension, dimension);
    for (int i = 0; i < dimension; i++) {
        for (int j = 0; j <= i; j++) {
            final double v = statList.get(i + dimension * j).getAverage();
            covariance.setEntry(i, j, v);
            covariance.setEntry(j, i, v);
        }
    }
    return covariance;
}