All Downloads are FREE. Search and download functionalities are using the official Maven repository.

sirius.kernel.commons.Lambdas Maven / Gradle / Ivy

Go to download

Provides common core classes and the microkernel powering all Sirius applications

There is a newer version: 12.9.1
Show newest version
/*
 * Made with all the love in the world
 * by scireum in Remshalden, Germany
 *
 * Copyright by scireum GmbH
 * http://www.scireum.de - [email protected]
 */

package sirius.kernel.commons;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collector;

/**
 * Helper class which provides various methods to work with lambdas.
 */
public class Lambdas {

    private Lambdas() {
    }

    /**
     * Provides an identity function which permits to "touch" the element for which it was called.
     * 

* This is designed to be used with {@link java.util.Optional} like this: * {@code return Optional.of("Test").map(Lambdas.touch(s -> System.out.println(s)))} * * @param consumer the lambda used to "touch" (use) each parameter * @param the type of the elements accepted by the consumer * @return an identity function which also applies the consumer on each parameter */ public static Function touch(Consumer consumer) { return t -> { consumer.accept(t); return t; }; } /** * Used to collect the results of a stream operation into an existing collection. * * @param collection the target collection * @param the type of the elements accepted by the collector * @param the type of the collection which is filled by the collector * @return a {@link java.util.stream.Collector} inserting all elements into the given collection */ public static > Collector into(C collection) { return Collector.of(() -> collection, Collection::add, Lambdas::unsupportedBiOperation, Function.identity(), Collector.Characteristics.IDENTITY_FINISH); } /** * Can be used to group a given stream by identity and count the occurrences of each entity. * * @param the type of the key by which the values are grouped * @return a Collector which can be supplied to {@link java.util.stream.Stream#collect(java.util.stream.Collector)}. */ public static Collector, Map> groupAndCount() { return Collector.of(HashMap::new, Lambdas::increment, Lambdas::unsupportedBiOperation, Function.identity(), Collector.Characteristics.IDENTITY_FINISH); } private static void increment(Map map, K value) { map.put(value, map.computeIfAbsent(value, k -> 0) + 1); } /** * Can be used as lambda for unsupported BiOperations. *

* This is intended to be a dummy parameter (e.g. for Collector.of. It will always throw * an UnsupportedOperationException if invoked. * * @param the type of objects for which the operation is to be used * @param a the first parameter of the binary operation * @param b the second parameter of the binary operation * @return this method will never return a value as an UnsupportedOperationException is thrown * @throws java.lang.UnsupportedOperationException always thrown by this method */ public static O unsupportedBiOperation(O a, O b) { throw new UnsupportedOperationException(); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy