MATSIM
MapUtils.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * MapUtils.java
4  * *
5  * *********************************************************************** *
6  * *
7  * copyright : (C) 2013 by the members listed in the COPYING, *
8  * LICENSE and WARRANTY file. *
9  * email : info at matsim dot org *
10  * *
11  * *********************************************************************** *
12  * *
13  * This program is free software; you can redistribute it and/or modify *
14  * it under the terms of the GNU General Public License as published by *
15  * the Free Software Foundation; either version 2 of the License, or *
16  * (at your option) any later version. *
17  * See also COPYING, LICENSE and WARRANTY file *
18  * *
19  * *********************************************************************** */
20 package org.matsim.core.utils.collections;
21 
22 import java.util.ArrayList;
23 import java.util.Collection;
24 import java.util.HashMap;
25 import java.util.HashSet;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.Set;
29 
36 public final class MapUtils {
37  private MapUtils() {}
38 
48  public static <K,V> Collection<V> getCollection(
49  final K key,
50  final Map<K, Collection<V>> map) {
51  Collection<V> coll = map.get( key );
52 
53  if ( coll == null ) {
54  coll = new ArrayList<V>();
55  map.put( key , coll );
56  }
57 
58  return coll;
59  }
60 
70  public static <K,V> List<V> getList(
71  final K key,
72  final Map<K, List<V>> map) {
73  List<V> coll = map.get( key );
74 
75  if ( coll == null ) {
76  coll = new ArrayList<V>();
77  map.put( key , coll );
78  }
79 
80  return coll;
81  }
82 
92  public static <K,V> Set<V> getSet(
93  final K key,
94  final Map<K, Set<V>> map) {
95  Set<V> coll = map.get( key );
96 
97  if ( coll == null ) {
98  coll = new HashSet<V>();
99  map.put( key , coll );
100  }
101 
102  return coll;
103  }
104 
115  public static <K,C,V> Map<C,V> getMap(
116  final K key,
117  final Map<K, Map<C,V>> map) {
118  Map<C,V> coll = map.get( key );
119 
120  if ( coll == null ) {
121  coll = new HashMap<C,V>();
122  map.put( key , coll );
123  }
124 
125  return coll;
126  }
127 
138  public static <K,T> T getArbitraryObject(
139  final K key,
140  final Map<K, T> map,
141  final Factory<T> fact) {
142  T coll = map.get( key );
143 
144  if ( coll == null ) {
145  coll = fact.create();
146  map.put( key , coll );
147  }
148 
149  return coll;
150  }
151 
161  public static <K> Double getDouble(
162  final K key,
163  final Map<K, Double> map,
164  final double initialValue) {
165  Double d = map.get( key );
166 
167  if ( d == null ) {
168  d = initialValue;
169  map.put( key , d );
170  }
171 
172  return d;
173  }
174 
186  public static <K> double addToDouble(
187  final K key,
188  final Map<K, Double> map,
189  final double initialValue,
190  final double toAdd) {
191  final double newValue =
192  getDouble( key , map , initialValue ) +
193  toAdd;
194  map.put( key , newValue );
195  return newValue;
196  }
197 
198 
208  public static <K> Integer getInteger(
209  final K key,
210  final Map<K, Integer> map,
211  final int initialValue) {
212  Integer i = map.get( key );
213 
214  if ( i == null ) {
215  i = initialValue;
216  map.put( key , i );
217  }
218 
219  return i;
220  }
221 
233  public static <K> double addToInteger(
234  final K key,
235  final Map<K, Integer> map,
236  final int initialValue,
237  final int toAdd) {
238  final int newValue =
239  getInteger( key , map , initialValue ) +
240  toAdd;
241  map.put( key , newValue );
242  return newValue;
243  }
244 
255  public static <K,V> Collection<V> get(
256  final Iterable<K> keys,
257  final Map<K, V> map ) {
258  final List<V> coll = new ArrayList<V>();
259 
260  for ( K k : keys ) coll.add( map.get( k ) );
261 
262  return coll;
263  }
264 
265  public interface Factory<T> {
266  T create();
267  }
268 
274  public static class DefaultFactory<T> implements Factory<T> {
275  private final Class<? extends T> theClass;
276 
277  public DefaultFactory( final Class<? extends T> theClass ) {
278  this.theClass = theClass;
279  }
280 
285  @Override
286  public T create() {
287  try {
288  return theClass.getConstructor().newInstance();
289  } catch (Exception e) {
290  throw new RuntimeException( e );
291  }
292  }
293  }
294 }
295 
static< K > Integer getInteger(final K key, final Map< K, Integer > map, final int initialValue)
Definition: MapUtils.java:208
static< K, V > Set< V > getSet(final K key, final Map< K, Set< V >> map)
Definition: MapUtils.java:92
static< K > double addToInteger(final K key, final Map< K, Integer > map, final int initialValue, final int toAdd)
Definition: MapUtils.java:233
DefaultFactory(final Class<? extends T > theClass)
Definition: MapUtils.java:277
static< K, V > List< V > getList(final K key, final Map< K, List< V >> map)
Definition: MapUtils.java:70
static< K > Double getDouble(final K key, final Map< K, Double > map, final double initialValue)
Definition: MapUtils.java:161
static< K, C, V > Map< C, V > getMap(final K key, final Map< K, Map< C, V >> map)
Definition: MapUtils.java:115
static< K, T > T getArbitraryObject(final K key, final Map< K, T > map, final Factory< T > fact)
Definition: MapUtils.java:138
static< K > double addToDouble(final K key, final Map< K, Double > map, final double initialValue, final double toAdd)
Definition: MapUtils.java:186
static< K, V > Collection< V > getCollection(final K key, final Map< K, Collection< V >> map)
Definition: MapUtils.java:48