Home | History | Annotate | Download | only in base
      1 /*
      2  * Copyright (C) 2011 The Guava Authors
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  * http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.google.common.base;
     18 
     19 import static com.google.common.base.Preconditions.checkNotNull;
     20 
     21 import com.google.common.annotations.Beta;
     22 import com.google.common.annotations.GwtCompatible;
     23 
     24 import java.io.Serializable;
     25 
     26 import javax.annotation.Nullable;
     27 
     28 /**
     29  * Utility methods for working with {@link Enum} instances.
     30  *
     31  * @author Steve McKay
     32  *
     33  * @since 9.0
     34  */
     35 @GwtCompatible
     36 @Beta
     37 public final class Enums {
     38 
     39   private Enums() {}
     40 
     41   /**
     42    * Returns a {@link Function} that maps an {@link Enum} name to the associated
     43    * {@code Enum} constant. The {@code Function} will return {@code null} if the
     44    * {@code Enum} constant does not exist.
     45    *
     46    * @param enumClass the {@link Class} of the {@code Enum} declaring the
     47    *     constant values.
     48    */
     49   public static <T extends Enum<T>> Function<String, T> valueOfFunction(Class<T> enumClass) {
     50     return new ValueOfFunction<T>(enumClass);
     51   }
     52 
     53   /**
     54    * {@link Function} that maps an {@link Enum} name to the associated
     55    * constant, or {@code null} if the constant does not exist.
     56    */
     57   private static final class ValueOfFunction<T extends Enum<T>> implements
     58       Function<String, T>, Serializable {
     59 
     60     private final Class<T> enumClass;
     61 
     62     private ValueOfFunction(Class<T> enumClass) {
     63       this.enumClass = checkNotNull(enumClass);
     64     }
     65 
     66     @Override
     67     public T apply(String value) {
     68       try {
     69         return Enum.valueOf(enumClass, value);
     70       } catch (IllegalArgumentException e) {
     71         return null;
     72       }
     73     }
     74 
     75     @Override public boolean equals(@Nullable Object obj) {
     76       return obj instanceof ValueOfFunction &&
     77           enumClass.equals(((ValueOfFunction) obj).enumClass);
     78     }
     79 
     80     @Override public int hashCode() {
     81       return enumClass.hashCode();
     82     }
     83 
     84     @Override public String toString() {
     85       return "Enums.valueOf(" + enumClass + ")";
     86     }
     87 
     88     private static final long serialVersionUID = 0;
     89   }
     90 }
     91