jdk/src/share/classes/java/util/IdentityHashMap.java
changeset 9235 ddd556c97e6c
parent 7803 56bc97d69d93
child 9275 1df1f7dfab7f
equal deleted inserted replaced
9029:e92fcf58f684 9235:ddd556c97e6c
     1 /*
     1 /*
     2  * Copyright (c) 2000, 2008, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
   827         public V next() {
   827         public V next() {
   828             return (V) traversalTable[nextIndex() + 1];
   828             return (V) traversalTable[nextIndex() + 1];
   829         }
   829         }
   830     }
   830     }
   831 
   831 
   832     /**
       
   833      * Since we don't use Entry objects, we use the Iterator
       
   834      * itself as an entry.
       
   835      */
       
   836     private class EntryIterator
   832     private class EntryIterator
   837         extends IdentityHashMapIterator<Map.Entry<K,V>>
   833         extends IdentityHashMapIterator<Map.Entry<K,V>>
   838         implements Map.Entry<K,V>
       
   839     {
   834     {
       
   835         private Entry lastReturnedEntry = null;
       
   836 
   840         public Map.Entry<K,V> next() {
   837         public Map.Entry<K,V> next() {
   841             nextIndex();
   838             lastReturnedEntry = new Entry(nextIndex());
   842             return this;
   839             return lastReturnedEntry;
   843         }
   840         }
   844 
   841 
   845         public K getKey() {
   842         public void remove() {
   846             // Provide a better exception than out of bounds index
   843             lastReturnedIndex =
   847             if (lastReturnedIndex < 0)
   844                 ((null == lastReturnedEntry) ? -1 : lastReturnedEntry.index);
   848                 throw new IllegalStateException("Entry was removed");
   845             super.remove();
   849 
   846             lastReturnedEntry.index = lastReturnedIndex;
   850             return (K) unmaskNull(traversalTable[lastReturnedIndex]);
   847             lastReturnedEntry = null;
   851         }
   848         }
   852 
   849 
   853         public V getValue() {
   850         private class Entry implements Map.Entry<K,V> {
   854             // Provide a better exception than out of bounds index
   851             private int index;
   855             if (lastReturnedIndex < 0)
   852 
   856                 throw new IllegalStateException("Entry was removed");
   853             private Entry(int index) {
   857 
   854                 this.index = index;
   858             return (V) traversalTable[lastReturnedIndex+1];
   855             }
   859         }
   856 
   860 
   857             public K getKey() {
   861         public V setValue(V value) {
   858                 checkIndexForEntryUse();
   862             // It would be mean-spirited to proceed here if remove() called
   859                 return (K) unmaskNull(traversalTable[index]);
   863             if (lastReturnedIndex < 0)
   860             }
   864                 throw new IllegalStateException("Entry was removed");
   861 
   865             V oldValue = (V) traversalTable[lastReturnedIndex+1];
   862             public V getValue() {
   866             traversalTable[lastReturnedIndex+1] = value;
   863                 checkIndexForEntryUse();
   867             // if shadowing, force into main table
   864                 return (V) traversalTable[index+1];
   868             if (traversalTable != IdentityHashMap.this.table)
   865             }
   869                 put((K) traversalTable[lastReturnedIndex], value);
   866 
   870             return oldValue;
   867             public V setValue(V value) {
   871         }
   868                 checkIndexForEntryUse();
   872 
   869                 V oldValue = (V) traversalTable[index+1];
   873         public boolean equals(Object o) {
   870                 traversalTable[index+1] = value;
   874             if (lastReturnedIndex < 0)
   871                 // if shadowing, force into main table
   875                 return super.equals(o);
   872                 if (traversalTable != IdentityHashMap.this.table)
   876 
   873                     put((K) traversalTable[index], value);
   877             if (!(o instanceof Map.Entry))
   874                 return oldValue;
   878                 return false;
   875             }
   879             Map.Entry e = (Map.Entry)o;
   876 
   880             return e.getKey()   == getKey() &&
   877             public boolean equals(Object o) {
   881                    e.getValue() == getValue();
   878                 if (index < 0)
   882         }
   879                     return super.equals(o);
   883 
   880 
   884         public int hashCode() {
   881                 if (!(o instanceof Map.Entry))
   885             if (lastReturnedIndex < 0)
   882                     return false;
   886                 return super.hashCode();
   883                 Map.Entry e = (Map.Entry)o;
   887 
   884                 return (e.getKey() == unmaskNull(traversalTable[index]) &&
   888             return System.identityHashCode(getKey()) ^
   885                        e.getValue() == traversalTable[index+1]);
   889                    System.identityHashCode(getValue());
   886             }
   890         }
   887 
   891 
   888             public int hashCode() {
   892         public String toString() {
   889                 if (lastReturnedIndex < 0)
   893             if (lastReturnedIndex < 0)
   890                     return super.hashCode();
   894                 return super.toString();
   891 
   895 
   892                 return (System.identityHashCode(unmaskNull(traversalTable[index])) ^
   896             return getKey() + "=" + getValue();
   893                        System.identityHashCode(traversalTable[index+1]));
       
   894             }
       
   895 
       
   896             public String toString() {
       
   897                 if (index < 0)
       
   898                     return super.toString();
       
   899 
       
   900                 return (unmaskNull(traversalTable[index]) + "="
       
   901                         + traversalTable[index+1]);
       
   902             }
       
   903 
       
   904             private void checkIndexForEntryUse() {
       
   905                 if (index < 0)
       
   906                     throw new IllegalStateException("Entry was removed");
       
   907             }
   897         }
   908         }
   898     }
   909     }
   899 
   910 
   900     // Views
   911     // Views
   901 
   912