| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
package net.sf.ezmorph.object; |
| 18 | |
|
| 19 | |
import java.math.BigDecimal; |
| 20 | |
import java.math.BigInteger; |
| 21 | |
import java.util.Locale; |
| 22 | |
|
| 23 | |
import net.sf.ezmorph.MorphException; |
| 24 | |
|
| 25 | |
import org.apache.commons.lang.builder.EqualsBuilder; |
| 26 | |
import org.apache.commons.lang.builder.HashCodeBuilder; |
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
public final class BigIntegerMorpher extends AbstractObjectMorpher |
| 34 | |
{ |
| 35 | |
private BigInteger defaultValue; |
| 36 | |
|
| 37 | |
public BigIntegerMorpher() |
| 38 | |
{ |
| 39 | 176 | super(); |
| 40 | 176 | } |
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
public BigIntegerMorpher( BigInteger defaultValue ) |
| 46 | |
{ |
| 47 | 192 | super( true ); |
| 48 | 192 | this.defaultValue = defaultValue; |
| 49 | 192 | } |
| 50 | |
|
| 51 | |
public boolean equals( Object obj ) |
| 52 | |
{ |
| 53 | 28 | if( this == obj ){ |
| 54 | 8 | return true; |
| 55 | |
} |
| 56 | 20 | if( obj == null ){ |
| 57 | 4 | return false; |
| 58 | |
} |
| 59 | |
|
| 60 | 16 | if( !(obj instanceof BigIntegerMorpher) ){ |
| 61 | 4 | return false; |
| 62 | |
} |
| 63 | |
|
| 64 | 12 | BigIntegerMorpher other = (BigIntegerMorpher) obj; |
| 65 | 12 | EqualsBuilder builder = new EqualsBuilder(); |
| 66 | 12 | if( isUseDefault() && other.isUseDefault() ){ |
| 67 | 4 | builder.append( getDefaultValue(), other.getDefaultValue() ); |
| 68 | 4 | return builder.isEquals(); |
| 69 | 8 | }else if( !isUseDefault() && !other.isUseDefault() ){ |
| 70 | 4 | return builder.isEquals(); |
| 71 | |
}else{ |
| 72 | 4 | return false; |
| 73 | |
} |
| 74 | |
} |
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
public BigInteger getDefaultValue() |
| 80 | |
{ |
| 81 | 20 | return defaultValue; |
| 82 | |
} |
| 83 | |
|
| 84 | |
public int hashCode() |
| 85 | |
{ |
| 86 | 24 | HashCodeBuilder builder = new HashCodeBuilder(); |
| 87 | 24 | if( isUseDefault() ){ |
| 88 | 12 | builder.append( getDefaultValue() ); |
| 89 | |
} |
| 90 | 24 | return builder.toHashCode(); |
| 91 | |
} |
| 92 | |
|
| 93 | |
public Object morph( Object value ) |
| 94 | |
{ |
| 95 | 100 | if( value instanceof BigInteger ){ |
| 96 | 4 | return value; |
| 97 | |
} |
| 98 | |
|
| 99 | 96 | if( value == null ){ |
| 100 | 8 | if( isUseDefault() ){ |
| 101 | 4 | return defaultValue; |
| 102 | |
}else{ |
| 103 | 4 | return (BigInteger) null; |
| 104 | |
} |
| 105 | |
} |
| 106 | |
|
| 107 | 88 | if( value instanceof Number ){ |
| 108 | 48 | if( value instanceof Float ){ |
| 109 | 12 | Float f = ((Float) value); |
| 110 | 12 | if( f.isInfinite() || f.isNaN() ){ |
| 111 | 8 | throw new MorphException( "BigInteger can not be infinite or NaN" ); |
| 112 | |
} |
| 113 | 28 | }else if( value instanceof Double ){ |
| 114 | 12 | Double d = ((Double) value); |
| 115 | 12 | if( d.isInfinite() || d.isNaN() ){ |
| 116 | 8 | throw new MorphException( "BigInteger can not be infinite or NaN" ); |
| 117 | |
} |
| 118 | 19 | }else if( value instanceof BigDecimal ){ |
| 119 | 8 | return ((BigDecimal) value).toBigInteger(); |
| 120 | |
} |
| 121 | 24 | return BigInteger.valueOf( ((Number) value).longValue() ); |
| 122 | |
}else{ |
| 123 | |
try{ |
| 124 | 40 | String str = getIntegerValue( value ); |
| 125 | 40 | if( str.length() == 0 || str.equalsIgnoreCase( "null" ) ){ |
| 126 | 8 | return (BigInteger) null; |
| 127 | |
}else{ |
| 128 | 32 | return new BigInteger( str ); |
| 129 | |
} |
| 130 | |
} |
| 131 | 16 | catch( NumberFormatException nfe ){ |
| 132 | 16 | if( isUseDefault() ){ |
| 133 | 12 | return defaultValue; |
| 134 | |
}else{ |
| 135 | 4 | throw new MorphException( nfe ); |
| 136 | |
} |
| 137 | |
} |
| 138 | |
} |
| 139 | |
} |
| 140 | |
|
| 141 | |
public Class morphsTo() |
| 142 | |
{ |
| 143 | 0 | return BigInteger.class; |
| 144 | |
} |
| 145 | |
|
| 146 | |
|
| 147 | |
|
| 148 | |
|
| 149 | |
protected String getIntegerValue( Object obj ) |
| 150 | |
{ |
| 151 | |
|
| 152 | 40 | Locale defaultLocale = Locale.getDefault(); |
| 153 | 40 | String str = null; |
| 154 | |
try{ |
| 155 | 40 | Locale.setDefault( Locale.US ); |
| 156 | 70 | str = String.valueOf( obj ) |
| 157 | 30 | .trim(); |
| 158 | |
} |
| 159 | 0 | finally{ |
| 160 | 40 | Locale.setDefault( defaultLocale ); |
| 161 | 10 | } |
| 162 | |
|
| 163 | 40 | int index = str.indexOf( "." ); |
| 164 | 40 | if( index != -1 ){ |
| 165 | 8 | str = str.substring( 0, index ); |
| 166 | |
} |
| 167 | 40 | return str; |
| 168 | |
} |
| 169 | |
} |