| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| BigDecimalMorpher |
|
| 5.857142857142857;5.857 |
| 1 | /* | |
| 2 | * Copyright 2006-2007 the original author or authors. | |
| 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 | package net.sf.ezmorph.object; | |
| 18 | ||
| 19 | import java.math.BigDecimal; | |
| 20 | import java.math.BigInteger; | |
| 21 | ||
| 22 | import net.sf.ezmorph.MorphException; | |
| 23 | ||
| 24 | import org.apache.commons.lang.builder.EqualsBuilder; | |
| 25 | import org.apache.commons.lang.builder.HashCodeBuilder; | |
| 26 | ||
| 27 | /** | |
| 28 | * Morphs to a BigDecimal. | |
| 29 | * | |
| 30 | * @author Andres Almiray <aalmiray@users.sourceforge.net> | |
| 31 | */ | |
| 32 | public final class BigDecimalMorpher extends AbstractObjectMorpher | |
| 33 | { | |
| 34 | private BigDecimal defaultValue; | |
| 35 | ||
| 36 | public BigDecimalMorpher() | |
| 37 | { | |
| 38 | 168 | super(); |
| 39 | 168 | } |
| 40 | ||
| 41 | /** | |
| 42 | * @param defaultValue return value if the value to be morphed is null | |
| 43 | */ | |
| 44 | public BigDecimalMorpher( BigDecimal defaultValue ) | |
| 45 | { | |
| 46 | 184 | super( true ); |
| 47 | 184 | this.defaultValue = defaultValue; |
| 48 | 184 | } |
| 49 | ||
| 50 | public boolean equals( Object obj ) | |
| 51 | { | |
| 52 | 28 | if( this == obj ){ |
| 53 | 8 | return true; |
| 54 | } | |
| 55 | 20 | if( obj == null ){ |
| 56 | 4 | return false; |
| 57 | } | |
| 58 | ||
| 59 | 16 | if( !(obj instanceof BigDecimalMorpher) ){ |
| 60 | 4 | return false; |
| 61 | } | |
| 62 | ||
| 63 | 12 | BigDecimalMorpher other = (BigDecimalMorpher) obj; |
| 64 | 12 | EqualsBuilder builder = new EqualsBuilder(); |
| 65 | 12 | if( isUseDefault() && other.isUseDefault() ){ |
| 66 | 4 | builder.append( getDefaultValue(), other.getDefaultValue() ); |
| 67 | 4 | return builder.isEquals(); |
| 68 | 8 | }else if( !isUseDefault() && !other.isUseDefault() ){ |
| 69 | 4 | return builder.isEquals(); |
| 70 | }else{ | |
| 71 | 4 | return false; |
| 72 | } | |
| 73 | } | |
| 74 | ||
| 75 | /** | |
| 76 | * Returns the default value for this Morpher. | |
| 77 | */ | |
| 78 | public BigDecimal getDefaultValue() | |
| 79 | { | |
| 80 | 20 | return defaultValue; |
| 81 | } | |
| 82 | ||
| 83 | public int hashCode() | |
| 84 | { | |
| 85 | 24 | HashCodeBuilder builder = new HashCodeBuilder(); |
| 86 | 24 | if( isUseDefault() ){ |
| 87 | 12 | builder.append( getDefaultValue() ); |
| 88 | } | |
| 89 | 24 | return builder.toHashCode(); |
| 90 | } | |
| 91 | ||
| 92 | public Object morph( Object value ) | |
| 93 | { | |
| 94 | 76 | if( value instanceof BigDecimal ){ |
| 95 | 4 | return value; |
| 96 | } | |
| 97 | ||
| 98 | 72 | if( value == null ){ |
| 99 | 8 | if( isUseDefault() ){ |
| 100 | 4 | return defaultValue; |
| 101 | }else{ | |
| 102 | 4 | return (BigDecimal) null; |
| 103 | } | |
| 104 | } | |
| 105 | ||
| 106 | 64 | if( value instanceof Number ){ |
| 107 | 28 | if( value instanceof Float ){ |
| 108 | 12 | Float f = ((Float) value); |
| 109 | 12 | if( f.isInfinite() || f.isNaN() ){ |
| 110 | 8 | throw new MorphException( "BigDecimal can not be infinite or NaN" ); |
| 111 | } | |
| 112 | 13 | }else if( value instanceof Double ){ |
| 113 | 12 | Double d = ((Double) value); |
| 114 | 12 | if( d.isInfinite() || d.isNaN() ){ |
| 115 | 8 | throw new MorphException( "BigDecimal can not be infinite or NaN" ); |
| 116 | } | |
| 117 | 4 | }else if( value instanceof BigInteger ){ |
| 118 | 4 | return new BigDecimal( (BigInteger) value ); |
| 119 | } | |
| 120 | ||
| 121 | 8 | return new BigDecimal( ((Number) value).doubleValue() ); |
| 122 | }else{ | |
| 123 | try{ | |
| 124 | 63 | String str = String.valueOf( value ) |
| 125 | 27 | .trim(); |
| 126 | 36 | if( str.length() == 0 || str.equalsIgnoreCase( "null" ) ){ |
| 127 | 8 | return (BigDecimal) null; |
| 128 | }else{ | |
| 129 | 28 | return new BigDecimal( str ); |
| 130 | } | |
| 131 | } | |
| 132 | 16 | catch( NumberFormatException nfe ){ |
| 133 | 16 | if( isUseDefault() ){ |
| 134 | 12 | return defaultValue; |
| 135 | }else{ | |
| 136 | 4 | throw new MorphException( nfe ); |
| 137 | } | |
| 138 | } | |
| 139 | } | |
| 140 | } | |
| 141 | ||
| 142 | public Class morphsTo() | |
| 143 | { | |
| 144 | 0 | return BigDecimal.class; |
| 145 | } | |
| 146 | } |