From 620a0d52048fb7a9b1c700d7f37619aeea83ea38 Mon Sep 17 00:00:00 2001 From: tpylak <tpylak> Date: Mon, 4 Apr 2011 12:52:52 +0000 Subject: [PATCH] minor: convenience map methods SVN: 20643 --- .../cisd/common/collections/GroupByMap.java | 23 +++++++++++++++++++ .../cisd/common/collections/TableMap.java | 17 +++++++++++++- 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/common/source/java/ch/systemsx/cisd/common/collections/GroupByMap.java b/common/source/java/ch/systemsx/cisd/common/collections/GroupByMap.java index c62cf97db7f..88fab65314a 100644 --- a/common/source/java/ch/systemsx/cisd/common/collections/GroupByMap.java +++ b/common/source/java/ch/systemsx/cisd/common/collections/GroupByMap.java @@ -17,6 +17,7 @@ package ch.systemsx.cisd.common.collections; import java.util.ArrayList; +import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -71,9 +72,31 @@ public class GroupByMap<K, E> return map.get(key); } + /** + * Returns all rows added at the specified key. + * + * @throws IllegalStateException if the key is not in the map + */ + public List<E> getOrDie(K key) + { + List<E> result = tryGet(key); + if (result == null) + { + throw new IllegalStateException("No element with the key " + key); + } + return result; + } + /** @return all available keys */ public Set<K> getKeys() { return map.keySet(); } + + /** @return unmodifiable map from key to a list of its values. */ + public Map<K, List<E>> getMap() + { + return Collections.unmodifiableMap(map); + } + } diff --git a/common/source/java/ch/systemsx/cisd/common/collections/TableMap.java b/common/source/java/ch/systemsx/cisd/common/collections/TableMap.java index 2752ec4294f..256ff8bea2d 100644 --- a/common/source/java/ch/systemsx/cisd/common/collections/TableMap.java +++ b/common/source/java/ch/systemsx/cisd/common/collections/TableMap.java @@ -160,6 +160,21 @@ public class TableMap<K, E> implements Iterable<E> } } + /** + * Gets the row for the specified key. + * + * @throws IllegalStateException if key cannot be found. + */ + public final E getOrDie(final K key) + { + E elem = tryGet(key); + if (elem == null) + { + throw new IllegalStateException("No value for teh specified key found: " + key); + } + return elem; + } + /** * Gets the row for the specified key or <code>null</code> if not found. */ @@ -189,7 +204,7 @@ public class TableMap<K, E> implements Iterable<E> { return Collections.unmodifiableSet(map.keySet()); } - + /** * Removes and returns the row for the specified key. * -- GitLab