added a utility function

git-svn-id: https://wala.svn.sourceforge.net/svnroot/wala/trunk@3062 f5eafffb-2e1d-0410-98e4-8ec43c5233c4
This commit is contained in:
msridhar1 2008-10-22 15:36:55 +00:00
parent 22d1e1087b
commit c7ff218238
1 changed files with 20 additions and 0 deletions

View File

@ -11,6 +11,7 @@
package com.ibm.wala.util.collections;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
@ -39,6 +40,25 @@ public class MapUtil {
return result;
}
/**
* @param M a mapping from Object -> Collection
* @param key
* @return the Collection corresponding to key in M; create one if needed
* @throws IllegalArgumentException if M is null
*/
public static <K, T> Collection<T> findOrCreateCollection(Map<K, Collection<T>> M, K key) {
if (M == null) {
throw new IllegalArgumentException("M is null");
}
Collection<T> result = M.get(key);
if (result == null) {
result = HashSetFactory.make(2);
M.put(key, result);
}
return result;
}
/**
* @param M a mapping from Object -> Set
* @param key