Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/util/PreHashedMap.java
38829 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 <tt>init</tt> method is invoked by the <tt>PreHashedMap</tt>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* <tt>Hasher</tt> program in the <tt>make/tools/Hasher</tt> 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 <tt>rows</tt> 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 rows124* The row array to be initialized125*/126protected abstract void init(Object[] ht);127128@SuppressWarnings("unchecked")129private V toV(Object x) {130return (V)x;131}132133public V get(Object k) {134int h = (k.hashCode() >> shift) & mask;135Object[] a = (Object[])ht[h];136if (a == null) return null;137for (;;) {138if (a[0].equals(k))139return toV(a[1]);140if (a.length < 3)141return null;142a = (Object[])a[2];143}144}145146/**147* @throws UnsupportedOperationException148* If the given key is not part of this map's initial key set149*/150public V put(String k, V v) {151int h = (k.hashCode() >> shift) & mask;152Object[] a = (Object[])ht[h];153if (a == null)154throw new UnsupportedOperationException(k);155for (;;) {156if (a[0].equals(k)) {157V ov = toV(a[1]);158a[1] = v;159return ov;160}161if (a.length < 3)162throw new UnsupportedOperationException(k);163a = (Object[])a[2];164}165}166167public Set<String> keySet() {168return new AbstractSet<String> () {169170public int size() {171return size;172}173174public Iterator<String> iterator() {175return new Iterator<String>() {176private int i = -1;177Object[] a = null;178String cur = null;179180private boolean findNext() {181if (a != null) {182if (a.length == 3) {183a = (Object[])a[2];184cur = (String)a[0];185return true;186}187i++;188a = null;189}190cur = null;191if (i >= rows)192return false;193if (i < 0 || ht[i] == null) {194do {195if (++i >= rows)196return false;197} while (ht[i] == null);198}199a = (Object[])ht[i];200cur = (String)a[0];201return true;202}203204public boolean hasNext() {205if (cur != null)206return true;207return findNext();208}209210public String next() {211if (cur == null) {212if (!findNext())213throw new NoSuchElementException();214}215String s = cur;216cur = null;217return s;218}219220public void remove() {221throw new UnsupportedOperationException();222}223224};225}226};227}228229public Set<Map.Entry<String,V>> entrySet() {230return new AbstractSet<Map.Entry<String,V>> () {231232public int size() {233return size;234}235236public Iterator<Map.Entry<String,V>> iterator() {237return new Iterator<Map.Entry<String,V>>() {238final Iterator<String> i = keySet().iterator();239240public boolean hasNext() {241return i.hasNext();242}243244public Map.Entry<String,V> next() {245return new Map.Entry<String,V>() {246String k = i.next();247public String getKey() { return k; }248public V getValue() { return get(k); }249public int hashCode() {250V v = get(k);251return (k.hashCode()252+ (v == null253? 0254: v.hashCode()));255}256public boolean equals(Object ob) {257if (ob == this)258return true;259if (!(ob instanceof Map.Entry))260return false;261Map.Entry<?,?> that = (Map.Entry<?,?>)ob;262return ((this.getKey() == null263? that.getKey() == null264: this.getKey()265.equals(that.getKey()))266&&267(this.getValue() == null268? that.getValue() == null269: this.getValue()270.equals(that.getValue())));271}272public V setValue(V v) {273throw new UnsupportedOperationException();274}275};276}277278public void remove() {279throw new UnsupportedOperationException();280}281282};283}284};285}286287}288289290