
org.opentripplanner.util.MapUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of otp Show documentation
Show all versions of otp Show documentation
The OpenTripPlanner multimodal journey planning system
/* This program is free software: you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public License
as published by the Free Software Foundation, either version 3 of
the License, or (props, at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see . */
package org.opentripplanner.util;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class MapUtils {
/**
* An extremely common pattern: add an item to a set in a hash value, creating that set if
* necessary
* TODO: Replace with Guava
*/
public static final void addToMapSet(Map> mapList, T key, U value) {
Set list = mapList.get(key);
if (list == null) {
list = new HashSet();
mapList.put(key, list);
}
list.add(value);
}
/**
* An extremely common pattern: add an item to a list in a hash value, creating that list if
* necessary
* TODO: Replace with Guava
*/
public static final void addToMapList(Map> mapList, T key, U value) {
List list = mapList.get(key);
if (list == null) {
list = new ArrayList();
mapList.put(key, list);
}
list.add(value);
}
public static final boolean addToMaxMap(Map map, T key, double value) {
Double oldValue = map.get(key);
if (oldValue == null || value > oldValue) {
map.put(key, value);
return true;
}
return false;
}
public static void addToMapListUnique(Map> mapList,
T key, List values) {
List list = mapList.get(key);
if (list == null) {
list = new ArrayList(values.size());
mapList.put(key, list);
}
for (U value : values) {
if (!list.contains(value)) {
list.add(value);
}
}
}
public static > void mergeInUnique(Map mapList,
Map from) {
for (Map.Entry entry : from.entrySet()) {
T key = entry.getKey();
V value = entry.getValue();
V originalValue = mapList.get(key);
if (originalValue != null) {
HashSet originalSet = new HashSet(originalValue);
for (U item : value) {
if (!originalSet.contains(item))
originalValue.add(item);
}
} else {
mapList.put(key, value);
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy