diff options
author | Jon Bratseth <bratseth@verizonmedia.com> | 2019-07-02 15:19:26 -0700 |
---|---|---|
committer | Jon Bratseth <bratseth@verizonmedia.com> | 2019-07-02 15:19:26 -0700 |
commit | 1a60bfebe04b418b0bbde8ebd8b05904e4df1760 (patch) | |
tree | f969e994699de78a2fc7de873ca5e977ffd42aff /vespajlib/src/main | |
parent | 08c7c9919ee6f60047cd57b3afece54dfa7dda52 (diff) |
Forfeit soft constraints when necessary
Diffstat (limited to 'vespajlib/src/main')
-rw-r--r-- | vespajlib/src/main/java/com/yahoo/collections/ListMap.java | 23 |
1 files changed, 19 insertions, 4 deletions
diff --git a/vespajlib/src/main/java/com/yahoo/collections/ListMap.java b/vespajlib/src/main/java/com/yahoo/collections/ListMap.java index e851362a99d..052ea55d6fe 100644 --- a/vespajlib/src/main/java/com/yahoo/collections/ListMap.java +++ b/vespajlib/src/main/java/com/yahoo/collections/ListMap.java @@ -23,6 +23,12 @@ public class ListMap<K, V> { this(HashMap.class); } + /** Copy constructor. This will not be frozen even if the argument map is */ + public ListMap(ListMap<K, V> original) { + map = new HashMap<>(); + original.map.forEach((k, v) -> this.map.put(k, new ArrayList<>(v))); + } + @SuppressWarnings("unchecked") public ListMap(@SuppressWarnings("rawtypes") Class<? extends Map> implementation) { try { @@ -45,6 +51,15 @@ public class ListMap<K, V> { list.add(value); } + /** Put a key without adding a new value, such that there is an empty list of values if no values are already added */ + public void put(K key) { + List<V> list = map.get(key); + if (list == null) { + list = new ArrayList<>(); + map.put(key, list); + } + } + public void removeAll(K key) { map.remove(key); } @@ -73,13 +88,13 @@ public class ListMap<K, V> { /** * Returns the List containing the elements with this key, or an empty list - * if there are no elements for this key. The list returned is unmodifiable. + * if there are no elements for this key. + * The returned list can be modified to add and remove values if the value exists. */ public List<V> get(K key) { List<V> list = map.get(key); - if (list == null) - return ImmutableList.of();; - return ImmutableList.copyOf(list); + if (list == null) return ImmutableList.of();; + return list; } /** The same as get */ |