| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
package net.sf.ezmorph.primitive; |
| 18 | |
|
| 19 | |
import net.sf.ezmorph.MorphException; |
| 20 | |
|
| 21 | |
import org.apache.commons.lang.builder.EqualsBuilder; |
| 22 | |
import org.apache.commons.lang.builder.HashCodeBuilder; |
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
public final class CharMorpher extends AbstractPrimitiveMorpher |
| 30 | |
{ |
| 31 | |
private char defaultValue; |
| 32 | |
|
| 33 | |
public CharMorpher() |
| 34 | |
{ |
| 35 | 180 | super(); |
| 36 | 180 | } |
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
public CharMorpher( char defaultValue ) |
| 42 | |
{ |
| 43 | 637 | super( true ); |
| 44 | 637 | this.defaultValue = defaultValue; |
| 45 | 637 | } |
| 46 | |
|
| 47 | |
public boolean equals( Object obj ) |
| 48 | |
{ |
| 49 | 28 | if( this == obj ){ |
| 50 | 8 | return true; |
| 51 | |
} |
| 52 | 20 | if( obj == null ){ |
| 53 | 4 | return false; |
| 54 | |
} |
| 55 | |
|
| 56 | 16 | if( !(obj instanceof CharMorpher) ){ |
| 57 | 4 | return false; |
| 58 | |
} |
| 59 | |
|
| 60 | 12 | CharMorpher other = (CharMorpher) obj; |
| 61 | 12 | EqualsBuilder builder = new EqualsBuilder(); |
| 62 | 12 | if( isUseDefault() && other.isUseDefault() ){ |
| 63 | 4 | builder.append( getDefaultValue(), other.getDefaultValue() ); |
| 64 | 4 | return builder.isEquals(); |
| 65 | 8 | }else if( !isUseDefault() && !other.isUseDefault() ){ |
| 66 | 4 | return builder.isEquals(); |
| 67 | |
}else{ |
| 68 | 4 | return false; |
| 69 | |
} |
| 70 | |
} |
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
public char getDefaultValue() |
| 76 | |
{ |
| 77 | 20 | return defaultValue; |
| 78 | |
} |
| 79 | |
|
| 80 | |
public int hashCode() |
| 81 | |
{ |
| 82 | 24 | HashCodeBuilder builder = new HashCodeBuilder(); |
| 83 | 24 | if( isUseDefault() ){ |
| 84 | 12 | builder.append( getDefaultValue() ); |
| 85 | |
} |
| 86 | 24 | return builder.toHashCode(); |
| 87 | |
} |
| 88 | |
|
| 89 | |
|
| 90 | |
|
| 91 | |
|
| 92 | |
|
| 93 | |
|
| 94 | |
|
| 95 | |
public char morph( Object value ) |
| 96 | |
{ |
| 97 | 208 | if( value == null ){ |
| 98 | 116 | if( isUseDefault() ){ |
| 99 | 108 | return defaultValue; |
| 100 | |
}else{ |
| 101 | 8 | throw new MorphException( "value is null" ); |
| 102 | |
} |
| 103 | |
} |
| 104 | |
|
| 105 | 92 | if( value instanceof Character ){ |
| 106 | 24 | return ((Character) value).charValue(); |
| 107 | |
}else{ |
| 108 | 68 | String s = String.valueOf( value ); |
| 109 | 68 | if( s.length() > 0 ){ |
| 110 | 56 | return s.charAt( 0 ); |
| 111 | |
}else{ |
| 112 | 12 | if( isUseDefault() ){ |
| 113 | 8 | return defaultValue; |
| 114 | |
}else{ |
| 115 | 4 | throw new MorphException( "Can't morph value: " + value ); |
| 116 | |
} |
| 117 | |
} |
| 118 | |
} |
| 119 | |
} |
| 120 | |
|
| 121 | |
public Class morphsTo() |
| 122 | |
{ |
| 123 | 1022 | return Character.TYPE; |
| 124 | |
} |
| 125 | |
} |