Coverage Report - net.sourceforge.jeuclid.context.typewrapper.NumberTypeWrapper
 
Classes in this File Line Coverage Branch Coverage Complexity
NumberTypeWrapper
35%
5/14
50%
1/2
4,667
 
 1  
 /*
 2  
  * Copyright 2008 - 2008 JEuclid, http://jeuclid.sf.net
 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  
 
 17  
 /* $Id: NumberTypeWrapper.java,v 03dc0884e86f 2008/06/21 10:53:35 maxberger $ */
 18  
 
 19  
 package net.sourceforge.jeuclid.context.typewrapper;
 20  
 
 21  
 import java.lang.reflect.InvocationTargetException;
 22  
 
 23  
 /**
 24  
  * Converting String to Numbers and vice versa is also straightforward.
 25  
  * 
 26  
  * @version $Revision: 03dc0884e86f $
 27  
  */
 28  
 public final class NumberTypeWrapper extends AbstractSimpleTypeWrapper {
 29  
     /**
 30  
      * 
 31  
      */
 32  
     private static final long serialVersionUID = 1L;
 33  
 
 34  
     /**
 35  
      * Simple constructor.
 36  
      * 
 37  
      * @param valueType
 38  
      *            a subclass of Number
 39  
      */
 40  
     public NumberTypeWrapper(final Class<? extends Number> valueType) {
 41  1045
         super(valueType);
 42  1045
     }
 43  
 
 44  
     /**
 45  
      * @param valueType
 46  
      *            a subclass of Number
 47  
      * @return the singleton instance.
 48  
      */
 49  
     public static TypeWrapper getInstance(
 50  
             final Class<? extends Number> valueType) {
 51  1045
         return new NumberTypeWrapper(valueType);
 52  
     }
 53  
 
 54  
     /** {@inheritDoc} */
 55  
     @Override
 56  
     public Object fromString(final String value) {
 57  1254
         if (value == null) {
 58  0
             return null;
 59  
         }
 60  
         try {
 61  1254
             return this.getValueType().getConstructor(
 62  
                     new Class[] { String.class }).newInstance(
 63  
                     new Object[] { value });
 64  0
         } catch (final NoSuchMethodException e) {
 65  0
             throw new IllegalArgumentException(TypeWrapper.FAILED_TO_CONVERT
 66  
                     + value + TypeWrapper.TO + this.getValueType(), e);
 67  0
         } catch (final IllegalAccessException e) {
 68  0
             throw new IllegalArgumentException(TypeWrapper.FAILED_TO_CONVERT
 69  
                     + value + TypeWrapper.TO + this.getValueType(), e);
 70  0
         } catch (final InstantiationException e) {
 71  0
             throw new IllegalArgumentException(TypeWrapper.FAILED_TO_CONVERT
 72  
                     + value + TypeWrapper.TO + this.getValueType(), e);
 73  0
         } catch (final InvocationTargetException e) {
 74  0
             throw new IllegalArgumentException(TypeWrapper.FAILED_TO_CONVERT
 75  
                     + value + TypeWrapper.TO + this.getValueType(), e);
 76  
         }
 77  
     }
 78  
 }