< prev index next >

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

Print this page




 737     }
 738 
 739     /**
 740      * Throws an exception if e is not of the correct type for this enum set.
 741      */
 742     private void typeCheck(K key) {
 743         Class<?> keyClass = key.getClass();
 744         if (keyClass != keyType && keyClass.getSuperclass() != keyType)
 745             throw new ClassCastException(keyClass + " != " + keyType);
 746     }
 747 
 748     /**
 749      * Returns all of the values comprising K.
 750      * The result is uncloned, cached, and shared by all callers.
 751      */
 752     private static <K extends Enum<K>> K[] getKeyUniverse(Class<K> keyType) {
 753         return SharedSecrets.getJavaLangAccess()
 754                                         .getEnumConstantsShared(keyType);
 755     }
 756 

 757     private static final long serialVersionUID = 458661240069192865L;
 758 
 759     /**
 760      * Save the state of the {@code EnumMap} instance to a stream (i.e.,
 761      * serialize it).
 762      *
 763      * @serialData The <i>size</i> of the enum map (the number of key-value
 764      *             mappings) is emitted (int), followed by the key (Object)
 765      *             and value (Object) for each key-value mapping represented
 766      *             by the enum map.
 767      */

 768     private void writeObject(java.io.ObjectOutputStream s)
 769         throws java.io.IOException
 770     {
 771         // Write out the key type and any hidden stuff
 772         s.defaultWriteObject();
 773 
 774         // Write out size (number of Mappings)
 775         s.writeInt(size);
 776 
 777         // Write out keys and values (alternating)
 778         int entriesToBeWritten = size;
 779         for (int i = 0; entriesToBeWritten > 0; i++) {
 780             if (null != vals[i]) {
 781                 s.writeObject(keyUniverse[i]);
 782                 s.writeObject(unmaskNull(vals[i]));
 783                 entriesToBeWritten--;
 784             }
 785         }
 786     }
 787 
 788     /**
 789      * Reconstitute the {@code EnumMap} instance from a stream (i.e.,
 790      * deserialize it).
 791      */
 792     @SuppressWarnings("unchecked")

 793     private void readObject(java.io.ObjectInputStream s)
 794         throws java.io.IOException, ClassNotFoundException
 795     {
 796         // Read in the key type and any hidden stuff
 797         s.defaultReadObject();
 798 
 799         keyUniverse = getKeyUniverse(keyType);
 800         vals = new Object[keyUniverse.length];
 801 
 802         // Read in size (number of Mappings)
 803         int size = s.readInt();
 804 
 805         // Read the keys and values, and put the mappings in the HashMap
 806         for (int i = 0; i < size; i++) {
 807             K key = (K) s.readObject();
 808             V value = (V) s.readObject();
 809             put(key, value);
 810         }
 811     }
 812 }


 737     }
 738 
 739     /**
 740      * Throws an exception if e is not of the correct type for this enum set.
 741      */
 742     private void typeCheck(K key) {
 743         Class<?> keyClass = key.getClass();
 744         if (keyClass != keyType && keyClass.getSuperclass() != keyType)
 745             throw new ClassCastException(keyClass + " != " + keyType);
 746     }
 747 
 748     /**
 749      * Returns all of the values comprising K.
 750      * The result is uncloned, cached, and shared by all callers.
 751      */
 752     private static <K extends Enum<K>> K[] getKeyUniverse(Class<K> keyType) {
 753         return SharedSecrets.getJavaLangAccess()
 754                                         .getEnumConstantsShared(keyType);
 755     }
 756 
 757     @java.io.Serial
 758     private static final long serialVersionUID = 458661240069192865L;
 759 
 760     /**
 761      * Save the state of the {@code EnumMap} instance to a stream (i.e.,
 762      * serialize it).
 763      *
 764      * @serialData The <i>size</i> of the enum map (the number of key-value
 765      *             mappings) is emitted (int), followed by the key (Object)
 766      *             and value (Object) for each key-value mapping represented
 767      *             by the enum map.
 768      */
 769     @java.io.Serial
 770     private void writeObject(java.io.ObjectOutputStream s)
 771         throws java.io.IOException
 772     {
 773         // Write out the key type and any hidden stuff
 774         s.defaultWriteObject();
 775 
 776         // Write out size (number of Mappings)
 777         s.writeInt(size);
 778 
 779         // Write out keys and values (alternating)
 780         int entriesToBeWritten = size;
 781         for (int i = 0; entriesToBeWritten > 0; i++) {
 782             if (null != vals[i]) {
 783                 s.writeObject(keyUniverse[i]);
 784                 s.writeObject(unmaskNull(vals[i]));
 785                 entriesToBeWritten--;
 786             }
 787         }
 788     }
 789 
 790     /**
 791      * Reconstitute the {@code EnumMap} instance from a stream (i.e.,
 792      * deserialize it).
 793      */
 794     @SuppressWarnings("unchecked")
 795     @java.io.Serial
 796     private void readObject(java.io.ObjectInputStream s)
 797         throws java.io.IOException, ClassNotFoundException
 798     {
 799         // Read in the key type and any hidden stuff
 800         s.defaultReadObject();
 801 
 802         keyUniverse = getKeyUniverse(keyType);
 803         vals = new Object[keyUniverse.length];
 804 
 805         // Read in size (number of Mappings)
 806         int size = s.readInt();
 807 
 808         // Read the keys and values, and put the mappings in the HashMap
 809         for (int i = 0; i < size; i++) {
 810             K key = (K) s.readObject();
 811             V value = (V) s.readObject();
 812             put(key, value);
 813         }
 814     }
 815 }
< prev index next >