001: /* Soot - a J*va Optimization Framework
002: * Copyright (C) 2002 Florian Loitsch
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the
016: * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
017: * Boston, MA 02111-1307, USA.
018: */
019:
020: /*
021: * Modified by the Sable Research Group and others 1997-1999.
022: * See the 'credits' file distributed with Soot for the complete list of
023: * contributors. (Soot is distributed at http://www.sable.mcgill.ca/soot)
024: */
025:
026: package soot.util;
027:
028: import soot.toolkits.graph.*;
029: import soot.*;
030: import java.util.*;
031:
032: /**
033: * Maps each unit to the result of <code>mapTo</code>.
034: */
035: public abstract class UnitMap implements Map {
036: private Hashtable<Object, Object> unitToResult;
037:
038: /**
039: * maps each unit of this body to the result of <code>mapTo</code>.<br>
040: * before the mapping the method <code>init</code> is called.<br>
041: * the internal hashtable is initialized without any parameter.
042: *
043: * @param b a Body
044: */
045: public UnitMap(Body b) {
046: unitToResult = new Hashtable<Object, Object>();
047: map(b);
048: }
049:
050: /**
051: * maps each unit of the graph to the result of <code>mapTo</code>.<br>
052: * before the mapping the method <code>init</code> is called.<br>
053: * the internal hashtable is initialized without any parameter.
054: *
055: * @param g a UnitGraph
056: */
057: public UnitMap(UnitGraph g) {
058: this (g.getBody());
059: }
060:
061: /**
062: * maps each unit of this body to the result of <code>mapTo</code>.<br>
063: * before the mapping the method <code>init</code> is called.<br>
064: * the internal hashtable is initialized to <code>initialCapacity</code>.
065: *
066: * @param b a Body
067: * @param initialCapacity the initialCapacity of the internal hashtable.
068: */
069: public UnitMap(Body b, int initialCapacity) {
070: unitToResult = new Hashtable<Object, Object>(initialCapacity);
071: map(b);
072: }
073:
074: /**
075: * maps each unit of the graph to the result of <code>mapTo</code>.<br>
076: * before the mapping the method <code>init</code> is called.<br>
077: * the internal hashtable is initialized to <code>initialCapacity</code>.
078: *
079: * @param g a UnitGraph
080: * @param initialCapacity the initialCapacity of the internal hashtable.
081: */
082: public UnitMap(UnitGraph g, int initialCapacity) {
083: this (g.getBody(), initialCapacity);
084: }
085:
086: /**
087: * maps each unit of this body to the result of <code>mapTo</code>.<br>
088: * before the mapping the method <code>init</code> is called.<br>
089: * the internal hashtable is initialized to <code>initialCapacity</code> and
090: * <code>loadFactor</code>.
091: *
092: * @param b a Body
093: * @param initialCapacity the initialCapacity of the internal hashtable.
094: * @param loadFactor the loadFactor of the internal hashtable.
095: */
096: public UnitMap(Body b, int initialCapacity, float loadFactor) {
097: unitToResult = new Hashtable<Object, Object>(initialCapacity);
098: init();
099: map(b);
100: }
101:
102: /**
103: * maps each unit of the graph to the result of <code>mapTo</code>.<br>
104: * before the mapping the method <code>init</code> is called.<br>
105: * the internal hashtable is initialized to <code>initialCapacity</code> and
106: * <code>loadFactor</code>.
107: *
108: * @param g a UnitGraph
109: * @param initialCapacity the initialCapacity of the internal hashtable.
110: * @param loadFactor the loadFactor of the internal hashtable.
111: */
112: public UnitMap(UnitGraph g, int initialCapacity, float loadFactor) {
113: this (g.getBody(), initialCapacity);
114: }
115:
116: /**
117: * does the actual mapping. assumes, that the hashtable is already initialized.
118: */
119: private void map(Body b) {
120: Iterator unitIt = b.getUnits().iterator();
121: while (unitIt.hasNext()) {
122: Unit currentUnit = (Unit) unitIt.next();
123: Object o = mapTo(currentUnit);
124: if (o != null)
125: unitToResult.put(currentUnit, o);
126: }
127: }
128:
129: /**
130: * allows one-time initialization before any mapping. This method is called
131: * before any mapping of a unit (but only once in the beginning).<br>
132: * If not overwritten does nothing.
133: */
134: protected void init() {
135: };
136:
137: /**
138: * maps a unit to an object. This method is called for every unit. If
139: * the returned object is <code>null</code> no object will be mapped.<br>
140: *
141: * @param the Unit to which <code>o</code> should be mapped.
142: * @return an object that is mapped to the unit, or <code>null</code>.
143: */
144: protected abstract Object mapTo(Unit unit);
145:
146: /*====== the Map-interface. all methods are deleguated tp the hashmap======*/
147:
148: public void clear() {
149: unitToResult.clear();
150: }
151:
152: public boolean containsKey(Object key) {
153: return unitToResult.containsKey(key);
154: }
155:
156: public boolean containsValue(Object value) {
157: return unitToResult.containsValue(value);
158: }
159:
160: public Set entrySet() {
161: return unitToResult.entrySet();
162: }
163:
164: public boolean equals(Object o) {
165: return unitToResult.equals(o);
166: }
167:
168: public Object get(Object key) {
169: return unitToResult.get(key);
170: }
171:
172: public int hashCode() {
173: return unitToResult.hashCode();
174: }
175:
176: public boolean isEmpty() {
177: return unitToResult.isEmpty();
178: }
179:
180: public Set<Object> keySet() {
181: return unitToResult.keySet();
182: }
183:
184: public Object put(Object key, Object value) {
185: return unitToResult.put(key, value);
186: }
187:
188: public void putAll(Map t) {
189: unitToResult.putAll(t);
190: }
191:
192: public Object remove(Object key) {
193: return unitToResult.remove(key);
194: }
195:
196: public int size() {
197: return unitToResult.size();
198: }
199:
200: public Collection<Object> values() {
201: return unitToResult.values();
202: }
203: }
|