hotspot/test/compiler/6865031/Test.java
author kvn
Wed, 09 Sep 2009 16:28:03 -0700
changeset 3803 a7d7c2525f61
parent 3599 35bb709f2c62
child 5547 f4b087cbb361
permissions -rw-r--r--
6880533: test/compiler/6865031/Test.java miss -XX:+IgnoreUnrecognizedVMOptions Summary: Add missing test option -XX:+IgnoreUnrecognizedVMOptions. Reviewed-by: never
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3599
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
     1
/*
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
     2
 * Copyright 2009 Goldman Sachs International.  All Rights Reserved.
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
     4
 *
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
     7
 * published by the Free Software Foundation.
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
     8
 *
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
    13
 * accompanied this code).
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
    14
 *
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
    18
 *
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
    21
 * have any questions.
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
    22
 *
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
    23
 */
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
    24
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
    25
/*
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
    26
 * @test
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
    27
 * @bug 6865031
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
    28
 * @summary Application gives bad result (throws bad exception) with compressed oops
3803
a7d7c2525f61 6880533: test/compiler/6865031/Test.java miss -XX:+IgnoreUnrecognizedVMOptions
kvn
parents: 3599
diff changeset
    29
 * @run main/othervm -XX:+IgnoreUnrecognizedVMOptions -XX:+UseCompressedOops -XX:HeapBaseMinAddress=32g -XX:-LoopUnswitching -XX:CompileCommand=inline,AbstractMemoryEfficientList.equals Test hello goodbye
3599
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
    30
 */
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
    31
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
    32
import java.lang.ref.ReferenceQueue;
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
    33
import java.lang.ref.WeakReference;
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
    34
import java.util.ArrayList;
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
    35
import java.util.Arrays;
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
    36
import java.util.List;
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
    37
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
    38
interface MyList {
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
    39
    public int size();
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
    40
    public Object set(final int index, final Object element);
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
    41
    public Object get(final int index);
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
    42
}
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
    43
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
    44
abstract class AbstractMemoryEfficientList implements MyList {
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
    45
    abstract public int size();
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
    46
    abstract public Object get(final int index);
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
    47
    abstract public Object set(final int index, final Object element);
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
    48
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
    49
    public boolean equals(Object o) {
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
    50
        if (o == this) {
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
    51
            return true;
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
    52
        }
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
    53
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
    54
        if (!(o instanceof MyList)) {
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
    55
            return false;
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
    56
        }
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
    57
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
    58
        final MyList that = (MyList) o;
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
    59
        if (this.size() != that.size()) {
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
    60
            return false;
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
    61
        }
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
    62
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
    63
        for (int i = 0; i < this.size(); i++) {
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
    64
            try {
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
    65
                if (!((this.get(i)).equals(that.get(i)))) {
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
    66
                    return false;
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
    67
                }
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
    68
            } catch (IndexOutOfBoundsException e) {
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
    69
                System.out.println("THROWING RT EXC");
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
    70
                System.out.println("concurrent modification of this:" + this.getClass() + ":" + System.identityHashCode(this) + "; that:" + that.getClass() + ":" + System.identityHashCode(that) + "; i:" + i);
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
    71
                e.printStackTrace();
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
    72
                System.exit(97);
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
    73
                throw new RuntimeException("concurrent modification of this:" + this.getClass() + ":" + System.identityHashCode(this) + "; that:" + that.getClass() + ":" + System.identityHashCode(that) + "; i:" + i, e);
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
    74
            }
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
    75
        }
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
    76
        return true;
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
    77
    }
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
    78
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
    79
    public int hashCode() {
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
    80
        int hashCode = 1;
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
    81
        for (int i = 0; i < this.size(); i++) {
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
    82
            Object obj = this.get(i);
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
    83
            hashCode = 31 * hashCode + (obj == null ? 0 : obj.hashCode());
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
    84
        }
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
    85
        return hashCode;
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
    86
    }
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
    87
}
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
    88
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
    89
