jdk/src/share/classes/sun/misc/Cache.java
author darcy
Tue, 07 Jan 2014 19:19:32 -0800
changeset 22123 fac5d9194929
parent 5506 202f599c92aa
child 22565 2f3102102bd9
permissions -rw-r--r--
8031369: Fix raw types warnings in sun.misc.{Cache, SoftCache} Reviewed-by: mduigou, lancea
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
22123
fac5d9194929 8031369: Fix raw types warnings in sun.misc.{Cache, SoftCache}
darcy
parents: 5506
diff changeset
     2
 * Copyright (c) 1995, 2014, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.misc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.Dictionary;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.Enumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.NoSuchElementException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * Caches the collision list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
class CacheEntry extends Ref {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
    int hash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    Object key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    CacheEntry next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    public Object reconstitute() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * The Cache class. Maps keys to values. Any object can be used as
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * a key and/or value.  This is very similar to the Hashtable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * class, except that after putting an object into the Cache,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * it is not guaranteed that a subsequent get will return it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * The Cache will automatically remove entries if memory is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * getting tight and if the entry is not referenced from outside
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * the Cache.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * To sucessfully store and retrieve objects from a hash table the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * object used as the key must implement the hashCode() and equals()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * methods.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * This example creates a Cache of numbers. It uses the names of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * the numbers as keys:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *      Cache numbers = new Cache();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *      numbers.put("one", new Integer(1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *      numbers.put("two", new Integer(1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *      numbers.put("three", new Integer(1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * To retrieve a number use:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *      Integer n = (Integer)numbers.get("two");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 *      if (n != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 *          System.out.println("two = " + n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 *      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * @see java.lang.Object#hashCode
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * @see java.lang.Object#equals
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * @see sun.misc.Ref
22123
fac5d9194929 8031369: Fix raw types warnings in sun.misc.{Cache, SoftCache}
darcy
parents: 5506
diff changeset
    76
 * @deprecated Consider {@link java.util.LinkedHashMap} for LRU caches.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 */
22123
fac5d9194929 8031369: Fix raw types warnings in sun.misc.{Cache, SoftCache}
darcy
parents: 5506
diff changeset
    78
@Deprecated
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
public
22123
fac5d9194929 8031369: Fix raw types warnings in sun.misc.{Cache, SoftCache}
darcy
parents: 5506
diff changeset
    80
    class Cache extends Dictionary<Object, Object> {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * The hash table data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    private CacheEntry table[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * The total number of entries in the hash table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    private int count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * Rehashes the table when count exceeds this threshold.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    private int threshold;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * The load factor for the hashtable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    private float loadFactor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    private void init(int initialCapacity, float loadFactor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        if ((initialCapacity <= 0) || (loadFactor <= 0.0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        this.loadFactor = loadFactor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        table = new CacheEntry[initialCapacity];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        threshold = (int) (initialCapacity * loadFactor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * Constructs a new, empty Cache with the specified initial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * capacity and the specified load factor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * @param initialCapacity the initial number of buckets
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * @param loadFactor a number between 0.0 and 1.0, it defines
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     *          the threshold for rehashing the Cache into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     *          a bigger one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * @exception IllegalArgumentException If the initial capacity
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * is less than or equal to zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * @exception IllegalArgumentException If the load factor is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * less than or equal to zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    public Cache (int initialCapacity, float loadFactor) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        init(initialCapacity, loadFactor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * Constructs a new, empty Cache with the specified initial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * capacity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * @param initialCapacity the initial number of buckets
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    public Cache (int initialCapacity) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        init(initialCapacity, 0.75f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * Constructs a new, empty Cache. A default capacity and load factor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * is used. Note that the Cache will automatically grow when it gets
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * full.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    public Cache () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            init(101, 0.75f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        } catch (IllegalArgumentException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            // This should never happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            throw new Error("panic");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * Returns the number of elements contained within the Cache.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    public int size() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        return count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * Returns true if the Cache contains no elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    public boolean isEmpty() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        return count == 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * Returns an enumeration of the Cache's keys.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * @see Cache#elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * @see Enumeration
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     */
22123
fac5d9194929 8031369: Fix raw types warnings in sun.misc.{Cache, SoftCache}
darcy
parents: 5506
diff changeset
   168
    public synchronized Enumeration<Object> keys() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        return new CacheEnumerator(table, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * Returns an enumeration of the elements. Use the Enumeration methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * on the returned object to fetch the elements sequentially.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * @see Cache#keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * @see Enumeration
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     */
22123
fac5d9194929 8031369: Fix raw types warnings in sun.misc.{Cache, SoftCache}
darcy
parents: 5506
diff changeset
   178
    public synchronized Enumeration<Object> elements() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        return new CacheEnumerator(table, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * Gets the object associated with the specified key in the Cache.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * @param key the key in the hash table
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * @returns the element for the key or null if the key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     *          is not defined in the hash table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * @see Cache#put
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    public synchronized Object get(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        CacheEntry tab[] = table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        int hash = key.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        int index = (hash & 0x7FFFFFFF) % tab.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        for (CacheEntry e = tab[index]; e != null; e = e.next) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            if ((e.hash == hash) && e.key.equals(key)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                return e.check();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * Rehashes the contents of the table into a bigger table.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * This is method is called automatically when the Cache's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * size exceeds the threshold.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    protected void rehash() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        int oldCapacity = table.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        CacheEntry oldTable[] = table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        int newCapacity = oldCapacity * 2 + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        CacheEntry newTable[] = new CacheEntry[newCapacity];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        threshold = (int) (newCapacity * loadFactor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        table = newTable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        // System.out.println("rehash old=" + oldCapacity + ", new=" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        // newCapacity + ", thresh=" + threshold + ", count=" + count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        for (int i = oldCapacity; i-- > 0;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            for (CacheEntry old = oldTable[i]; old != null;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                CacheEntry e = old;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                old = old.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                if (e.check() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                    int index = (e.hash & 0x7FFFFFFF) % newCapacity;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                    e.next = newTable[index];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                    newTable[index] = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                    count--;    /* remove entries that have disappeared */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * Puts the specified element into the Cache, using the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * key.  The element may be retrieved by doing a get() with the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * key.  The key and the element cannot be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * @param key the specified hashtable key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * @param value the specified element
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * @return the old value of the key, or null if it did not have one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * @exception NullPointerException If the value of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * element is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * @see Cache#get
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    public synchronized Object put(Object key, Object value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        // Make sure the value is not null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        if (value == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        // Makes sure the key is not already in the cache.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        CacheEntry tab[] = table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        int hash = key.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        int index = (hash & 0x7FFFFFFF) % tab.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        CacheEntry ne = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        for (CacheEntry e = tab[index]; e != null; e = e.next) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            if ((e.hash == hash) && e.key.equals(key)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                Object old = e.check();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                e.setThing(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                return old;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            } else if (e.check() == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                ne = e;         /* reuse old flushed value */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        if (count >= threshold) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            // Rehash the table if the threshold is exceeded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            rehash();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            return put(key, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        // Creates the new entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        if (ne == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            ne = new CacheEntry ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            ne.next = tab[index];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            tab[index] = ne;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        ne.hash = hash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        ne.key = key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        ne.setThing(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * Removes the element corresponding to the key. Does nothing if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * key is not present.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * @param key the key that needs to be removed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * @return the value of key, or null if the key was not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    public synchronized Object remove(Object key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        CacheEntry tab[] = table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        int hash = key.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        int index = (hash & 0x7FFFFFFF) % tab.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        for (CacheEntry e = tab[index], prev = null; e != null; prev = e, e = e.next) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            if ((e.hash == hash) && e.key.equals(key)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                if (prev != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                    prev.next = e.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                    tab[index] = e.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                count--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                return e.check();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
 * A Cache enumerator class.  This class should remain opaque
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
 * to the client. It will use the Enumeration interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
 */
22123
fac5d9194929 8031369: Fix raw types warnings in sun.misc.{Cache, SoftCache}
darcy
parents: 5506
diff changeset
   310
class CacheEnumerator implements Enumeration<Object> {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    boolean keys;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    int index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    CacheEntry table[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    CacheEntry entry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    CacheEnumerator (CacheEntry table[], boolean keys) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        this.table = table;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        this.keys = keys;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        this.index = table.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    public boolean hasMoreElements() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        while (index >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            while (entry != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                if (entry.check() != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                    entry = entry.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            while (--index >= 0 && (entry = table[index]) == null) ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    public Object nextElement() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        while (index >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            if (entry == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                while (--index >= 0 && (entry = table[index]) == null) ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            if (entry != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                CacheEntry e = entry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                entry = e.next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                if (e.check() != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                    return keys ? e.key : e.check();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        throw new NoSuchElementException("CacheEnumerator");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
}