Coverage Summary for Class: Enums (com.google.common.base)
| Class | Method, % | Line, % |
|---|---|---|
| Enums | 0% (0/7) | 0% (0/22) |
| Enums$StringConverter | 0% (0/6) | 0% (0/10) |
| Total | 0% (0/13) | 0% (0/32) |
1 /* 2 * Copyright (C) 2011 The Guava Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 5 * in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the License 10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 11 * or implied. See the License for the specific language governing permissions and limitations under 12 * the License. 13 */ 14 15 package com.google.common.base; 16 17 import static com.google.common.base.Preconditions.checkNotNull; 18 19 import com.google.common.annotations.GwtCompatible; 20 import com.google.common.annotations.GwtIncompatible; 21 import java.io.Serializable; 22 import java.lang.ref.WeakReference; 23 import java.lang.reflect.Field; 24 import java.util.EnumSet; 25 import java.util.HashMap; 26 import java.util.Map; 27 import java.util.WeakHashMap; 28 import javax.annotation.CheckForNull; 29 30 /** 31 * Utility methods for working with {@link Enum} instances. 32 * 33 * @author Steve McKay 34 * @since 9.0 35 */ 36 @GwtCompatible(emulated = true) 37 @ElementTypesAreNonnullByDefault 38 public final class Enums { 39 40 private Enums() {} 41 42 /** 43 * Returns the {@link Field} in which {@code enumValue} is defined. For example, to get the {@code 44 * Description} annotation on the {@code GOLF} constant of enum {@code Sport}, use {@code 45 * Enums.getField(Sport.GOLF).getAnnotation(Description.class)}. 46 * 47 * @since 12.0 48 */ 49 @GwtIncompatible // reflection 50 public static Field getField(Enum<?> enumValue) { 51 Class<?> clazz = enumValue.getDeclaringClass(); 52 try { 53 return clazz.getDeclaredField(enumValue.name()); 54 } catch (NoSuchFieldException impossible) { 55 throw new AssertionError(impossible); 56 } 57 } 58 59 /** 60 * Returns an optional enum constant for the given type, using {@link Enum#valueOf}. If the 61 * constant does not exist, {@link Optional#absent} is returned. A common use case is for parsing 62 * user input or falling back to a default enum constant. For example, {@code 63 * Enums.getIfPresent(Country.class, countryInput).or(Country.DEFAULT);} 64 * 65 * @since 12.0 66 */ 67 public static <T extends Enum<T>> Optional<T> getIfPresent(Class<T> enumClass, String value) { 68 checkNotNull(enumClass); 69 checkNotNull(value); 70 return Platform.getEnumIfPresent(enumClass, value); 71 } 72 73 @GwtIncompatible // java.lang.ref.WeakReference 74 private static final Map<Class<? extends Enum<?>>, Map<String, WeakReference<? extends Enum<?>>>> 75 enumConstantCache = new WeakHashMap<>(); 76 77 @GwtIncompatible // java.lang.ref.WeakReference 78 private static <T extends Enum<T>> Map<String, WeakReference<? extends Enum<?>>> populateCache( 79 Class<T> enumClass) { 80 Map<String, WeakReference<? extends Enum<?>>> result = new HashMap<>(); 81 for (T enumInstance : EnumSet.allOf(enumClass)) { 82 result.put(enumInstance.name(), new WeakReference<Enum<?>>(enumInstance)); 83 } 84 enumConstantCache.put(enumClass, result); 85 return result; 86 } 87 88 @GwtIncompatible // java.lang.ref.WeakReference 89 static <T extends Enum<T>> Map<String, WeakReference<? extends Enum<?>>> getEnumConstants( 90 Class<T> enumClass) { 91 synchronized (enumConstantCache) { 92 Map<String, WeakReference<? extends Enum<?>>> constants = enumConstantCache.get(enumClass); 93 if (constants == null) { 94 constants = populateCache(enumClass); 95 } 96 return constants; 97 } 98 } 99 100 /** 101 * Returns a converter that converts between strings and {@code enum} values of type {@code 102 * enumClass} using {@link Enum#valueOf(Class, String)} and {@link Enum#name()}. The converter 103 * will throw an {@code IllegalArgumentException} if the argument is not the name of any enum 104 * constant in the specified enum. 105 * 106 * @since 16.0 107 */ 108 public static <T extends Enum<T>> Converter<String, T> stringConverter(final Class<T> enumClass) { 109 return new StringConverter<T>(enumClass); 110 } 111 112 private static final class StringConverter<T extends Enum<T>> extends Converter<String, T> 113 implements Serializable { 114 115 private final Class<T> enumClass; 116 117 StringConverter(Class<T> enumClass) { 118 this.enumClass = checkNotNull(enumClass); 119 } 120 121 @Override 122 protected T doForward(String value) { 123 return Enum.valueOf(enumClass, value); 124 } 125 126 @Override 127 protected String doBackward(T enumValue) { 128 return enumValue.name(); 129 } 130 131 @Override 132 public boolean equals(@CheckForNull Object object) { 133 if (object instanceof StringConverter) { 134 StringConverter<?> that = (StringConverter<?>) object; 135 return this.enumClass.equals(that.enumClass); 136 } 137 return false; 138 } 139 140 @Override 141 public int hashCode() { 142 return enumClass.hashCode(); 143 } 144 145 @Override 146 public String toString() { 147 return "Enums.stringConverter(" + enumClass.getName() + ".class)"; 148 } 149 150 private static final long serialVersionUID = 0L; 151 } 152 }