Coverage Report - org.apache.commons.flatfile.morph.TextToByteConverter
 
Classes in this File Line Coverage Branch Coverage Complexity
TextToByteConverter
95%
22/23
64%
18/28
2.429
 
 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.lang3.ArrayUtils;
 22  
 
 23  
 import net.sf.morph.Defaults;
 24  
 import net.sf.morph.transform.DecoratedConverter;
 25  
 import net.sf.morph.transform.transformers.BaseTransformer;
 26  
 import net.sf.morph.util.ContainerUtils;
 27  
 
 28  
 /**
 29  
  * Text to byte converter.
 30  
  * @version $Revision: 1301233 $ $Date: 2012-03-15 17:06:02 -0500 (Thu, 15 Mar 2012) $
 31  
  */
 32  12
 public class TextToByteConverter extends BaseTransformer implements
 33  
         DecoratedConverter {
 34  
     private DecoratedConverter textConverter;
 35  
 
 36  
     /**
 37  
      * {@inheritDoc}
 38  
      */
 39  
     @Override
 40  
     protected Object convertImpl(@SuppressWarnings("rawtypes") Class destinationClass, Object source,
 41  
             Locale locale) throws Exception {
 42  114
         if (isByte(destinationClass)) {
 43  144
             byte[] b = (byte[]) getTextConverter().convert(byte[].class,
 44  36
                     source, locale);
 45  108
             return b == null || b.length == 0 ? null : new Byte(b[0]);
 46  
         }
 47  8
         byte[] b = source == null ? null : new byte[] { ((Byte) source)
 48  2
                 .byteValue() };
 49  6
         return getTextConverter().convert(destinationClass, b, locale);
 50  
     }
 51  
 
 52  
     /**
 53  
      * {@inheritDoc}
 54  
      */
 55  
     @Override
 56  
     protected Class<?>[] getDestinationClassesImpl() throws Exception {
 57  16
         return ArrayUtils.addAll(getTextConverter().getDestinationClasses(),
 58  4
             byte.class, Byte.class);
 59  
     }
 60  
 
 61  
     /**
 62  
      * {@inheritDoc}
 63  
      */
 64  
     @Override
 65  
     protected Class<?>[] getSourceClassesImpl() throws Exception {
 66  16
         return ArrayUtils.addAll(getTextConverter().getSourceClasses(),
 67  4
             byte.class, Byte.class);
 68  
     }
 69  
 
 70  
     /**
 71  
      * {@inheritDoc}
 72  
      */
 73  
     @Override
 74  
     protected boolean isTransformableImpl(@SuppressWarnings("rawtypes") Class destinationType,
 75  
             @SuppressWarnings("rawtypes") Class sourceType) throws Exception {
 76  30
         DecoratedConverter cnv = getTextConverter();
 77  38
         return (isByte(sourceType) && ContainerUtils.contains(
 78  0
             cnv.getDestinationClasses(), destinationType))
 79  12
             || (isByte(destinationType) && ContainerUtils.contains(
 80  4
                 cnv.getSourceClasses(), sourceType));
 81  
     }
 82  
 
 83  
     /**
 84  
      * Learn whether the given class is Byte or byte.
 85  
      * @param c to check
 86  
      * @return boolean
 87  
      */
 88  
     private boolean isByte(Class<?> c) {
 89  174
         return byte.class.equals(c) || Byte.class.equals(c);
 90  
     }
 91  
 
 92  
     /**
 93  
      * Get the DecoratedConverter textConverter.
 94  
      * @return DecoratedConverter
 95  
      */
 96  
     public synchronized DecoratedConverter getTextConverter() {
 97  168
         if (textConverter == null) {
 98  12
             setTextConverter(Defaults.createTextConverter());
 99  
         }
 100  168
         return textConverter;
 101  
     }
 102  
 
 103  
     /**
 104  
      * Set the DecoratedConverter textConverter.
 105  
      * @param textConverter DecoratedConverter
 106  
      */
 107  
     public synchronized void setTextConverter(DecoratedConverter textConverter) {
 108  12
         this.textConverter = textConverter;
 109  12
     }
 110  
 
 111  
 }