KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > util > UnitMap


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 2002 Florian Loitsch
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */

19
20 /*
21  * Modified by the Sable Research Group and others 1997-1999.
22  * See the 'credits' file distributed with Soot for the complete list of
23  * contributors. (Soot is distributed at http://www.sable.mcgill.ca/soot)
24  */

25
26
27 package soot.util;
28 import soot.jimple.toolkits.graph.*;
29 import soot.toolkits.graph.*;
30 import soot.jimple.*;
31 import soot.*;
32 import java.util.*;
33
34 /**
35  * Maps each unit to the result of <code>mapTo</code>.
36  */

37 public abstract class UnitMap implements Map {
38   private Hashtable unitToResult;
39
40   /**
41    * maps each unit of this body to the result of <code>mapTo</code>.<br>
42    * before the mapping the method <code>init</code> is called.<br>
43    * the internal hashtable is initialized without any parameter.
44    *
45    * @param b a Body
46    */

47   public UnitMap(Body b) {
48     unitToResult = new Hashtable();
49     map(b);
50   }
51
52   /**
53    * maps each unit of the graph to the result of <code>mapTo</code>.<br>
54    * before the mapping the method <code>init</code> is called.<br>
55    * the internal hashtable is initialized without any parameter.
56    *
57    * @param g a UnitGraph
58    */

59   public UnitMap(UnitGraph g) {
60     this(g.getBody());
61   }
62
63   /**
64    * maps each unit of this body to the result of <code>mapTo</code>.<br>
65    * before the mapping the method <code>init</code> is called.<br>
66    * the internal hashtable is initialized to <code>initialCapacity</code>.
67    *
68    * @param b a Body
69    * @param initialCapacity the initialCapacity of the internal hashtable.
70    */

71   public UnitMap(Body b, int initialCapacity) {
72     unitToResult = new Hashtable(initialCapacity);
73     map(b);
74   }
75
76   /**
77    * maps each unit of the graph to the result of <code>mapTo</code>.<br>
78    * before the mapping the method <code>init</code> is called.<br>
79    * the internal hashtable is initialized to <code>initialCapacity</code>.
80    *
81    * @param g a UnitGraph
82    * @param initialCapacity the initialCapacity of the internal hashtable.
83    */

84   public UnitMap(UnitGraph g, int initialCapacity) {
85     this(g.getBody(), initialCapacity);
86   }
87
88   /**
89    * maps each unit of this body to the result of <code>mapTo</code>.<br>
90    * before the mapping the method <code>init</code> is called.<br>
91    * the internal hashtable is initialized to <code>initialCapacity</code> and
92    * <code>loadFactor</code>.
93    *
94    * @param b a Body
95    * @param initialCapacity the initialCapacity of the internal hashtable.
96    * @param loadFactor the loadFactor of the internal hashtable.
97    */

98   public UnitMap(Body b, int initialCapacity, float loadFactor) {
99     unitToResult = new Hashtable(initialCapacity);
100     init();
101     map(b);
102   }
103
104   /**
105    * maps each unit of the graph to the result of <code>mapTo</code>.<br>
106    * before the mapping the method <code>init</code> is called.<br>
107    * the internal hashtable is initialized to <code>initialCapacity</code> and
108    * <code>loadFactor</code>.
109    *
110    * @param g a UnitGraph
111    * @param initialCapacity the initialCapacity of the internal hashtable.
112    * @param loadFactor the loadFactor of the internal hashtable.
113    */

114   public UnitMap(UnitGraph g, int initialCapacity, float loadFactor) {
115     this(g.getBody(), initialCapacity);
116   }
117
118   /**
119    * does the actual mapping. assumes, that the hashtable is already initialized.
120    */

121   private void map(Body b) {
122     Iterator unitIt = b.getUnits().iterator();
123     while (unitIt.hasNext()) {
124       Unit currentUnit = (Unit)unitIt.next();
125       Object JavaDoc o = mapTo(currentUnit);
126       if (o != null)
127         unitToResult.put(currentUnit, o);
128     }
129   }
130
131   /**
132    * allows one-time initialization before any mapping. This method is called
133    * before any mapping of a unit (but only once in the beginning).<br>
134    * If not overwritten does nothing.
135    */

136   protected void init() {};
137
138   /**
139    * maps a unit to an object. This method is called for every unit. If
140    * the returned object is <code>null</code> no object will be mapped.<br>
141    *
142    * @param the Unit to which <code>o</code> should be mapped.
143    * @return an object that is mapped to the unit, or <code>null</code>.
144    */

145   protected abstract Object JavaDoc mapTo(Unit unit);
146
147   /*====== the Map-interface. all methods are deleguated tp the hashmap======*/
148
149   public void clear() {
150     unitToResult.clear();
151   }
152
153   public boolean containsKey(Object JavaDoc key) {
154     return unitToResult.containsKey(key);
155   }
156
157   public boolean containsValue(Object JavaDoc value) {
158     return unitToResult.containsValue(value);
159   }
160
161   public Set entrySet() {
162     return unitToResult.entrySet();
163   }
164
165   public boolean equals(Object JavaDoc o) {
166     return unitToResult.equals(o);
167   }
168
169   public Object JavaDoc get(Object JavaDoc key) {
170     return unitToResult.get(key);
171   }
172
173   public int hashCode() {
174     return unitToResult.hashCode();
175   }
176
177   public boolean isEmpty() {
178     return unitToResult.isEmpty();
179   }
180
181   public Set keySet() {
182     return unitToResult.keySet();
183   }
184
185   public Object JavaDoc put(Object JavaDoc key, Object JavaDoc value) {
186     return unitToResult.put(key, value);
187   }
188
189   public void putAll(Map t) {
190     unitToResult.putAll(t);
191   }
192
193   public Object JavaDoc remove(Object JavaDoc key) {
194     return unitToResult.remove(key);
195   }
196
197   public int size() {
198     return unitToResult.size();
199   }
200
201   public Collection values() {
202     return unitToResult.values();
203   }
204 }
205
206
Popular Tags