KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > toolkits > scalar > ObjectIntMapper


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.toolkits.scalar;
28
29 import soot.util.*;
30 import java.util.*;
31
32 /**
33  * gives an injection of Objects to ints. Different instances of
34  * <code>ObjectIntMap</code> may map different ints to the same object.
35  */

36 class ObjectIntMapper {
37   private Vector intToObjects;
38   private int counter;
39   private Map objectToInts;
40   
41   public ObjectIntMapper() {
42     intToObjects = new Vector();
43     objectToInts = new HashMap();
44     counter = 0;
45   }
46
47   public ObjectIntMapper(FlowUniverse flowUniverse) {
48     this(flowUniverse.iterator(), flowUniverse.size());
49   }
50
51   public ObjectIntMapper(Collection collection) {
52     this(collection.iterator(), collection.size());
53   }
54
55   private ObjectIntMapper(Iterator it, int initSize) {
56     intToObjects = new Vector(initSize);
57     objectToInts = new HashMap(initSize);
58     counter = 0;
59     while (it.hasNext())
60       add(it.next());
61   }
62
63   /**
64    * adds <code>o</code> into the map. no test are made, if it is already in the
65    * map.
66    */

67   private int add(Object JavaDoc o) {
68     objectToInts.put(o, new Integer JavaDoc(counter));
69     intToObjects.add(o);
70     return counter++;
71   }
72
73   /**
74    * returns the mapping of <code>o</code>. if there has been a call to
75    * <code>objectToInt</code> with the same <code>o</code> before, the same
76    * value will be returned.
77    *
78    * @param o
79    * @return <code>o</code>'s mapping
80    */

81   public int getInt(Object JavaDoc o) {
82     Integer JavaDoc i = (Integer JavaDoc)objectToInts.get(o);
83     if (i != null) return i.intValue();
84     return add(o);
85   }
86
87   /**
88    * returns the object associated to <code>i</code>.
89    *
90    * @param i
91    * @return <code>i</code>'s object
92    */

93   public Object JavaDoc getObject(int i) {
94     return intToObjects.get(i);
95   }
96
97   /**
98    * returns true, if <code>o</code> has already been mapped.
99    *
100    * @param o
101    * @return true if <code>o</code> has already a number.
102    */

103   public boolean contains(Object JavaDoc o) {
104     return objectToInts.containsKey(o);
105   }
106
107   /**
108    * returns the number of mapped objects.
109    */

110   public int size() {
111     return counter;
112   }
113
114 }
115
Popular Tags