Coverage Report - net.sourceforge.jeuclid.context.typewrapper.EnumTypeWrapper
 
Classes in this File Line Coverage Branch Coverage Complexity
EnumTypeWrapper
38%
5/13
50%
1/2
3,25
 
 1  
 /*
 2  
  * Copyright 2008 - 2008 JEuclid, http://jeuclid.sf.net
 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  
 /* $Id: EnumTypeWrapper.java,v aa78526a6b7b 2009/06/03 10:50:27 maxberger $ */
 18  
 
 19  
 package net.sourceforge.jeuclid.context.typewrapper;
 20  
 
 21  
 import java.lang.reflect.InvocationTargetException;
 22  
 
 23  
 /**
 24  
  * Converting String to Enum and vice versa is easy with help of Enum class.
 25  
  * 
 26  
  * @version $Revision: aa78526a6b7b $
 27  
  */
 28  
 public final class EnumTypeWrapper extends AbstractSimpleTypeWrapper {
 29  
     private static final String FAILED_TO_RETRIEVE_VALUES_OF_ENUM_CLASS = "Failed to retrieve values of enum class ";
 30  
 
 31  
     /**
 32  
      * 
 33  
      */
 34  
     private static final long serialVersionUID = 1L;
 35  
 
 36  
     /**
 37  
      * Simple constructor.
 38  
      * 
 39  
      * @param valueType
 40  
      *            an enum class
 41  
      */
 42  
     private EnumTypeWrapper(final Class<? extends Enum<?>> valueType) {
 43  209
         super(valueType);
 44  209
     }
 45  
 
 46  
     /**
 47  
      * @return the singleton instance.
 48  
      * @param valueType
 49  
      *            an enum class
 50  
      */
 51  
     public static TypeWrapper getInstance(
 52  
             final Class<? extends Enum<?>> valueType) {
 53  209
         return new EnumTypeWrapper(valueType);
 54  
     }
 55  
 
 56  
     /** {@inheritDoc} */
 57  
     @SuppressWarnings("unchecked")
 58  
     @Override
 59  
     public Object fromString(final String value) {
 60  418
         if (value == null) {
 61  0
             return null;
 62  
         }
 63  418
         return Enum.valueOf((Class) this.getValueType(), value);
 64  
     }
 65  
 
 66  
     /**
 67  
      * Retrieves values of the enum type being wrapped.
 68  
      * 
 69  
      * @return array of possible enum values
 70  
      */
 71  
     public Object[] values() {
 72  
         try {
 73  0
             return (Object[]) this.getValueType().getMethod("values").invoke(
 74  
                     null);
 75  0
         } catch (final InvocationTargetException e) {
 76  0
             throw new RuntimeException(
 77  
                     EnumTypeWrapper.FAILED_TO_RETRIEVE_VALUES_OF_ENUM_CLASS
 78  
                             + this.getValueType(), e);
 79  0
         } catch (final IllegalAccessException e) {
 80  0
             throw new RuntimeException(
 81  
                     EnumTypeWrapper.FAILED_TO_RETRIEVE_VALUES_OF_ENUM_CLASS
 82  
                             + this.getValueType(), e);
 83  0
         } catch (final NoSuchMethodException e) {
 84  0
             throw new RuntimeException(
 85  
                     EnumTypeWrapper.FAILED_TO_RETRIEVE_VALUES_OF_ENUM_CLASS
 86  
                             + this.getValueType(), e);
 87  
         }
 88  
     }
 89  
 }