Coverage Report - org.apache.commons.flatfile.morph.FieldOptionConstantConverter
 
Classes in this File Line Coverage Branch Coverage Complexity
FieldOptionConstantConverter
92%
12/13
75%
3/4
1.6
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  * contributor license agreements.  See the NOTICE file distributed with
 4  
  * this work for additional information regarding copyright ownership.
 5  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 6  
  * (the "License"); you may not use this file except in compliance with
 7  
  * the License.  You may obtain a copy of the License at
 8  
  *
 9  
  *      http://www.apache.org/licenses/LICENSE-2.0
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.apache.commons.flatfile.morph;
 18  
 
 19  
 import java.util.Locale;
 20  
 
 21  
 import org.apache.commons.flatfile.FieldOption;
 22  
 
 23  
 import net.sf.morph.Defaults;
 24  
 import net.sf.morph.transform.DecoratedConverter;
 25  
 import net.sf.morph.transform.transformers.BaseTransformer;
 26  
 
 27  
 /**
 28  
  * Convert text to FieldOption instances from class constants.
 29  
  * @version $Revision: 1301237 $ $Date: 2012-03-15 17:08:23 -0500 (Thu, 15 Mar 2012) $
 30  
  */
 31  
 @SuppressWarnings("unchecked")
 32  6
 public class FieldOptionConstantConverter extends BaseTransformer implements
 33  
         DecoratedConverter {
 34  
     private DecoratedConverter textConverter;
 35  
 
 36  
     /**
 37  
      * {@inheritDoc}
 38  
      */
 39  
     @SuppressWarnings("rawtypes")
 40  
     @Override
 41  
     protected Class[] getDestinationClassesImpl() throws Exception {
 42  6
         return new Class[] { FieldOption.class };
 43  
     }
 44  
 
 45  
     /**
 46  
      * {@inheritDoc}
 47  
      */
 48  
     @SuppressWarnings("rawtypes")
 49  
     @Override
 50  
     protected Class[] getSourceClassesImpl() throws Exception {
 51  6
         return getTextConverter().getSourceClasses();
 52  
     }
 53  
 
 54  
     /**
 55  
      * {@inheritDoc}
 56  
      */
 57  
     @Override
 58  
     protected Object convertImpl(@SuppressWarnings("rawtypes") Class destinationClass, Object source,
 59  
             Locale locale) throws Exception {
 60  272
         String constant = (String) getTextConverter().convert(String.class,
 61  68
                 source, locale);
 62  204
         if (destinationClass.isEnum()) {
 63  204
             return Enum.valueOf(destinationClass, constant);
 64  
         }
 65  
         // return static field
 66  0
         return destinationClass.getField(constant).get(null);
 67  
     }
 68  
 
 69  
     /**
 70  
      * Get the DecoratedConverter textConverter.
 71  
      * @return DecoratedConverter
 72  
      */
 73  
     public synchronized DecoratedConverter getTextConverter() {
 74  210
         if (textConverter == null) {
 75  6
             setTextConverter(Defaults.createTextConverter());
 76  
         }
 77  210
         return textConverter;
 78  
     }
 79  
 
 80  
     /**
 81  
      * Set the DecoratedConverter textConverter.
 82  
      * @param textConverter DecoratedConverter
 83  
      */
 84  
     public synchronized void setTextConverter(DecoratedConverter textConverter) {
 85  6
         this.textConverter = textConverter;
 86  6
     }
 87  
 
 88  
 }