View Javadoc

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  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   * Morphs to a BigInteger.
30   *
31   * @author Andres Almiray <aalmiray@users.sourceforge.net>
32   */
33  public final class BigIntegerMorpher extends AbstractObjectMorpher
34  {
35     private BigInteger defaultValue;
36  
37     public BigIntegerMorpher()
38     {
39        super();
40     }
41  
42     /**
43      * @param defaultValue return value if the value to be morphed is null
44      */
45     public BigIntegerMorpher( BigInteger defaultValue )
46     {
47        super( true );
48        this.defaultValue = defaultValue;
49     }
50  
51     public boolean equals( Object obj )
52     {
53        if( this == obj ){
54           return true;
55        }
56        if( obj == null ){
57           return false;
58        }
59  
60        if( !(obj instanceof BigIntegerMorpher) ){
61           return false;
62        }
63  
64        BigIntegerMorpher other = (BigIntegerMorpher) obj;
65        EqualsBuilder builder = new EqualsBuilder();
66        if( isUseDefault() && other.isUseDefault() ){
67           builder.append( getDefaultValue(), other.getDefaultValue() );
68           return builder.isEquals();
69        }else if( !isUseDefault() && !other.isUseDefault() ){
70           return builder.isEquals();
71        }else{
72           return false;
73        }
74     }
75  
76     /**
77      * Returns the default value for this Morpher.
78      */
79     public BigInteger getDefaultValue()
80     {
81        return defaultValue;
82     }
83  
84     public int hashCode()
85     {
86        HashCodeBuilder builder = new HashCodeBuilder();
87        if( isUseDefault() ){
88           builder.append( getDefaultValue() );
89        }
90        return builder.toHashCode();
91     }
92  
93     public Object morph( Object value )
94     {
95        if( value instanceof BigInteger ){
96           return value;
97        }
98  
99        if( value == null ){
100          if( isUseDefault() ){
101             return defaultValue;
102          }else{
103             return (BigInteger) null;
104          }
105       }
106 
107       if( value instanceof Number ){
108          if( value instanceof Float ){
109             Float f = ((Float) value);
110             if( f.isInfinite() || f.isNaN() ){
111                throw new MorphException( "BigInteger can not be infinite or NaN" );
112             }
113          }else if( value instanceof Double ){
114             Double d = ((Double) value);
115             if( d.isInfinite() || d.isNaN() ){
116                throw new MorphException( "BigInteger can not be infinite or NaN" );
117             }
118          }else if( value instanceof BigDecimal ){
119             return ((BigDecimal) value).toBigInteger();
120          }
121          return BigInteger.valueOf( ((Number) value).longValue() );
122       }else{
123          try{
124             String str = getIntegerValue( value );
125             if( str.length() == 0 || str.equalsIgnoreCase( "null" ) ){
126                return (BigInteger) null;
127             }else{
128                return new BigInteger( str );
129             }
130          }
131          catch( NumberFormatException nfe ){
132             if( isUseDefault() ){
133                return defaultValue;
134             }else{
135                throw new MorphException( nfe );
136             }
137          }
138       }
139    }
140 
141    public Class morphsTo()
142    {
143       return BigInteger.class;
144    }
145 
146    /**
147     * Trims the String from the begining to the first "."
148     */
149    protected String getIntegerValue( Object obj )
150    {
151       // use en_US Locale
152       Locale defaultLocale = Locale.getDefault();
153       String str = null;
154       try{
155          Locale.setDefault( Locale.US );
156          str = String.valueOf( obj )
157                .trim();
158       }
159       finally{
160          Locale.setDefault( defaultLocale );
161       }
162 
163       int index = str.indexOf( "." );
164       if( index != -1 ){
165          str = str.substring( 0, index );
166       }
167       return str;
168    }
169 }