Path: blob/master/src/java.base/share/classes/sun/util/PreHashedMap.java
41152 views
/*1* Copyright (c) 2004, 2012, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.util;2627import java.util.Iterator;28import java.util.Map;29import java.util.Set;30import java.util.AbstractMap;31import java.util.AbstractSet;32import java.util.NoSuchElementException;333435/**36* A precomputed hash map.37*38* <p> Subclasses of this class are of the following form:39*40* <blockquote><pre>41* class FooMap42* extends sun.util.PreHashedMap<String>43* {44*45* private FooMap() {46* super(ROWS, SIZE, SHIFT, MASK);47* }48*49* protected void init(Object[] ht) {50* ht[0] = new Object[] { "key-1", value_1 };51* ht[1] = new Object[] { "key-2", value_2,52* new Object { "key-3", value_3 } };53* ...54* }55*56* }</pre></blockquote>57*58* <p> The {@code init} method is invoked by the {@code PreHashedMap}59* constructor with an object array long enough for the map's rows. The method60* must construct the hash chain for each row and store it in the appropriate61* element of the array.62*63* <p> Each entry in the map is represented by a unique hash-chain node. The64* final node of a hash chain is a two-element object array whose first element65* is the entry's key and whose second element is the entry's value. A66* non-final node of a hash chain is a three-element object array whose first67* two elements are the entry's key and value and whose third element is the68* next node in the chain.69*70* <p> Instances of this class are mutable and are not safe for concurrent71* access. They may be made immutable and thread-safe via the appropriate72* methods in the {@link java.util.Collections} utility class.73*74* <p> In the JDK build, subclasses of this class are typically created via the75* {@code Hasher} program in the {@code make/tools/Hasher} directory.76*77* @author Mark Reinhold78* @since 1.579*80* @see java.util.AbstractMap81*/8283public abstract class PreHashedMap<V>84extends AbstractMap<String,V>85{8687private final int rows;88private final int size;89private final int shift;90private final int mask;91private final Object[] ht;9293/**94* Creates a new map.95*96* <p> This constructor invokes the {@link #init init} method, passing it a97* newly-constructed row array that is {@code rows} elements long.98*99* @param rows100* The number of rows in the map101* @param size102* The number of entries in the map103* @param shift104* The value by which hash codes are right-shifted105* @param mask106* The value with which hash codes are masked after being shifted107*/108protected PreHashedMap(int rows, int size, int shift, int mask) {109this.rows = rows;110this.size = size;111this.shift = shift;112this.mask = mask;113this.ht = new Object[rows];114init(ht);115}116117/**118* Initializes this map.119*120* <p> This method must construct the map's hash chains and store them into121* the appropriate elements of the given hash-table row array.122*123* @param ht The row array to be initialized124*/125protected abstract void init(Object[] ht);126127@SuppressWarnings("unchecked")128private V toV(Object x) {129return (V)x;130}131132public V get(Object k) {133int h = (k.hashCode() >> shift) & mask;134Object[] a = (Object[])ht[h];135if (a == null) return null;136for (;;) {137if (a[0].equals(k))138return toV(a[1]);139if (a.length < 3)140return null;141a = (Object[])a[2];142}143}144145/**146* @throws UnsupportedOperationException147* If the given key is not part of this map's initial key set148*/149public V put(String k, V v) {150int h = (k.hashCode() >> shift) & mask;151Object[] a = (Object[])ht[h];152if (a == null)153throw new UnsupportedOperationException(k);154for (;;) {155if (a[0].equals(k)) {156V ov = toV(a[1]);157a[1] = v;158return ov;159}160if (a.length < 3)161throw new UnsupportedOperationException(k);162a = (Object[])a[2];163}164}165166public Set<String> keySet() {167return new AbstractSet<> () {168169public int size() {170return size;171}172173public Iterator<String> iterator() {174return new Iterator<>() {175private int i = -1;176Object[] a = null;177String cur = null;178179private boolean findNext() {180if (a != null) {181if (a.length == 3) {182a = (Object[])a[2];183cur = (String)a[0];184return true;185}186i++;187a = null;188}189cur = null;190if (i >= rows)191return false;192if (i < 0 || ht[i] == null) {193do {194if (++i >= rows)195return false;196} while (ht[i] == null);197}198a = (Object[])ht[i];199cur = (String)a[0];200return true;201}202203public boolean hasNext() {204if (cur != null)205return true;206return findNext();207}208209public String next() {210if (cur == null) {211if (!findNext())212throw new NoSuchElementException();213}214String s = cur;215cur = null;216return s;217}218219public void remove() {220throw new UnsupportedOperationException();221}222223};224}225};226}227228public Set<Map.Entry<String,V>> entrySet() {229return new AbstractSet<Map.Entry<String,V>> () {230231public int size() {232return size;233}234235public Iterator<Map.Entry<String,V>> iterator() {236return new Iterator<Map.Entry<String,V>>() {237final Iterator<String> i = keySet().iterator();238239public boolean hasNext() {240return i.hasNext();241}242243public Map.Entry<String,V> next() {244return new Map.Entry<String,V>() {245String k = i.next();246public String getKey() { return k; }247public V getValue() { return get(k); }248public int hashCode() {249V v = get(k);250return (k.hashCode()251+ (v == null252? 0253: v.hashCode()));254}255public boolean equals(Object ob) {256if (ob == this)257return true;258if (!(ob instanceof Map.Entry))259return false;260Map.Entry<?,?> that = (Map.Entry<?,?>)ob;261return ((this.getKey() == null262? that.getKey() == null263: this.getKey()264.equals(that.getKey()))265&&266(this.getValue() == null267? that.getValue() == null268: this.getValue()269.equals(that.getValue())));270}271public V setValue(V v) {272throw new UnsupportedOperationException();273}274};275}276277public void remove() {278throw new UnsupportedOperationException();279}280281};282}283};284}285286}287288289