< prev index next >

src/java.base/share/classes/java/lang/Class.java

Print this page
rev 49518 : [mq]: 8200696-Optimal-initial-capacity-of-java-lang-Class-enumConstantDirectory
   1 /*
   2  * Copyright (c) 1994, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   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
   7  * published by the Free Software Foundation.  Oracle designates this
   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


3512                    IllegalAccessException ex) { return null; }
3513         }
3514         return constants;
3515     }
3516     private transient volatile T[] enumConstants;
3517 
3518     /**
3519      * Returns a map from simple name to enum constant.  This package-private
3520      * method is used internally by Enum to implement
3521      * {@code public static <T extends Enum<T>> T valueOf(Class<T>, String)}
3522      * efficiently.  Note that the map is returned by this method is
3523      * created lazily on first use.  Typically it won't ever get created.
3524      */
3525     Map<String, T> enumConstantDirectory() {
3526         Map<String, T> directory = enumConstantDirectory;
3527         if (directory == null) {
3528             T[] universe = getEnumConstantsShared();
3529             if (universe == null)
3530                 throw new IllegalArgumentException(
3531                     getName() + " is not an enum type");
3532             directory = new HashMap<>(2 * universe.length);
3533             for (T constant : universe) {
3534                 directory.put(((Enum<?>)constant).name(), constant);
3535             }
3536             enumConstantDirectory = directory;
3537         }
3538         return directory;
3539     }
3540     private transient volatile Map<String, T> enumConstantDirectory;
3541 
3542     /**
3543      * Casts an object to the class or interface represented
3544      * by this {@code Class} object.
3545      *
3546      * @param obj the object to be cast
3547      * @return the object after casting, or null if obj is null
3548      *
3549      * @throws ClassCastException if the object is not
3550      * null and is not assignable to the type T.
3551      *
3552      * @since 1.5


   1 /*
   2  * Copyright (c) 1994, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   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
   7  * published by the Free Software Foundation.  Oracle designates this
   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


3512                    IllegalAccessException ex) { return null; }
3513         }
3514         return constants;
3515     }
3516     private transient volatile T[] enumConstants;
3517 
3518     /**
3519      * Returns a map from simple name to enum constant.  This package-private
3520      * method is used internally by Enum to implement
3521      * {@code public static <T extends Enum<T>> T valueOf(Class<T>, String)}
3522      * efficiently.  Note that the map is returned by this method is
3523      * created lazily on first use.  Typically it won't ever get created.
3524      */
3525     Map<String, T> enumConstantDirectory() {
3526         Map<String, T> directory = enumConstantDirectory;
3527         if (directory == null) {
3528             T[] universe = getEnumConstantsShared();
3529             if (universe == null)
3530                 throw new IllegalArgumentException(
3531                     getName() + " is not an enum type");
3532             directory = new HashMap<>((int)(universe.length / 0.75f + 1.0f));
3533             for (T constant : universe) {
3534                 directory.put(((Enum<?>)constant).name(), constant);
3535             }
3536             enumConstantDirectory = directory;
3537         }
3538         return directory;
3539     }
3540     private transient volatile Map<String, T> enumConstantDirectory;
3541 
3542     /**
3543      * Casts an object to the class or interface represented
3544      * by this {@code Class} object.
3545      *
3546      * @param obj the object to be cast
3547      * @return the object after casting, or null if obj is null
3548      *
3549      * @throws ClassCastException if the object is not
3550      * null and is not assignable to the type T.
3551      *
3552      * @since 1.5


< prev index next >