Coverage Report - org.apache.commons.convert.conversion.AbstractConversion
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractConversion
0%
0/23
0%
0/8
2
 
 1  
 /*
 2  
  *  Copyright 2004 The Apache Software Foundation
 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.apache.commons.convert.conversion;
 17  
 
 18  
 import org.apache.commons.convert.Conversion;
 19  
 import org.apache.commons.convert.Converter;
 20  
 
 21  
 /**
 22  
  * Abstract base class for conversion objects that provides basic features.
 23  
  * <p>
 24  
  * Most Conversion implementations will extend this class. It provides convenient
 25  
  * implementations of the methods of the interface, and simplifies null-handling
 26  
  * in the subclass.
 27  
  * <p>
 28  
  * To implement a conversion from a single class, simply extend this class and
 29  
  * implement the {@link #convertValue(Object, Converter)} method. To implement a 
 30  
  * conversion from a class and its subclasses, use {@link AbstractConversionFactory}.
 31  
  *
 32  
  * @author Stephen Colebourne
 33  
  * @version $Id: AbstractConversion.java 155441 2005-02-26 13:19:22Z dirkv $
 34  
  * @since 1.0
 35  
  */
 36  
 public abstract class AbstractConversion implements Conversion {
 37  
 
 38  
     /** Useful constant for subclass constructors */
 39  0
     protected static final Class STRING_CLASS = String.class;
 40  
 
 41  
     /** The type to convert from */
 42  
     private final Class fromType;
 43  
     /** The type to convert to */
 44  
     private final Class toType;
 45  
 
 46  
     /**
 47  
      * Constructor that stores the from and to types.
 48  
      * 
 49  
      * @param fromType  the type to convert from
 50  
      * @param toType  the type to convert to
 51  
      */
 52  
     protected AbstractConversion(Class fromType, Class toType) {
 53  0
         super();
 54  0
         this.fromType = fromType;
 55  0
         this.toType = toType;
 56  0
     }
 57  
 
 58  
     //-----------------------------------------------------------------------
 59  
     /**
 60  
      * Converts an object from one type to another.
 61  
      * <p>
 62  
      * This implementation delegates to <code>convertValue</code> after handling null.
 63  
      * If the null-safe behaviour is undesired, override this method.
 64  
      *
 65  
      * @param value  the input value to be converted, may be null
 66  
      * @param converter  the converter being used, not null
 67  
      * @return the converted value
 68  
      * @throws Exception if conversion fails, use ConversionException if creating
 69  
      *  a new exception, otherwise just allow exceptions to be thrown
 70  
      */
 71  
     public Object convert(Object value, Converter converter) throws Exception {
 72  0
         if (value == null) {
 73  0
             return null;
 74  
         }
 75  0
         return convertValue(value, converter);
 76  
     }
 77  
 
 78  
     /**
 79  
      * Convert the specified non-null value to another type.
 80  
      * 
 81  
      * @param value  the input value to be converted, pre-checked to not be null
 82  
      * @param converter  the converter being used, not null
 83  
      * @return the converted value
 84  
      * @throws Exception if conversion fails, use ConversionException if creating
 85  
      *  a new exception, otherwise just allow exceptions to be thrown
 86  
      */
 87  
     protected Object convertValue(Object value, Converter converter) throws Exception {
 88  0
         throw new UnsupportedOperationException("Not implemented");
 89  
     }
 90  
 
 91  
     //-----------------------------------------------------------------------
 92  
     /**
 93  
      * The type to convert from.
 94  
      *
 95  
      * @return the Class object representing the class to convert to
 96  
      */
 97  
     public Class getFromType() {
 98  0
         return fromType;
 99  
     }
 100  
 
 101  
     /**
 102  
      * The type to convert to.
 103  
      *
 104  
      * @return the Class object representing the class to convert from
 105  
      */
 106  
     public Class getToType() {
 107  0
         return toType;
 108  
     }
 109  
 
 110  
     //-----------------------------------------------------------------------
 111  
     /**
 112  
      * Gets a suitable debugging string.
 113  
      * 
 114  
      * @return a debugging string
 115  
      */
 116  
     public String toString() {
 117  0
         String from = convertClassToName(getFromType());
 118  0
         String to = convertClassToName(getToType());
 119  0
         return "Conversion[" + from + "->" + to + "]";
 120  
     }
 121  
 
 122  
     /**
 123  
      * Converts a class to a string name for debugging.
 124  
      * 
 125  
      * @param cls  the class to convert
 126  
      * @return the class name
 127  
      */
 128  
     private String convertClassToName(Class cls) {
 129  0
         if (cls == null) {
 130  0
             return "null";
 131  
         }
 132  0
         String str = cls.getName();
 133  0
         int pos = str.lastIndexOf('.');
 134  0
         if (str.substring(0, pos).equals("java.lang")) {
 135  0
             str = str.substring(pos + 1);
 136  
         }
 137  0
         if (str.substring(0, pos).equals("java.util")) {
 138  0
             str = str.substring(pos + 1);
 139  
         }
 140  0
         return str;
 141  
     }
 142  
 
 143  
 }