| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| DoubleConverter |
|
| 5.333333333333333;5.333 |
| 1 | /* | |
| 2 | * Copyright 2003-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.convert1.string; | |
| 17 | ||
| 18 | ||
| 19 | import org.apache.commons.convert1.ConversionException; | |
| 20 | import org.apache.commons.convert1.Converter; | |
| 21 | ||
| 22 | ||
| 23 | /** | |
| 24 | * <p>Standard {@link Converter} implementation that converts an incoming | |
| 25 | * String into a <code>java.lang.Double</code> object, optionally using a | |
| 26 | * default value or throwing a {@link ConversionException} if a conversion | |
| 27 | * error occurs.</p> | |
| 28 | * | |
| 29 | * @author Craig R. McClanahan | |
| 30 | * @version $Id: DoubleConverter.java 155441 2005-02-26 13:19:22Z dirkv $ | |
| 31 | * @since 0.1 | |
| 32 | */ | |
| 33 | ||
| 34 | public final class DoubleConverter implements Converter { | |
| 35 | ||
| 36 | ||
| 37 | // ----------------------------------------------------------- Constructors | |
| 38 | ||
| 39 | ||
| 40 | /** | |
| 41 | * Create a {@link Converter} that will throw a {@link ConversionException} | |
| 42 | * if a conversion error occurs. | |
| 43 | */ | |
| 44 | 0 | public DoubleConverter() { |
| 45 | ||
| 46 | 0 | this.defaultValue = null; |
| 47 | 0 | this.useDefault = false; |
| 48 | ||
| 49 | 0 | } |
| 50 | ||
| 51 | ||
| 52 | /** | |
| 53 | * Create a {@link Converter} that will return the specified default value | |
| 54 | * if a conversion error occurs. | |
| 55 | * | |
| 56 | * @param defaultValue The default value to be returned | |
| 57 | */ | |
| 58 | 0 | public DoubleConverter(Object defaultValue) { |
| 59 | ||
| 60 | 0 | this.defaultValue = defaultValue; |
| 61 | 0 | this.useDefault = true; |
| 62 | ||
| 63 | 0 | } |
| 64 | ||
| 65 | ||
| 66 | // ----------------------------------------------------- Instance Variables | |
| 67 | ||
| 68 | ||
| 69 | /** | |
| 70 | * The default value specified to our Constructor, if any. | |
| 71 | */ | |
| 72 | 0 | private Object defaultValue = null; |
| 73 | ||
| 74 | ||
| 75 | /** | |
| 76 | * Should we return the default value on conversion errors? | |
| 77 | */ | |
| 78 | 0 | private boolean useDefault = true; |
| 79 | ||
| 80 | ||
| 81 | // --------------------------------------------------------- Public Methods | |
| 82 | ||
| 83 | ||
| 84 | /** | |
| 85 | * Convert the specified input object into an output object of the | |
| 86 | * specified type. | |
| 87 | * | |
| 88 | * @param type Data type to which this value should be converted | |
| 89 | * @param value The input value to be converted | |
| 90 | * | |
| 91 | * @exception ConversionException if conversion cannot be performed | |
| 92 | * successfully | |
| 93 | */ | |
| 94 | public Object convert(Class type, Object value) { | |
| 95 | ||
| 96 | 0 | if (value == null) { |
| 97 | 0 | if (useDefault) { |
| 98 | 0 | return (defaultValue); |
| 99 | } else { | |
| 100 | 0 | throw new ConversionException("No value specified"); |
| 101 | } | |
| 102 | } | |
| 103 | ||
| 104 | 0 | if (value instanceof Double) { |
| 105 | 0 | return (value); |
| 106 | 0 | } else if(value instanceof Number) { |
| 107 | 0 | return new Double(((Number)value).doubleValue()); |
| 108 | } | |
| 109 | ||
| 110 | ||
| 111 | try { | |
| 112 | 0 | return (new Double(value.toString())); |
| 113 | 0 | } catch (Exception e) { |
| 114 | 0 | if (useDefault) { |
| 115 | 0 | return (defaultValue); |
| 116 | } else { | |
| 117 | 0 | throw new ConversionException(e); |
| 118 | } | |
| 119 | } | |
| 120 | ||
| 121 | } | |
| 122 | ||
| 123 | ||
| 124 | } |