KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > ba > CompactLocationNumbering


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

19
20 package edu.umd.cs.findbugs.ba;
21
22 import java.util.HashMap JavaDoc;
23 import java.util.Iterator JavaDoc;
24
25 /**
26  * Compute a compact numbering of Locations in a CFG.
27  * This is useful for analyses that want to use a BitSet to
28  * keep track of Locations.
29  *
30  * @author David Hovemeyer
31  */

32 public class CompactLocationNumbering {
33     private HashMap JavaDoc<Location, Integer JavaDoc> locationToNumberMap;
34     private HashMap JavaDoc<Integer JavaDoc, Location> numberToLocationMap;
35
36     /**
37      * Constructor.
38      *
39      * @param cfg the CFG containing the Locations to number
40      */

41     public CompactLocationNumbering(CFG cfg) {
42         this.locationToNumberMap = new HashMap JavaDoc<Location, Integer JavaDoc>();
43         this.numberToLocationMap = new HashMap JavaDoc<Integer JavaDoc, Location>();
44         build(cfg);
45     }
46     
47     /**
48      * Get the size of the numbering,
49      * which is the maximum number assigned plus one.
50      *
51      * @return the maximum number assigned plus one
52      */

53     public int getSize() {
54         return locationToNumberMap.size();
55     }
56     
57     /**
58      * Get the number of given Location,
59      * which will be a non-negative integer
60      * in the range 0..getSize() - 1.
61      *
62      * @param location
63      * @return the number of the location
64      */

65     public int getNumber(Location location) {
66         return locationToNumberMap.get(location).intValue();
67     }
68
69     /**
70      * Get the Location given its number.
71      *
72      * @param number the number
73      * @return Location corresponding to that number
74      */

75     public Location getLocation(int number) {
76         return numberToLocationMap.get((Integer JavaDoc)(number));
77     }
78
79     private void build(CFG cfg) {
80         int count = 0;
81         for (Iterator JavaDoc<Location> i = cfg.locationIterator(); i.hasNext(); ) {
82             Integer JavaDoc number = (Integer JavaDoc)(count++);
83             Location location = i.next();
84             locationToNumberMap.put(location, number);
85             numberToLocationMap.put(number, location);
86         }
87     }
88 }
89
Popular Tags