Coverage Report - org.dozer.converters.EnumConverter
 
Classes in this File Line Coverage Branch Coverage Complexity
EnumConverter
86%
13/15
72%
13/18
16
 
 1  
 /**
 2  
  * Copyright 2005-2013 Dozer Project
 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  
 package org.dozer.converters;
 17  
 
 18  
 import org.apache.commons.beanutils.Converter;
 19  
 import org.apache.commons.lang3.EnumUtils;
 20  
 import org.dozer.util.MappingUtils;
 21  
 
 22  
 /**
 23  
  * Internal class for converting Supported Data Types --> Enum. Only intended for internal use.
 24  
  * 
 25  
  * @author siarhei.krukau
 26  
  */
 27  38
 public class EnumConverter implements Converter {
 28  
 
 29  
   @SuppressWarnings({ "unchecked", "rawtypes" })
 30  
   public Object convert(Class destClass, Object srcObj) {
 31  38
     if (null == srcObj) {
 32  0
       MappingUtils.throwMappingException("Cannot convert null to enum of type " + destClass);
 33  
     }
 34  
 
 35  
     try {
 36  38
       if ((srcObj.getClass().equals(Byte.class)) || (srcObj.getClass().equals(Byte.TYPE))) {
 37  8
         return EnumUtils.getEnumList(destClass).get(((Byte) srcObj).intValue());
 38  30
       } else if ((srcObj.getClass().equals(Short.class)) || (srcObj.getClass().equals(Short.TYPE))) {
 39  8
         return EnumUtils.getEnumList(destClass).get(((Short) srcObj).intValue());
 40  22
       } else if ((srcObj.getClass().equals(Integer.class)) || (srcObj.getClass().equals(Integer.TYPE))) {
 41  8
         return EnumUtils.getEnumList(destClass).get((Integer) srcObj);
 42  14
       } else if ((srcObj.getClass().equals(Long.class)) || (srcObj.getClass().equals(Long.TYPE))) {
 43  8
         return EnumUtils.getEnumList(destClass).get(((Long) srcObj).intValue());
 44  
       } else {
 45  6
         return Enum.valueOf(destClass, srcObj.toString());
 46  
       }
 47  10
     } catch (Exception e) {
 48  10
       MappingUtils.throwMappingException("Cannot convert [" + srcObj + "] to enum of type " + destClass, e);
 49  
     }
 50  0
     return srcObj;
 51  
   }
 52  
 }