MATSIM
IdCollectors.java
Go to the documentation of this file.
1 /*
2  * *********************************************************************** *
3  * project: org.matsim.*
4  * *********************************************************************** *
5  * *
6  * copyright : (C) 2023 by the members listed in the COPYING, *
7  * LICENSE and WARRANTY file. *
8  * email : info at matsim dot org *
9  * *
10  * *********************************************************************** *
11  * *
12  * This program is free software; you can redistribute it and/or modify *
13  * it under the terms of the GNU General Public License as published by *
14  * the Free Software Foundation; either version 2 of the License, or *
15  * (at your option) any later version. *
16  * See also COPYING, LICENSE and WARRANTY file *
17  * *
18  * *********************************************************************** *
19  */
20 
21 package org.matsim.api.core.v01;
22 
23 import java.util.Map;
24 import java.util.Objects;
25 import java.util.function.BinaryOperator;
26 import java.util.function.Function;
27 import java.util.stream.Collector;
28 import java.util.stream.Collector.Characteristics;
29 
30 import com.google.common.base.Preconditions;
31 
35 public class IdCollectors {
36  public static <T, K, V> Collector<T, ?, IdMap<K, V>> toIdMap(Class<K> idClass, Function<? super T, ? extends Id<K>> keyMapper,
37  Function<? super T, ? extends V> valueMapper) {
38  return Collector.of(
39  // supplier
40  () -> new IdMap<>(idClass),
41  // accumulator
42  (map, element) -> {
43  Id<K> k = keyMapper.apply(element);
44  V v = Objects.requireNonNull(valueMapper.apply(element));
45  V u = map.putIfAbsent(k, v);
46  Preconditions.checkState(u == null, "Duplicate key %s (attempted merging values %s and %s)", k, u, v);
47  },
48  // combiner
49  (m1, m2) -> {
50  for (Map.Entry<Id<K>, V> e : m2.entrySet()) {
51  Id<K> k = e.getKey();
52  V v = Objects.requireNonNull(e.getValue());
53  V u = m1.putIfAbsent(k, v);
54  Preconditions.checkState(u == null, "Duplicate key %s (attempted merging values %s and %s)", k, u, v);
55  }
56  return m1;
57  },
58  // characteristics
59  Characteristics.IDENTITY_FINISH);
60  }
61 
62  public static <T, K, V> Collector<T, ?, IdMap<K, V>> toIdMap(Class<K> idClass, Function<? super T, ? extends Id<K>> keyMapper,
63  Function<? super T, ? extends V> valueMapper, BinaryOperator<V> mergeFunction) {
64  return Collector.of(
65  // supplier
66  () -> new IdMap<>(idClass),
67  // accumulator
68  (map, element) -> map.merge(keyMapper.apply(element), valueMapper.apply(element), mergeFunction),
69  // combiner
70  (m1, m2) -> {
71  for (Map.Entry<Id<K>, V> e : m2.entrySet())
72  m1.merge(e.getKey(), e.getValue(), mergeFunction);
73  return m1;
74  },
75  // characteristics
76  Characteristics.IDENTITY_FINISH);
77  }
78 
79  public static <T> Collector<Id<T>, ?, IdSet<T>> toIdSet(Class<T> idClass) {
80  return Collector.of(
81  // supplier
82  () -> new IdSet<>(idClass),
83  // accumulator
84  IdSet::add,
85  // combiner
86  (left, right) -> {
87  if (left.size() < right.size()) {
88  right.addAll(left);
89  return right;
90  } else {
91  left.addAll(right);
92  return left;
93  }
94  },
95  // characteristics
96  Characteristics.UNORDERED, Characteristics.IDENTITY_FINISH);
97  }
98 }
static< T > Collector< Id< T >, ?, IdSet< T > > toIdSet(Class< T > idClass)
static< T, K, V > Collector< T, ?, IdMap< K, V > > toIdMap(Class< K > idClass, Function<? super T, ? extends Id< K >> keyMapper, Function<? super T, ? extends V > valueMapper)
boolean add(Id< T > value)
Definition: IdSet.java:202
static< T, K, V > Collector< T, ?, IdMap< K, V > > toIdMap(Class< K > idClass, Function<? super T, ? extends Id< K >> keyMapper, Function<? super T, ? extends V > valueMapper, BinaryOperator< V > mergeFunction)