/** * {@code BinaryOperator<Map>} that merges the contents of its right * argument into its left argument, throwing {@code IllegalStateException} * if duplicate keys are encountered. * * @param <K> type of the map keys * @param <V> type of the map values * @param <M> type of the map * @return a merge function for two maps */ private static <K, V, M extends Map<K,V>> BinaryOperator<M> uniqKeysMapMerger() { return (m1, m2) -> { for (Map.Entry<K,V> e : m2.entrySet()) { K k = e.getKey(); V v = Objects.requireNonNull(e.getValue()); V u = null; if (m1 instanceof ConcurrentMap) { u = ((ConcurrentMap<K, V>) m1).putIfAbsent(k, v); } else { u = Maps.putIfAbsent(m1, k, v); } if (u != null) { throw duplicateKeyException(k, u, v); } } return m1; }; }
/** * {@code BinaryOperator<Map>} that merges the contents of its right * argument into its left argument, throwing {@code IllegalStateException} * if duplicate keys are encountered. * * @param <K> type of the map keys * @param <V> type of the map values * @param <M> type of the map * @return a merge function for two maps */ private static <K, V, M extends Map<K,V>> BinaryOperator<M> uniqKeysMapMerger() { return (m1, m2) -> { for (Map.Entry<K,V> e : m2.entrySet()) { K k = e.getKey(); V v = Objects.requireNonNull(e.getValue()); V u = null; if (m1 instanceof ConcurrentMap) { u = ((ConcurrentMap<K, V>) m1).putIfAbsent(k, v); } else { u = Maps.putIfAbsent(m1, k, v); } if (u != null) { throw duplicateKeyException(k, u, v); } } return m1; }; }
/** * {@code BiConsumer<Map, T>} that accumulates (key, value) pairs * extracted from elements into the map, throwing {@code IllegalStateException} * if duplicate keys are encountered. * * @param keyMapper a function that maps an element into a key * @param valueMapper a function that maps an element into a value * @param <T> type of elements * @param <K> type of map keys * @param <V> type of map values * @return an accumulating consumer */ private static <T, K, V> BiConsumer<Map<K, V>, T> uniqKeysMapAccumulator(Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends V> valueMapper) { return (map, element) -> { K k = keyMapper.apply(element); V v = Objects.requireNonNull(valueMapper.apply(element)); V u = null; if (map instanceof ConcurrentMap) { u = ((ConcurrentMap<K, V>) map).putIfAbsent(k, v); } else { u = Maps.putIfAbsent(map, k, v); } if (u != null) { throw duplicateKeyException(k, u, v); } }; }
/** * {@code BiConsumer<Map, T>} that accumulates (key, value) pairs * extracted from elements into the map, throwing {@code IllegalStateException} * if duplicate keys are encountered. * * @param keyMapper a function that maps an element into a key * @param valueMapper a function that maps an element into a value * @param <T> type of elements * @param <K> type of map keys * @param <V> type of map values * @return an accumulating consumer */ private static <T, K, V> BiConsumer<Map<K, V>, T> uniqKeysMapAccumulator(Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends V> valueMapper) { return (map, element) -> { K k = keyMapper.apply(element); V v = Objects.requireNonNull(valueMapper.apply(element)); V u = null; if (map instanceof ConcurrentMap) { u = ((ConcurrentMap<K, V>) map).putIfAbsent(k, v); } else { u = Maps.putIfAbsent(map, k, v); } if (u != null) { throw duplicateKeyException(k, u, v); } }; }