com.ibm.wala.util.collections.MapUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of com.ibm.wala.util Show documentation
Show all versions of com.ibm.wala.util Show documentation
T. J. Watson Libraries for Analysis
/*
* Copyright (c) 2002 - 2006 IBM Corporation.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*/
package com.ibm.wala.util.collections;
import com.ibm.wala.util.intset.MutableIntSet;
import com.ibm.wala.util.intset.MutableSparseIntSet;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.WeakHashMap;
/** utilities for managing {@link Map}s */
public class MapUtil {
/**
* @param M a mapping from Object -> Set
* @return the Set corresponding to key in M; create one if needed
* @throws IllegalArgumentException if M is null
* @throws ClassCastException if the key is of an inappropriate type for this map (optional)
* @throws NullPointerException if the specified key is null and this map does not permit null
* keys (optional)
*/
public static Set findOrCreateSet(Map> M, K key) {
if (M == null) {
throw new IllegalArgumentException("M is null");
}
Set result = M.get(key);
if (result == null) {
result = HashSetFactory.make(2);
M.put(key, result);
}
return result;
}
/**
* @throws ClassCastException if the key is of an inappropriate type for this map (optional)
* @throws NullPointerException if the specified key is null and this map does not permit null
* keys (optional)
*/
public static MutableIntSet findOrCreateMutableIntSet(Map M, K key) {
if (M == null) {
throw new IllegalArgumentException("M is null");
}
MutableIntSet mis = M.get(key);
if (mis == null) {
mis = MutableSparseIntSet.makeEmpty();
M.put(key, mis);
}
return mis;
}
/**
* @return the Collection corresponding to key in M; create one if needed
* @throws ClassCastException if the key is of an inappropriate type for this map (optional)
* @throws NullPointerException if the specified key is null and this map does not permit null
* keys (optional)
*/
public static Collection findOrCreateCollection(Map> M, K key) {
if (M == null) {
throw new IllegalArgumentException("M is null");
}
Collection result = M.get(key);
if (result == null) {
result = HashSetFactory.make(2);
M.put(key, result);
}
return result;
}
/**
* @return the Set corresponding to key in M; create one if needed
* @throws IllegalArgumentException if M is null
* @throws ClassCastException if the key is of an inappropriate type for this map (optional)
* @throws NullPointerException if the specified key is null and this map does not permit null
* keys (optional)
*/
public static List findOrCreateList(Map> M, K key) {
if (M == null) {
throw new IllegalArgumentException("M is null");
}
if (!M.containsKey(key)) {
M.put(key, new ArrayList<>());
}
return M.get(key);
}
/**
* @param M a mapping from Object -> Map
* @return the Map corresponding to key in M; create one if needed
* @throws IllegalArgumentException if M is null
* @throws ClassCastException if the key is of an inappropriate type for this map (optional)
* @throws NullPointerException if the specified key is null and this map does not permit null
* keys (optional)
*/
public static Map findOrCreateMap(Map> M, K key) {
if (M == null) {
throw new IllegalArgumentException("M is null");
}
Map result = M.get(key);
if (result == null) {
result = HashMapFactory.make(2);
M.put(key, result);
}
return result;
}
/**
* @throws ClassCastException if the key is of an inappropriate type for this map (optional)
* @throws NullPointerException if the specified key is null and this map does not permit null
* keys (optional)
*/
public static V findOrCreateValue(Map M, K key, Factory factory) {
if (M == null) {
throw new IllegalArgumentException("M is null");
}
V result = M.get(key);
if (result == null) {
result = factory.make();
M.put(key, result);
}
return result;
}
/**
* @param M a mapping from Object -> WeakHashMap
* @return the WeakHashMap corresponding to key in M; create one if needed
* @throws IllegalArgumentException if M is null
* @throws ClassCastException if the key is of an inappropriate type for this map (optional)
* @throws NullPointerException if the specified key is null and this map does not permit null
* keys (optional)
*/
public static WeakHashMap findOrCreateWeakHashMap(
Map