author | mkos |
Sun, 30 Dec 2012 00:00:00 +0100 | |
changeset 22678 | ac1ea46be942 |
parent 12009 | 4abb694f273a |
permissions | -rw-r--r-- |
12009 | 1 |
/* |
22678
ac1ea46be942
8029237: Update copyright year to match last edit in jaxws repository for 2012
mkos
parents:
12009
diff
changeset
|
2 |
* Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. |
12009 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
24 |
*/ |
|
25 |
||
26 |
package com.sun.xml.internal.bind.v2.util; |
|
27 |
||
28 |
import java.util.AbstractList; |
|
29 |
import java.util.Arrays; |
|
30 |
import java.util.List; |
|
31 |
import java.util.Stack; |
|
32 |
||
33 |
/** |
|
34 |
* {@link Stack}-like data structure that allows the following efficient operations: |
|
35 |
* |
|
36 |
* <ol> |
|
37 |
* <li>Push/pop operation. |
|
38 |
* <li>Duplicate check. When an object that's already in the stack is pushed, |
|
39 |
* this class will tell you so. |
|
40 |
* </ol> |
|
41 |
* |
|
42 |
* <p> |
|
43 |
* Object equality is their identity equality. |
|
44 |
* |
|
45 |
* <p> |
|
46 |
* This class implements {@link List} for accessing items in the stack, |
|
47 |
* but {@link List} methods that alter the stack is not supported. |
|
48 |
* |
|
49 |
* @author Kohsuke Kawaguchi |
|
50 |
*/ |
|
51 |
public final class CollisionCheckStack<E> extends AbstractList<E> { |
|
52 |
private Object[] data; |
|
53 |
private int[] next; |
|
54 |
private int size = 0; |
|
55 |
||
56 |
private boolean latestPushResult = false; |
|
57 |
||
58 |
/** |
|
59 |
* True if the check shall be done by using the object identity. |
|
60 |
* False if the check shall be done with the equals method. |
|
61 |
*/ |
|
62 |
private boolean useIdentity = true; |
|
63 |
||
64 |
// for our purpose, there isn't much point in resizing this as we don't expect |
|
65 |
// the stack to grow that much. |
|
66 |
private final int[] initialHash; |
|
67 |
||
68 |
public CollisionCheckStack() { |
|
69 |
initialHash = new int[17]; |
|
70 |
data = new Object[16]; |
|
71 |
next = new int[16]; |
|
72 |
} |
|
73 |
||
74 |
/** |
|
75 |
* Set to false to use {@link Object#equals(Object)} to detect cycles. |
|
76 |
* This method can be only used when the stack is empty. |
|
77 |
*/ |
|
78 |
public void setUseIdentity(boolean useIdentity) { |
|
79 |
this.useIdentity = useIdentity; |
|
80 |
} |
|
81 |
||
82 |
public boolean getUseIdentity() { |
|
83 |
return useIdentity; |
|
84 |
} |
|
85 |
||
86 |
public boolean getLatestPushResult() { |
|
87 |
return latestPushResult; |
|
88 |
} |
|
89 |
||
90 |
/** |
|
91 |
* Pushes a new object to the stack. |
|
92 |
* |
|
93 |
* @return |
|
94 |
* true if this object has already been pushed |
|
95 |
*/ |
|
96 |
public boolean push(E o) { |
|
97 |
if(data.length==size) |
|
98 |
expandCapacity(); |
|
99 |
||
100 |
data[size] = o; |
|
101 |
int hash = hash(o); |
|
102 |
boolean r = findDuplicate(o, hash); |
|
103 |
next[size] = initialHash[hash]; |
|
104 |
initialHash[hash] = size+1; |
|
105 |
size++; |
|
106 |
this.latestPushResult = r; |
|
107 |
return latestPushResult; |
|
108 |
} |
|
109 |
||
110 |
/** |
|
111 |
* Pushes a new object to the stack without making it participate |
|
112 |
* with the collision check. |
|
113 |
*/ |
|
114 |
public void pushNocheck(E o) { |
|
115 |
if(data.length==size) |
|
116 |
expandCapacity(); |
|
117 |
data[size] = o; |
|
118 |
next[size] = -1; |
|
119 |
size++; |
|
120 |
} |
|
121 |
||
122 |
public boolean findDuplicate(E o) { |
|
123 |
int hash = hash(o); |
|
124 |
return findDuplicate(o, hash); |
|
125 |
} |
|
126 |
||
127 |
@Override |
|
128 |
public E get(int index) { |
|
129 |
return (E)data[index]; |
|
130 |
} |
|
131 |
||
132 |
@Override |
|
133 |
public int size() { |
|
134 |
return size; |
|
135 |
} |
|
136 |
||
137 |
private int hash(Object o) { |
|
138 |
return ((useIdentity?System.identityHashCode(o):o.hashCode())&0x7FFFFFFF) % initialHash.length; |
|
139 |
} |
|
140 |
||
141 |
/** |
|
142 |
* Pops an object from the stack |
|
143 |
*/ |
|
144 |
public E pop() { |
|
145 |
size--; |
|
146 |
Object o = data[size]; |
|
147 |
data[size] = null; // keeping references too long == memory leak |
|
148 |
int n = next[size]; |
|
149 |
if(n<0) { |
|
150 |
// pushed by nocheck. no need to update hash |
|
151 |
} else { |
|
152 |
int hash = hash(o); |
|
153 |
assert initialHash[hash]==size+1; |
|
154 |
initialHash[hash] = n; |
|
155 |
} |
|
156 |
return (E)o; |
|
157 |
} |
|
158 |
||
159 |
/** |
|
160 |
* Returns the top of the stack. |
|
161 |
*/ |
|
162 |
public E peek() { |
|
163 |
return (E)data[size-1]; |
|
164 |
} |
|
165 |
||
166 |
private boolean findDuplicate(E o, int hash) { |
|
167 |
int p = initialHash[hash]; |
|
168 |
while(p!=0) { |
|
169 |
p--; |
|
170 |
Object existing = data[p]; |
|
171 |
if (useIdentity) { |
|
172 |
if(existing==o) return true; |
|
173 |
} else { |
|
174 |
if (o.equals(existing)) return true; |
|
175 |
} |
|
176 |
p = next[p]; |
|
177 |
} |
|
178 |
return false; |
|
179 |
} |
|
180 |
||
181 |
private void expandCapacity() { |
|
182 |
int oldSize = data.length; |
|
183 |
int newSize = oldSize * 2; |
|
184 |
Object[] d = new Object[newSize]; |
|
185 |
int[] n = new int[newSize]; |
|
186 |
||
187 |
System.arraycopy(data,0,d,0,oldSize); |
|
188 |
System.arraycopy(next,0,n,0,oldSize); |
|
189 |
||
190 |
data = d; |
|
191 |
next = n; |
|
192 |
} |
|
193 |
||
194 |
/** |
|
195 |
* Clears all the contents in the stack. |
|
196 |
*/ |
|
197 |
public void reset() { |
|
198 |
if(size>0) { |
|
199 |
size = 0; |
|
200 |
Arrays.fill(initialHash,0); |
|
201 |
} |
|
202 |
} |
|
203 |
||
204 |
/** |
|
205 |
* String that represents the cycle. |
|
206 |
*/ |
|
207 |
public String getCycleString() { |
|
208 |
StringBuilder sb = new StringBuilder(); |
|
209 |
int i=size()-1; |
|
210 |
E obj = get(i); |
|
211 |
sb.append(obj); |
|
212 |
Object x; |
|
213 |
do { |
|
214 |
sb.append(" -> "); |
|
215 |
x = get(--i); |
|
216 |
sb.append(x); |
|
217 |
} while(obj!=x); |
|
218 |
||
219 |
return sb.toString(); |
|
220 |
} |
|
221 |
} |