soot.util.UnitMap Maven / Gradle / Ivy
package soot.util;
/*-
* #%L
* Soot - a J*va Optimization Framework
* %%
* Copyright (C) 2002 Florian Loitsch
* %%
* 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 2.1 of the
* License, or (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 Lesser Public License for more details.
*
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* .
* #L%
*/
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import soot.Body;
import soot.Unit;
import soot.toolkits.graph.UnitGraph;
/**
* Maps each unit to the result of mapTo
.
*/
public abstract class UnitMap implements Map {
private Map unitToResult;
/**
* maps each unit of this body to the result of mapTo
.
* before the mapping the method init
is called.
* the internal hashtable is initialized without any parameter.
*
* @param b
* a Body
*/
public UnitMap(Body b) {
unitToResult = new HashMap();
map(b);
}
/**
* maps each unit of the graph to the result of mapTo
.
* before the mapping the method init
is called.
* the internal hashtable is initialized without any parameter.
*
* @param g
* a UnitGraph
*/
public UnitMap(UnitGraph g) {
this(g.getBody());
}
/**
* maps each unit of this body to the result of mapTo
.
* before the mapping the method init
is called.
* the internal hashtable is initialized to initialCapacity
.
*
* @param b
* a Body
* @param initialCapacity
* the initialCapacity of the internal hashtable.
*/
public UnitMap(Body b, int initialCapacity) {
unitToResult = new HashMap(initialCapacity);
map(b);
}
/**
* maps each unit of the graph to the result of mapTo
.
* before the mapping the method init
is called.
* the internal hashtable is initialized to initialCapacity
.
*
* @param g
* a UnitGraph
* @param initialCapacity
* the initialCapacity of the internal hashtable.
*/
public UnitMap(UnitGraph g, int initialCapacity) {
this(g.getBody(), initialCapacity);
}
/**
* maps each unit of this body to the result of mapTo
.
* before the mapping the method init
is called.
* the internal hashtable is initialized to initialCapacity
and loadFactor
.
*
* @param b
* a Body
* @param initialCapacity
* the initialCapacity of the internal hashtable.
* @param loadFactor
* the loadFactor of the internal hashtable.
*/
public UnitMap(Body b, int initialCapacity, float loadFactor) {
unitToResult = new HashMap(initialCapacity);
init();
map(b);
}
/**
* maps each unit of the graph to the result of mapTo
.
* before the mapping the method init
is called.
* the internal hashtable is initialized to initialCapacity
and loadFactor
.
*
* @param g
* a UnitGraph
* @param initialCapacity
* the initialCapacity of the internal hashtable.
* @param loadFactor
* the loadFactor of the internal hashtable.
*/
public UnitMap(UnitGraph g, int initialCapacity, float loadFactor) {
this(g.getBody(), initialCapacity);
}
/**
* does the actual mapping. assumes, that the hashtable is already initialized.
*/
private void map(Body b) {
Iterator unitIt = b.getUnits().iterator();
while (unitIt.hasNext()) {
Unit currentUnit = unitIt.next();
T o = mapTo(currentUnit);
if (o != null) {
unitToResult.put(currentUnit, o);
}
}
}
/**
* allows one-time initialization before any mapping. This method is called before any mapping of a unit (but only once in
* the beginning).
* If not overwritten does nothing.
*/
protected void init() {
};
/**
* maps a unit to an object. This method is called for every unit. If the returned object is null
no object
* will be mapped.
*
* @param the
* Unit to which o
should be mapped.
* @return an object that is mapped to the unit, or null
.
*/
protected abstract T mapTo(Unit unit);
/* ====== the Map-interface. all methods are deleguated tp the hashmap====== */
public void clear() {
unitToResult.clear();
}
public boolean containsKey(Object key) {
return unitToResult.containsKey(key);
}
public boolean containsValue(Object value) {
return unitToResult.containsValue(value);
}
public Set> entrySet() {
return unitToResult.entrySet();
}
public boolean equals(Object o) {
return unitToResult.equals(o);
}
public T get(Object key) {
return unitToResult.get(key);
}
public int hashCode() {
return unitToResult.hashCode();
}
public boolean isEmpty() {
return unitToResult.isEmpty();
}
public Set keySet() {
return unitToResult.keySet();
}
public T put(Unit key, T value) {
return unitToResult.put(key, value);
}
public void putAll(Map t) {
unitToResult.putAll(t);
}
public T remove(Object key) {
return unitToResult.remove(key);
}
public int size() {
return unitToResult.size();
}
public Collection values() {
return unitToResult.values();
}
}