src/share/jaxws_classes/com/sun/xml/internal/bind/v2/util/CollisionCheckStack.java

Thu, 12 Oct 2017 19:44:07 +0800

author
aoqi
date
Thu, 12 Oct 2017 19:44:07 +0800
changeset 760
e530533619ec
parent 0
373ffda63c9a
permissions
-rw-r--r--

merge

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

mercurial