MCPcopy
hub / github.com/google/guava / getEnumIfPresent

Method getEnumIfPresent

guava/src/com/google/common/base/Platform.java:39–52  ·  view source on GitHub ↗
(Class<T> enumClass, String value)

Source from the content-addressed store, hash-verified

37 }
38
39 static <T extends Enum<T>> Optional<T> getEnumIfPresent(Class<T> enumClass, String value) {
40 WeakReference<? extends Enum<?>> ref = Enums.getEnumConstants(enumClass).get(value);
41 /*
42 * We use `fromNullable` instead of `of` because `WeakReference.get()` has a nullable return
43 * type.
44 *
45 * In practice, we are very unlikely to see `null`: The `WeakReference` to the enum constant
46 * won't be cleared as long as the enum constant is referenced somewhere, and the enum constant
47 * is referenced somewhere for as long as the enum class is loaded. *Maybe in theory* the enum
48 * class could be unloaded after the above call to `getEnumConstants` but before we call
49 * `get()`, but that is vanishingly unlikely.
50 */
51 return ref == null ? Optional.absent() : Optional.fromNullable(enumClass.cast(ref.get()));
52 }
53
54 static String formatCompact4Digits(double value) {
55 return String.format(Locale.ROOT, "%.4g", value);

Callers 1

getIfPresentMethod · 0.95

Calls 5

getEnumConstantsMethod · 0.95
absentMethod · 0.95
fromNullableMethod · 0.95
getMethod · 0.65
castMethod · 0.45

Tested by

no test coverage detected