< prev index next >

src/java.base/share/classes/java/util/EnumMap.java

Print this page
rev 13463 : 8148044: Remove Enum[0] constants from EnumSet and EnumMap
Reviewed-by: TBD


   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.util;
  27 
  28 import java.util.Map.Entry;
  29 import jdk.internal.misc.SharedSecrets;
  30 
  31 /**
  32  * A specialized {@link Map} implementation for use with enum type keys.  All
  33  * of the keys in an enum map must come from a single enum type that is
  34  * specified, explicitly or implicitly, when the map is created.  Enum maps
  35  * are represented internally as arrays.  This representation is extremely
  36  * compact and efficient.
  37  *
  38  * <p>Enum maps are maintained in the <i>natural order</i> of their keys
  39  * (the order in which the enum constants are declared).  This is reflected
  40  * in the iterators returned by the collections views ({@link #keySet()},
  41  * {@link #entrySet()}, and {@link #values()}).
  42  *
  43  * <p>Iterators returned by the collection views are <i>weakly consistent</i>:
  44  * they will never throw {@link ConcurrentModificationException} and they may
  45  * or may not show the effects of any modifications to the map that occur while
  46  * the iteration is in progress.
  47  *
  48  * <p>Null keys are not permitted.  Attempts to insert a null key will


 107      * Distinguished non-null value for representing null values.
 108      */
 109     private static final Object NULL = new Object() {
 110         public int hashCode() {
 111             return 0;
 112         }
 113 
 114         public String toString() {
 115             return "java.util.EnumMap.NULL";
 116         }
 117     };
 118 
 119     private Object maskNull(Object value) {
 120         return (value == null ? NULL : value);
 121     }
 122 
 123     @SuppressWarnings("unchecked")
 124     private V unmaskNull(Object value) {
 125         return (V)(value == NULL ? null : value);
 126     }
 127 
 128     private static final Enum<?>[] ZERO_LENGTH_ENUM_ARRAY = new Enum<?>[0];
 129 
 130     /**
 131      * Creates an empty enum map with the specified key type.
 132      *
 133      * @param keyType the class object of the key type for this enum map
 134      * @throws NullPointerException if {@code keyType} is null
 135      */
 136     public EnumMap(Class<K> keyType) {
 137         this.keyType = keyType;
 138         keyUniverse = getKeyUniverse(keyType);
 139         vals = new Object[keyUniverse.length];
 140     }
 141 
 142     /**
 143      * Creates an enum map with the same key type as the specified enum
 144      * map, initially containing the same mappings (if any).
 145      *
 146      * @param m the enum map from which to initialize this enum map
 147      * @throws NullPointerException if {@code m} is null
 148      */




   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.util;
  27 

  28 import jdk.internal.misc.SharedSecrets;
  29 
  30 /**
  31  * A specialized {@link Map} implementation for use with enum type keys.  All
  32  * of the keys in an enum map must come from a single enum type that is
  33  * specified, explicitly or implicitly, when the map is created.  Enum maps
  34  * are represented internally as arrays.  This representation is extremely
  35  * compact and efficient.
  36  *
  37  * <p>Enum maps are maintained in the <i>natural order</i> of their keys
  38  * (the order in which the enum constants are declared).  This is reflected
  39  * in the iterators returned by the collections views ({@link #keySet()},
  40  * {@link #entrySet()}, and {@link #values()}).
  41  *
  42  * <p>Iterators returned by the collection views are <i>weakly consistent</i>:
  43  * they will never throw {@link ConcurrentModificationException} and they may
  44  * or may not show the effects of any modifications to the map that occur while
  45  * the iteration is in progress.
  46  *
  47  * <p>Null keys are not permitted.  Attempts to insert a null key will


 106      * Distinguished non-null value for representing null values.
 107      */
 108     private static final Object NULL = new Object() {
 109         public int hashCode() {
 110             return 0;
 111         }
 112 
 113         public String toString() {
 114             return "java.util.EnumMap.NULL";
 115         }
 116     };
 117 
 118     private Object maskNull(Object value) {
 119         return (value == null ? NULL : value);
 120     }
 121 
 122     @SuppressWarnings("unchecked")
 123     private V unmaskNull(Object value) {
 124         return (V)(value == NULL ? null : value);
 125     }


 126 
 127     /**
 128      * Creates an empty enum map with the specified key type.
 129      *
 130      * @param keyType the class object of the key type for this enum map
 131      * @throws NullPointerException if {@code keyType} is null
 132      */
 133     public EnumMap(Class<K> keyType) {
 134         this.keyType = keyType;
 135         keyUniverse = getKeyUniverse(keyType);
 136         vals = new Object[keyUniverse.length];
 137     }
 138 
 139     /**
 140      * Creates an enum map with the same key type as the specified enum
 141      * map, initially containing the same mappings (if any).
 142      *
 143      * @param m the enum map from which to initialize this enum map
 144      * @throws NullPointerException if {@code m} is null
 145      */


< prev index next >