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  package net.sf.ezmorph.primitive;
17  
18  import net.sf.ezmorph.MorphException;
19  
20  import org.apache.commons.lang.builder.EqualsBuilder;
21  import org.apache.commons.lang.builder.HashCodeBuilder;
22  
23  /**
24   * Morphs to a short.
25   *
26   * @author Andres Almiray <aalmiray@users.sourceforge.net>
27   */
28  public final class ShortMorpher extends AbstractIntegerMorpher
29  {
30     private short defaultValue;
31  
32     public ShortMorpher()
33     {
34        super();
35     }
36  
37     /**
38      * @param defaultValue return value if the value to be morphed is null
39      */
40     public ShortMorpher( short defaultValue )
41     {
42        super( true );
43        this.defaultValue = defaultValue;
44     }
45  
46     public boolean equals( Object obj )
47     {
48        if( this == obj ){
49           return true;
50        }
51        if( obj == null ){
52           return false;
53        }
54  
55        if( !(obj instanceof ShortMorpher) ){
56           return false;
57        }
58  
59        ShortMorpher other = (ShortMorpher) obj;
60        EqualsBuilder builder = new EqualsBuilder();
61        if( isUseDefault() && other.isUseDefault() ){
62           builder.append( getDefaultValue(), other.getDefaultValue() );
63           return builder.isEquals();
64        }else if( !isUseDefault() && !other.isUseDefault() ){
65           return builder.isEquals();
66        }else{
67           return false;
68        }
69     }
70  
71     /**
72      * Returns the default value for this Morpher.
73      */
74     public short getDefaultValue()
75     {
76        return defaultValue;
77     }
78  
79     public int hashCode()
80     {
81        HashCodeBuilder builder = new HashCodeBuilder();
82        if( isUseDefault() ){
83           builder.append( getDefaultValue() );
84        }
85        return builder.toHashCode();
86     }
87  
88     /**
89      * Morphs the input object into an output object of the supported type.
90      *
91      * @param value The input value to be morphed
92      * @exception MorphException if conversion cannot be performed successfully
93      */
94     public short morph( Object value )
95     {
96        if( value == null ){
97           if( isUseDefault() ){
98              return defaultValue;
99           }else{
100             throw new MorphException( "value is null" );
101          }
102       }
103 
104       if( value instanceof Number ){
105          return ((Number) value).shortValue();
106       }else{
107          short i = 0;
108          try{
109             i = Short.parseShort( getIntegerValue( value ) );
110             return i;
111          }
112          catch( NumberFormatException nfe ){
113             if( isUseDefault() ){
114                return defaultValue;
115             }else{
116                throw new MorphException( nfe );
117             }
118          }
119       }
120    }
121 
122    public Class morphsTo()
123    {
124       return Short.TYPE;
125    }
126 }