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

graphql.collect.ImmutableKit Maven / Gradle / Ivy

There is a newer version: 230521-nf-execution
Show newest version
package graphql.collect;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import graphql.Internal;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

import static graphql.Assert.assertNotNull;

@Internal
public final class ImmutableKit {

    public static  ImmutableList emptyList() {
        return ImmutableList.of();
    }

    public static  ImmutableList nonNullCopyOf(Collection collection) {
        return collection == null ? emptyList() : ImmutableList.copyOf(collection);
    }

    public static  ImmutableMap emptyMap() {
        return ImmutableMap.of();
    }

    /**
     * ImmutableMaps are hard to build via {@link Map#computeIfAbsent(Object, Function)} style.  This methods
     * allows you to take a mutable map with mutable list of keys and make it immutable.
     * 

* This of course has a cost - if the map is very large you will be using more memory. But for static * maps that live a long life it maybe be worth it. * * @param startingMap the starting input map * @param for key * @param for victory * * @return and Immutable map of ImmutableList values */ public static ImmutableMap> toImmutableMapOfLists(Map> startingMap) { assertNotNull(startingMap); ImmutableMap.Builder> map = ImmutableMap.builder(); for (Map.Entry> e : startingMap.entrySet()) { ImmutableList value = ImmutableList.copyOf(startingMap.getOrDefault(e.getKey(), emptyList())); map.put(e.getKey(), value); } return map.build(); } public static ImmutableMap addToMap(Map existing, K newKey, V newVal) { return ImmutableMap.builder().putAll(existing).put(newKey, newVal).build(); } public static ImmutableMap mergeMaps(Map m1, Map m2) { return ImmutableMap.builder().putAll(m1).putAll(m2).build(); } public static ImmutableList concatLists(List l1, List l2) { return ImmutableList.builder().addAll(l1).addAll(l2).build(); } /** * This is more efficient than `c.stream().map().collect()` because it does not create the intermediate objects needed * for the flexible style. Benchmarking has shown this to outperform `stream()`. * * @param iterable the iterable to map * @param mapper the mapper function * @param for two * @param for result * * @return a map immutable list of results */ public static ImmutableList map(Iterable iterable, Function mapper) { assertNotNull(iterable); assertNotNull(mapper); @SuppressWarnings("RedundantTypeArguments") ImmutableList.Builder builder = ImmutableList.builder(); for (T t : iterable) { R r = mapper.apply(t); builder.add(r); } return builder.build(); } /** * This constructs a new Immutable list from an existing collection and adds a new element to it. * * @param existing the existing collection * @param newValue the new value to add * @param extraValues more values to add * @param for two * * @return an Immutable list with the extra effort. */ public static ImmutableList addToList(Collection existing, T newValue, T... extraValues) { assertNotNull(existing); assertNotNull(newValue); int expectedSize = existing.size() + 1 + extraValues.length; ImmutableList.Builder newList = ImmutableList.builderWithExpectedSize(expectedSize); newList.addAll(existing); newList.add(newValue); for (T extraValue : extraValues) { newList.add(extraValue); } return newList.build(); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy