diff options
author | Pierre <pierre.guillot@sonarsource.com> | 2023-07-10 16:59:50 +0200 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2023-07-17 20:03:45 +0000 |
commit | ce0e4ddf7ec619dde2c6cf02ae0ec08012f47335 (patch) | |
tree | 2f32948edd74d54872a7ec53c07fb1289e88a34a /sonar-core/src/main/java | |
parent | 46ac0043ee3aedee6b2304969533847781d985ef (diff) | |
download | sonarqube-ce0e4ddf7ec619dde2c6cf02ae0ec08012f47335.tar.gz sonarqube-ce0e4ddf7ec619dde2c6cf02ae0ec08012f47335.zip |
NO-JIRA replace guava copy uniqueIndex to Collectors.toMap and simplify lambdas
Diffstat (limited to 'sonar-core/src/main/java')
-rw-r--r-- | sonar-core/src/main/java/org/sonar/core/util/stream/MoreCollectors.java | 124 |
1 files changed, 0 insertions, 124 deletions
diff --git a/sonar-core/src/main/java/org/sonar/core/util/stream/MoreCollectors.java b/sonar-core/src/main/java/org/sonar/core/util/stream/MoreCollectors.java index abf0e22be21..80de3393b63 100644 --- a/sonar-core/src/main/java/org/sonar/core/util/stream/MoreCollectors.java +++ b/sonar-core/src/main/java/org/sonar/core/util/stream/MoreCollectors.java @@ -20,14 +20,11 @@ package org.sonar.core.util.stream; import com.google.common.collect.ImmutableListMultimap; -import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSetMultimap; -import java.util.HashMap; import java.util.Map; import java.util.function.BiConsumer; import java.util.function.BinaryOperator; import java.util.function.Function; -import java.util.function.Supplier; import java.util.stream.Collector; import java.util.stream.Stream; @@ -35,7 +32,6 @@ import static java.util.Objects.requireNonNull; public final class MoreCollectors { - private static final int DEFAULT_HASHMAP_CAPACITY = 0; private static final String KEY_FUNCTION_CANT_RETURN_NULL_MESSAGE = "Key function can't return null"; private static final String VALUE_FUNCTION_CANT_RETURN_NULL_MESSAGE = "Value function can't return null"; @@ -43,126 +39,6 @@ public final class MoreCollectors { // prevents instantiation } - - /** - * Creates an {@link ImmutableMap} from the stream where the values are the values in the stream and the keys are the - * result of the provided {@link Function keyFunction} applied to each value in the stream. - * - * <p> - * The {@link Function keyFunction} must return a unique (according to the key's type {@link Object#equals(Object)} - * and/or {@link Comparable#compareTo(Object)} implementations) value for each of them, otherwise a - * {@link IllegalArgumentException} will be thrown. - * </p> - * - * <p> - * {@link Function keyFunction} can't return {@code null}, otherwise a {@link NullPointerException} will be thrown. - * </p> - * - * @throws NullPointerException if {@code keyFunction} is {@code null}. - * @throws NullPointerException if result of {@code keyFunction} is {@code null}. - * @throws IllegalArgumentException if {@code keyFunction} returns the same value for multiple entries in the stream. - */ - public static <K, E> Collector<E, Map<K, E>, ImmutableMap<K, E>> uniqueIndex(Function<? super E, K> keyFunction) { - return uniqueIndex(keyFunction, Function.identity()); - } - - /** - * Same as {@link #uniqueIndex(Function)} but using an underlying {@link Map} initialized with a capacity for the - * specified expected size. - * - * <p>Note: using this method with a parallel stream will likely not have the expected memory usage benefit as all - * processing threads will use a Map with a capacity large enough for the final size.</p> - * - * <p> - * {@link Function keyFunction} can't return {@code null}, otherwise a {@link NullPointerException} will be thrown. - * </p> - * - * @throws NullPointerException if {@code keyFunction} is {@code null}. - * @throws NullPointerException if result of {@code keyFunction} is {@code null}. - * @throws IllegalArgumentException if {@code keyFunction} returns the same value for multiple entries in the stream. - * @see #uniqueIndex(Function) - */ - public static <K, E> Collector<E, Map<K, E>, ImmutableMap<K, E>> uniqueIndex(Function<? super E, K> keyFunction, int expectedSize) { - return uniqueIndex(keyFunction, Function.identity(), expectedSize); - } - - /** - * Creates an {@link ImmutableMap} from the stream where the values are the result of {@link Function valueFunction} - * applied to the values in the stream and the keys are the result of the provided {@link Function keyFunction} - * applied to each value in the stream. - * - * <p> - * The {@link Function keyFunction} must return a unique (according to the key's type {@link Object#equals(Object)} - * and/or {@link Comparable#compareTo(Object)} implementations) value for each of them, otherwise a - * {@link IllegalArgumentException} will be thrown. - * </p> - * - * <p> - * Neither {@link Function keyFunction} nor {@link Function valueFunction} can return {@code null}, otherwise a - * {@link NullPointerException} will be thrown. - * </p> - * - * @throws NullPointerException if {@code keyFunction} or {@code valueFunction} is {@code null}. - * @throws NullPointerException if result of {@code keyFunction} or {@code valueFunction} is {@code null}. - * @throws IllegalArgumentException if {@code keyFunction} returns the same value for multiple entries in the stream. - */ - public static <K, E, V> Collector<E, Map<K, V>, ImmutableMap<K, V>> uniqueIndex(Function<? super E, K> keyFunction, - Function<? super E, V> valueFunction) { - return uniqueIndex(keyFunction, valueFunction, DEFAULT_HASHMAP_CAPACITY); - } - - /** - * Same as {@link #uniqueIndex(Function, Function)} but using an underlying {@link Map} initialized with a capacity - * for the specified expected size. - * - * <p>Note: using this method with a parallel stream will likely not have the expected memory usage benefit as all - * processing threads will use a Map with a capacity large enough for the final size.</p> - * - * <p> - * Neither {@link Function keyFunction} nor {@link Function valueFunction} can return {@code null}, otherwise a - * {@link NullPointerException} will be thrown. - * </p> - * - * @throws NullPointerException if {@code keyFunction} or {@code valueFunction} is {@code null}. - * @throws NullPointerException if result of {@code keyFunction} or {@code valueFunction} is {@code null}. - * @throws IllegalArgumentException if {@code keyFunction} returns the same value for multiple entries in the stream. - * @see #uniqueIndex(Function, Function) - */ - public static <K, E, V> Collector<E, Map<K, V>, ImmutableMap<K, V>> uniqueIndex(Function<? super E, K> keyFunction, - Function<? super E, V> valueFunction, int expectedSize) { - verifyKeyAndValueFunctions(keyFunction, valueFunction); - - BiConsumer<Map<K, V>, E> accumulator = (map, element) -> { - K key = requireNonNull(keyFunction.apply(element), KEY_FUNCTION_CANT_RETURN_NULL_MESSAGE); - V value = requireNonNull(valueFunction.apply(element), VALUE_FUNCTION_CANT_RETURN_NULL_MESSAGE); - - putAndFailOnDuplicateKey(map, key, value); - }; - BinaryOperator<Map<K, V>> merger = (m1, m2) -> { - for (Map.Entry<K, V> entry : m2.entrySet()) { - putAndFailOnDuplicateKey(m1, entry.getKey(), entry.getValue()); - } - return m1; - }; - return Collector.of( - newHashMapSupplier(expectedSize), - accumulator, - merger, - ImmutableMap::copyOf, - Collector.Characteristics.UNORDERED); - } - - private static <K, V> Supplier<Map<K, V>> newHashMapSupplier(int expectedSize) { - return () -> expectedSize == DEFAULT_HASHMAP_CAPACITY ? new HashMap<>() : new HashMap<>(expectedSize); - } - - private static <K, V> void putAndFailOnDuplicateKey(Map<K, V> map, K key, V value) { - V existingValue = map.put(key, value); - if (existingValue != null) { - throw new IllegalArgumentException(String.format("Duplicate key %s", key)); - } - } - /** * Creates an {@link com.google.common.collect.ImmutableListMultimap} from the stream where the values are the values * in the stream and the keys are the result of the provided {@link Function keyFunction} applied to each value in the |