Coverage Report - org.apache.commons.flatfile.morph.TextToByteConverter
 
Classes in this File Line Coverage Branch Coverage Complexity
TextToByteConverter
100%
24/24
68%
15/22
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.Arrays;
 20  
 import java.util.HashSet;
 21  
 import java.util.Locale;
 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: 758023 $ $Date: 2009-03-24 16:09:19 -0500 (Tue, 24 Mar 2009) $
 31  
  */
 32  2
 public class TextToByteConverter extends BaseTransformer implements
 33  
         DecoratedConverter {
 34  
     private DecoratedConverter textConverter;
 35  
 
 36  
     /**
 37  
      * {@inheritDoc}
 38  
      */
 39  
     @SuppressWarnings("unchecked")
 40  
     protected Object convertImpl(Class destinationClass, Object source,
 41  
             Locale locale) throws Exception {
 42  19
         if (isByte(destinationClass)) {
 43  18
             byte[] b = (byte[]) getTextConverter().convert(byte[].class,
 44  
                     source, locale);
 45  18
             return b == null || b.length == 0 ? null : new Byte(b[0]);
 46  
         }
 47  1
         byte[] b = source == null ? null : new byte[] { ((Byte) source)
 48  
                 .byteValue() };
 49  1
         return getTextConverter().convert(destinationClass, b, locale);
 50  
     }
 51  
 
 52  
     /**
 53  
      * {@inheritDoc}
 54  
      */
 55  
     protected Class<?>[] getDestinationClassesImpl() throws Exception {
 56  2
         HashSet<Class<?>> s = new HashSet<Class<?>>();
 57  2
         s.addAll(Arrays.asList((Class<?>[]) getTextConverter().getDestinationClasses()));
 58  2
         s.add(byte.class);
 59  2
         s.add(Byte.class);
 60  2
         return s.toArray(new Class[s.size()]);
 61  
     }
 62  
 
 63  
     /**
 64  
      * {@inheritDoc}
 65  
      */
 66  
     protected Class<?>[] getSourceClassesImpl() throws Exception {
 67  2
         HashSet<Class<?>> s = new HashSet<Class<?>>();
 68  2
         s.addAll(Arrays.asList((Class<?>[]) getTextConverter().getSourceClasses()));
 69  2
         s.add(byte.class);
 70  2
         s.add(Byte.class);
 71  2
         return s.toArray(new Class[s.size()]);
 72  
     }
 73  
 
 74  
     /**
 75  
      * {@inheritDoc}
 76  
      */
 77  
     @SuppressWarnings("unchecked")
 78  
     protected boolean isTransformableImpl(Class destinationType,
 79  
             Class sourceType) throws Exception {
 80  5
         DecoratedConverter cnv = getTextConverter();
 81  5
         return (isByte(sourceType) && ContainerUtils.contains(cnv
 82  
                 .getDestinationClasses(), destinationType))
 83  
                 || (isByte(destinationType) && ContainerUtils.contains(cnv
 84  
                         .getSourceClasses(), sourceType));
 85  
     }
 86  
 
 87  
     /**
 88  
      * Learn whether the given class is Byte or byte.
 89  
      * @param c to check
 90  
      * @return boolean
 91  
      */
 92  
     private boolean isByte(Class<?> c) {
 93  29
         return c == byte.class || c == Byte.class;
 94  
     }
 95  
 
 96  
     /**
 97  
      * Get the DecoratedConverter textConverter.
 98  
      * @return DecoratedConverter
 99  
      */
 100  
     public synchronized DecoratedConverter getTextConverter() {
 101  28
         if (textConverter == null) {
 102  2
             setTextConverter(Defaults.createTextConverter());
 103  
         }
 104  28
         return textConverter;
 105  
     }
 106  
 
 107  
     /**
 108  
      * Set the DecoratedConverter textConverter.
 109  
      * @param textConverter DecoratedConverter
 110  
      */
 111  
     public synchronized void setTextConverter(DecoratedConverter textConverter) {
 112  2
         this.textConverter = textConverter;
 113  2
     }
 114  
 
 115  
 }