Coverage Report - org.apache.commons.flatfile.morph.FieldOptionConstantConverter
 
Classes in this File Line Coverage Branch Coverage Complexity
FieldOptionConstantConverter
92%
11/12
75%
3/4
0
 
 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: 757266 $ $Date: 2009-03-22 17:12:27 -0500 (Sun, 22 Mar 2009) $
 30  
  */
 31  
 @SuppressWarnings("unchecked")
 32  1
 public class FieldOptionConstantConverter extends BaseTransformer implements
 33  
         DecoratedConverter {
 34  
     private DecoratedConverter textConverter;
 35  
 
 36  
     /**
 37  
      * {@inheritDoc}
 38  
      */
 39  
     protected Class[] getDestinationClassesImpl() throws Exception {
 40  1
         return new Class[] { FieldOption.class };
 41  
     }
 42  
 
 43  
     /**
 44  
      * {@inheritDoc}
 45  
      */
 46  
     protected Class[] getSourceClassesImpl() throws Exception {
 47  1
         return getTextConverter().getSourceClasses();
 48  
     }
 49  
 
 50  
     /**
 51  
      * {@inheritDoc}
 52  
      */
 53  
     protected Object convertImpl(Class destinationClass, Object source,
 54  
             Locale locale) throws Exception {
 55  34
         String constant = (String) getTextConverter().convert(String.class,
 56  
                 source, locale);
 57  34
         if (destinationClass.isEnum()) {
 58  34
             return Enum.valueOf(destinationClass, constant);
 59  
         }
 60  
         // return static field
 61  0
         return destinationClass.getField(constant).get(null);
 62  
     }
 63  
 
 64  
     /**
 65  
      * Get the DecoratedConverter textConverter.
 66  
      * @return DecoratedConverter
 67  
      */
 68  
     public synchronized DecoratedConverter getTextConverter() {
 69  35
         if (textConverter == null) {
 70  1
             setTextConverter(Defaults.createTextConverter());
 71  
         }
 72  35
         return textConverter;
 73  
     }
 74  
 
 75  
     /**
 76  
      * Set the DecoratedConverter textConverter.
 77  
      * @param textConverter DecoratedConverter
 78  
      */
 79  
     public synchronized void setTextConverter(DecoratedConverter textConverter) {
 80  1
         this.textConverter = textConverter;
 81  1
     }
 82  
 
 83  
 }