(Class<T> enumClass, String value)
| 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); |
no test coverage detected