KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > ba > obl > Path


1 /*
2  * Bytecode Analysis Framework
3  * Copyright (C) 2004, 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.obl;
21
22 /**
23  * A Path is a sequence of program statements.
24  * For our purposes, basic blocks are considered statements.
25  *
26  * <p>See Weimer and Necula,
27  * <a HREF="http://doi.acm.org/10.1145/1028976.1029011"
28  * >Finding and preventing run-time error handling mistakes</a>,
29  * OOPSLA 2004.</p>
30  *
31  * @author David Hovemeyer
32  */

33 public class Path {
34     private static final int DEFAULT_CAPACITY = 8;
35     private static final int INVALID_HASH_CODE = -1;
36
37     private int[] blockIdList;
38     private int length;
39     private int cachedHashCode;
40
41     public Path() {
42         this.blockIdList = new int[DEFAULT_CAPACITY];
43         this.length = 0;
44         invalidate();
45     }
46
47     public void append(int id) {
48         grow(length);
49         blockIdList[length] = id;
50         ++length;
51         invalidate();
52     }
53
54     public int getBlockIdAt(int index) {
55         return blockIdList[index];
56     }
57
58     public int getLength() {
59         return length;
60     }
61     
62     /**
63      * Return an exact copy of this Path.
64      *
65      * @return an exact copy of this Path
66      */

67     public Path duplicate() {
68         Path dup = new Path();
69         dup.copyFrom(this);
70         return dup;
71     }
72     
73     /**
74      * Make this Path identical to the given one.
75      *
76      * @param other a Path to which this object should be made identical
77      */

78     public void copyFrom(Path other) {
79         grow(other.length - 1);
80         System.arraycopy(other.blockIdList, 0, this.blockIdList, 0, other.length);
81         this.length = other.length;
82         this.cachedHashCode = other.cachedHashCode;
83     }
84
85     private void invalidate() {
86         this.cachedHashCode = INVALID_HASH_CODE;
87     }
88     
89     @Override JavaDoc
90          public int hashCode() {
91         if (cachedHashCode == INVALID_HASH_CODE) {
92             int value = 0;
93             for (int i = 0; i < this.length; ++i) {
94                 value += (i * 1009 * blockIdList[i]);
95             }
96             cachedHashCode = value;
97         }
98         return cachedHashCode;
99     }
100     
101     @Override JavaDoc
102          public boolean equals(Object JavaDoc o) {
103         if (o == null || o.getClass() != this.getClass())
104             return false;
105         Path other = (Path) o;
106         if (this.length != other.length)
107             return false;
108         for (int i = 0; i < this.length; ++i) {
109             if (this.blockIdList[i] != other.blockIdList[i])
110                 return false;
111         }
112         return true;
113     }
114     
115     private static final String JavaDoc SYMBOLS =
116         "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!@#$%^&*()";
117     
118     @Override JavaDoc
119          public String JavaDoc toString() {
120         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
121         for (int i = 0; i < length; ++i) {
122             int block = blockIdList[i];
123             if (block < SYMBOLS.length())
124                 buf.append(SYMBOLS.charAt(block));
125             else
126                 buf.append("'" + block + "'");
127         }
128         return buf.toString();
129     }
130
131     private void grow(int index) {
132         if (index >= blockIdList.length) {
133             int newLen = blockIdList.length;
134             do {
135                 newLen *= 2;
136             } while (index >= newLen);
137
138             int[] arr = new int[newLen];
139             System.arraycopy(this.blockIdList, 0, arr, 0, length);
140             this.blockIdList = arr;
141         }
142     }
143 }
144
145 // vim:ts=4
146
Popular Tags