final class SingletonList extends AbstractMemoryEfficientList {
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
    90
    private Object element1;
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
    91
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
    92
    SingletonList(final Object obj1) {
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
    93
        super();
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
    94
        this.element1 = obj1;
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
    95
    }
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
    96
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
    97
    public int size() {
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
    98
        return 1;
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
    99
    }
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   100
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   101
    public Object get(final int index) {
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   102
        if (index == 0) {
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   103
            return this.element1;
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   104
        } else {
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   105
            throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + this.size());
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   106
        }
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   107
    }
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   108
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   109
    public Object set(final int index, final Object element) {
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   110
        if (index == 0) {
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   111
            final Object previousElement = this.element1;
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   112
            this.element1 = element;
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   113
            return previousElement;
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   114
        } else {
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   115
            throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + this.size());
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   116
        }
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   117
    }
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   118
}
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   119
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   120
final class DoubletonList extends AbstractMemoryEfficientList {
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   121
    private Object element1;
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   122
    private Object element2;
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   123
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   124
    DoubletonList(final Object obj1, final Object obj2) {
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   125
        this.element1 = obj1;
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   126
        this.element2 = obj2;
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   127
    }
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   128
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   129
    public int size() {
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   130
        return 2;
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   131
    }
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   132
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   133
    public Object get(final int index) {
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   134
        switch (index) {
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   135
            case 0 : return this.element1;
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   136
            case 1 : return this.element2;
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   137
            default: throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + this.size());
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   138
        }
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   139
    }
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   140
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   141
    public Object set(final int index, final Object element) {
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   142
        switch (index) {
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   143
            case 0 :
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   144
            {
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   145
                final Object previousElement = this.element1;
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   146
                this.element1 = element;
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   147
                return previousElement;
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   148
            }
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   149
            case 1 :
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   150
            {
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   151
                final Object previousElement = this.element2;
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   152
                this.element2 = element;
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   153
                return previousElement;
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   154
            }
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   155
            default : throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + this.size());
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   156
        }
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   157
    }
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   158
}
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   159
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   160
class WeakPool<V> {
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   161
    protected static final int DEFAULT_INITIAL_CAPACITY = 16;
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   162
    private static final int MAXIMUM_CAPACITY = 1 << 30;
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   163
    private static final float DEFAULT_LOAD_FACTOR = 0.75f;
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   164
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   165
    protected Entry<V>[] table;
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   166
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   167
    private int size;
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   168
    protected int threshold;
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   169
    private final float loadFactor;
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   170
    private final ReferenceQueue<V> queue = new ReferenceQueue<V>();
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   171
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   172
    public WeakPool()
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   173
    {
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   174
        this.loadFactor = DEFAULT_LOAD_FACTOR;
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   175
        threshold = DEFAULT_INITIAL_CAPACITY;
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   176
        table = new Entry[DEFAULT_INITIAL_CAPACITY];
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   177
    }
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   178
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   179
    /**
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   180
     * Check for equality of non-null reference x and possibly-null y.  By
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   181
     * default uses Object.equals.
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   182
     */
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   183
    private boolean eq(Object x, Object y)
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   184
    {
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   185
        return x == y || x.equals(y);
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   186
    }
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   187
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   188
    /**
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   189
     * Return index for hash code h.
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   190
     */
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   191
    private int indexFor(int h, int length)
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   192
    {
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   193
        return h & length - 1;
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   194
    }
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   195
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   196
    /**
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   197
     * Expunge stale entries from the table.
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   198
     */
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   199
    private void expungeStaleEntries()
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   200
    {
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   201
        Object r;
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   202
        while ((r = queue.poll()) != null)
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   203
        {
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   204
            Entry e = (Entry) r;
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   205
            int h = e.hash;
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   206
            int i = indexFor(h, table.length);
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   207
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   208
            // System.out.println("EXPUNGING " + h);
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   209
            Entry<V> prev = table[i];
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   210
            Entry<V> p = prev;
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   211
            while (p != null)
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   212
            {
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   213
                Entry<V> next = p.next;
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   214
                if (p == e)
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   215
                {
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   216
                    if (prev == e)
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   217
                    {
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   218
                        table[i] = next;
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   219
                    }
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   220
                    else
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   221
                    {
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   222
                        prev.next = next;
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   223
                    }
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   224
                    e.next = null;  // Help GC
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   225
                    size--;
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   226
                    break;
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   227
                }
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   228
                prev = p;
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   229
                p = next;
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   230
            }
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   231
        }
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   232
    }
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   233
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   234
    /**
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   235
     * Return the table after first expunging stale entries
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   236
     */
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   237
    private Entry<V>[] getTable()
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   238
    {
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   239
        expungeStaleEntries();
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   240
        return table;
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   241
    }
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   242
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   243
    /**
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   244
     * Returns the number of key-value mappings in this map.
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   245
     * This result is a snapshot, and may not reflect unprocessed
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   246
     * entries that will be removed before next attempted access
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   247
     * because they are no longer referenced.
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   248
     */
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   249
    public int size()
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   250
    {
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   251
        if (size == 0)
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   252
        {
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   253
            return 0;
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   254
        }
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   255
        expungeStaleEntries();
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   256
        return size;
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   257
    }
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   258
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   259
    /**
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   260
     * Returns <tt>true</tt> if this map contains no key-value mappings.
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   261
     * This result is a snapshot, and may not reflect unprocessed
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   262
     * entries that will be removed before next attempted access
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   263
     * because they are no longer referenced.
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   264
     */
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   265
    public boolean isEmpty()
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   266
    {
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   267
        return size() == 0;
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   268
    }
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   269
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   270
    /**
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   271
     * Returns the value stored in the pool that equals the requested key
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   272
     * or <tt>null</tt> if the map contains no mapping for
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   273
     * this key (or the key is null)
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   274
     *
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   275
     * @param key the key whose equals value is to be returned.
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   276
     * @return the object that is equal the specified key, or
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   277
     *         <tt>null</tt> if key is null or no object in the pool equals the key.
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   278
     */
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   279
    public V get(V key)
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   280
    {
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   281
        if (key == null)
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   282
        {
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   283
            return null;
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   284
        }
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   285
        int h = key.hashCode();
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   286
        Entry<V>[] tab = getTable();
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   287
        int index = indexFor(h, tab.length);
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   288
        Entry<V> e = tab[index];
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   289
        while (e != null)
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   290
        {
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   291
            V candidate = e.get();
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   292
            if (e.hash == h && eq(key, candidate))
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   293
            {
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   294
                return candidate;
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   295
            }
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   296
            e = e.next;
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   297
        }
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   298
        return null;
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   299
    }
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   300
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   301
    /**
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   302
     * Returns the entry associated with the specified key in the HashMap.
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   303
     * Returns null if the HashMap contains no mapping for this key.
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   304
     */
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   305
    Entry getEntry(Object key)
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   306
    {
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   307
        int h = key.hashCode();
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   308
        Entry[] tab = getTable();
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   309
        int index = indexFor(h, tab.length);
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   310
        Entry e = tab[index];
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   311
        while (e != null && !(e.hash == h && eq(key, e.get())))
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   312
        {
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   313
            e = e.next;
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   314
        }
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   315
        return e;
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   316
    }
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   317
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   318
    /**
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   319
     * Places the object into the pool. If the object is null, nothing happens.
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   320
     * If an equal object already exists, it is not replaced.
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   321
     *
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   322
     * @param key the object to put into the pool. key may be null.
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   323
     * @return the object in the pool that is equal to the key, or the newly placed key if no such object existed when put was called
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   324
     */
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   325
    public V put(V key)
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   326
    {
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   327
        if (key == null)
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   328
        {
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   329
            return null;
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   330
        }
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   331
        int h = key.hashCode();
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   332
        Entry<V>[] tab = getTable();
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   333
        int i = indexFor(h, tab.length);
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   334
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   335
        for (Entry<V> e = tab[i]; e != null; e = e.next)
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   336
        {
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   337
            V candidate = e.get();
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   338
            if (h == e.hash && eq(key, candidate))
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   339
            {
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   340
                return candidate;
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   341
            }
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   342
        }
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   343
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   344
        tab[i] = new Entry<V>(key, queue, h, tab[i]);
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   345
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   346
        if (++size >= threshold)
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   347
        {
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   348
            resize(tab.length * 2);
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   349
        }
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   350
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   351
    // System.out.println("Added " + key + " to pool");
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   352
        return key;
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   353
    }
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   354
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   355
    /**
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   356
     * Rehashes the contents of this map into a new array with a
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   357
     * larger capacity.  This method is called automatically when the
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   358
     * number of keys in this map reaches its threshold.
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   359
     * <p/>
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   360
     * If current capacity is MAXIMUM_CAPACITY, this method does not
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   361
     * resize the map, but but sets threshold to Integer.MAX_VALUE.
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   362
     * This has the effect of preventing future calls.
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   363
     *
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   364
     * @param newCapacity the new capacity, MUST be a power of two;
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   365
     *                    must be greater than current capacity unless current
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   366
     *                    capacity is MAXIMUM_CAPACITY (in which case value
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   367
     *                    is irrelevant).
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   368
     */
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   369
    void resize(int newCapacity)
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   370
    {
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   371
        Entry<V>[] oldTable = getTable();
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   372
        int oldCapacity = oldTable.length;
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   373
        if (oldCapacity == MAXIMUM_CAPACITY)
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   374
        {
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   375
            threshold = Integer.MAX_VALUE;
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   376
            return;
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   377
        }
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   378
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   379
        Entry<V>[] newTable = new Entry[newCapacity];
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   380
        transfer(oldTable, newTable);
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   381
        table = newTable;
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   382
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   383
        /*
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   384
         * If ignoring null elements and processing ref queue caused massive
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   385
         * shrinkage, then restore old table.  This should be rare, but avoids
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   386
         * unbounded expansion of garbage-filled tables.
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   387
         */
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   388
        if (size >= threshold / 2)
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   389
        {
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   390
            threshold = (int) (newCapacity * loadFactor);
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   391
        }
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   392
        else
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   393
        {
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   394
            expungeStaleEntries();
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   395
            transfer(newTable, oldTable);
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   396
            table = oldTable;
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   397
        }
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   398
    }
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   399
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   400
    /**
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   401
     * Transfer all entries from src to dest tables
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   402
     */
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   403
    private void transfer(Entry[] src, Entry[] dest)
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   404
    {
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   405
        for (int j = 0; j < src.length; ++j)
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   406
        {
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   407
            Entry e = src[j];
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   408
            src[j] = null;
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   409
            while (e != null)
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   410
            {
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   411
                Entry next = e.next;
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   412
                Object key = e.get();
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   413
                if (key == null)
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   414
                {
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   415
                    e.next = null;  // Help GC
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   416
                    size--;
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   417
                }
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   418
                else
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   419
                {
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   420
                    int i = indexFor(e.hash, dest.length);
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   421
                    e.next = dest[i];
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   422
                    dest[i] = e;
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   423
                }
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   424
                e = next;
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   425
            }
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   426
        }
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   427
    }
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   428
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   429
    /**
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   430
     * Removes the object in the pool that equals the key.
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   431
     *
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   432
     * @param key
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   433
     * @return previous value associated with specified key, or <tt>null</tt>
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   434
     *         if there was no mapping for key or the key is null.
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   435
     */
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   436
    public V removeFromPool(V key)
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   437
    {
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   438
        if (key == null)
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   439
        {
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   440
            return null;
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   441
        }
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   442
        int h = key.hashCode();
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   443
        Entry<V>[] tab = getTable();
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   444
        int i = indexFor(h, tab.length);
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   445
        Entry<V> prev = tab[i];
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   446
        Entry<V> e = prev;
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   447
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   448
        while (e != null)
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   449
        {
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   450
            Entry<V> next = e.next;
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   451
            V candidate = e.get();
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   452
            if (h == e.hash && eq(key, candidate))
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   453
            {
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   454
                size--;
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   455
                if (prev == e)
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   456
                {
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   457
                    tab[i] = next;
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   458
                }
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   459
                else
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   460
                {
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   461
                    prev.next = next;
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   462
                }
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   463
                return candidate;
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   464
            }
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   465
            prev = e;
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   466
            e = next;
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   467
        }
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   468
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   469
        return null;
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   470
    }
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   471
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   472
    /**
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   473
     * Removes all mappings from this map.
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   474
     */
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   475
    public void clear()
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   476
    {
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   477
        // clear out ref queue. We don't need to expunge entries
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   478
        // since table is getting cleared.
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   479
        while (queue.poll() != null)
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   480
        {
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   481
            // nop
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   482
        }
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   483
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   484
        table = new Entry[DEFAULT_INITIAL_CAPACITY];
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   485
        threshold = DEFAULT_INITIAL_CAPACITY;
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   486
        size = 0;
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   487
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   488
        // Allocation of array may have caused GC, which may have caused
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   489
        // additional entries to go stale.  Removing these entries from the
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   490
        // reference queue will make them eligible for reclamation.
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   491
        while (queue.poll() != null)
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   492
        {
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   493
            // nop
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   494
        }
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   495
    }
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   496
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   497
    /**
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   498
     * The entries in this hash table extend WeakReference, using its main ref
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   499
     * field as the key.
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   500
     */
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   501
    protected static class Entry<V>
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   502
    extends WeakReference<V>
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   503
    {
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   504
        private final int hash;
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   505
        private Entry<V> next;
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   506
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   507
        /**
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   508
         * Create new entry.
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   509
         */
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   510
        Entry(final V key, final ReferenceQueue<V> queue, final int hash, final Entry<V> next)
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   511
        {
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   512
            super(key, queue);
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   513
            this.hash = hash;
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   514
            this.next = next;
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   515
        }
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   516
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   517
        public V getKey()
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   518
        {
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   519
            return super.get();
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   520
        }
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   521
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   522
        public boolean equals(Object o)
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   523
        {
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   524
            if (!(o instanceof WeakPool.Entry))
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   525
            {
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   526
                return false;
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   527
            }
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   528
            WeakPool.Entry<V> that = (WeakPool.Entry<V>) o;
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   529
            V k1 = this.getKey();
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   530
            V k2 = that.getKey();
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   531
            return (k1==k2 || k1.equals(k2));
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   532
        }
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   533
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   534
        public int hashCode()
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   535
        {
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   536
            return this.hash;
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   537
        }
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   538
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   539
        public String toString()
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   540
        {
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   541
            return String.valueOf(this.getKey());
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   542
        }
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   543
    }
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   544
}
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   545
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   546
final class MultiSynonymKey {
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   547
    private List<MyList> keys;
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   548
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   549
    public MultiSynonymKey() {
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   550
        keys = new ArrayList<MyList>();
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   551
    }
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   552
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   553
    public MultiSynonymKey(MyList... arg) {
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   554
        keys = Arrays.asList(arg);
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   555
    }
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   556
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   557
    public List<MyList> getKeys() {
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   558
        return keys;
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   559
    }
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   560
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   561
    public int hashCode() {
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   562
        return this.getKeys().hashCode();
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   563
    }
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   564
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   565
    public boolean equals(Object obj) {
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   566
        if (this == obj) {
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   567
            return true;
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   568
        }
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   569
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   570
        if (!(obj instanceof MultiSynonymKey)) {
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   571
            return false;
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   572
        }
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   573
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   574
        MultiSynonymKey that = (MultiSynonymKey) obj;
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   575
        return this.getKeys().equals(that.getKeys());
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   576
    }
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   577
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   578
    public String toString() {
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   579
        return this.getClass().getName() + this.getKeys().toString();
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   580
    }
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   581
}
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   582
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   583
public class Test extends Thread {
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   584
    static public Test test;
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   585
    static private byte[] arg1;
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   586
    static private byte[] arg2;
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   587
    static public WeakPool<MultiSynonymKey> wp;
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   588
    public volatile MultiSynonymKey ml1;
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   589
    public volatile MultiSynonymKey ml2;
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   590
    private volatile MultiSynonymKey ml3;
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   591
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   592
    public void run() {
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   593
        int count=0;
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   594
        while (true) {
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   595
            try {
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   596
                Thread.sleep(10);
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   597
            } catch (Exception e) {}
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   598
            synchronized (wp) {
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   599
                ml2 = new MultiSynonymKey(new DoubletonList(new String(arg1), new String(arg2)));
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   600
                wp.put(ml2);
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   601
                ml3 = new MultiSynonymKey(new DoubletonList(new String(arg1), new String(arg2)));
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   602
            }
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   603
            try {
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   604
                Thread.sleep(10);
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   605
            } catch (Exception e) {}
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   606
            synchronized (wp) {
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   607
                ml1 = new MultiSynonymKey(new SingletonList(new String(arg1)));
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   608
                wp.put(ml1);
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   609
                ml3 = new MultiSynonymKey(new SingletonList(new String(arg1)));
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   610
            }
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   611
            if (count++==100)
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   612
                System.exit(95);
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   613
        }
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   614
    }
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   615
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   616
    public static void main(String[] args) throws Exception {
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   617
        wp = new WeakPool<MultiSynonymKey>();
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   618
        test = new Test();
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   619
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   620
        test.arg1 = args[0].getBytes();
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   621
        test.arg2 = args[1].getBytes();
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   622
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   623
        test.ml1 = new MultiSynonymKey(new SingletonList(new String(test.arg1)));
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   624
        test.ml2 = new MultiSynonymKey(new DoubletonList(new String(test.arg1), new String(test.arg2)));
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   625
        test.ml3 = new MultiSynonymKey(new DoubletonList(new String(test.arg1), new String(test.arg2)));
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   626
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   627
        wp.put(test.ml1);
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   628
        wp.put(test.ml2);
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   629
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   630
        test.setDaemon(true);
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   631
        test.start();
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   632
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   633
        int counter = 0;
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   634
        while (true) {
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   635
            synchronized (wp) {
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   636
                MultiSynonymKey foo = test.ml3;
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   637
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   638
                if (wp.put(foo) == foo) {
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   639
                    // System.out.println("foo " + counter);
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   640
                    // System.out.println(foo);
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   641
                }
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   642
            }
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   643
            counter++;
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   644
        }
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   645
    }
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   646
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   647
    private boolean eq(Object x, Object y) {
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   648
        return x == y || x.equals(y);
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   649
    }
35bb709f2c62 6865031: Application gives bad result (throws bad exception) with compressed oops
kvn
parents:
diff changeset
   650
}