| 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 net.sf.ezmorph.MorphException; |
| 20 | |
import net.sf.ezmorph.ObjectMorpher; |
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | 3 | public final class StringMorpher implements ObjectMorpher |
| 29 | |
{ |
| 30 | 4 | private static final StringMorpher INSTANCE = new StringMorpher(); |
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
public static StringMorpher getInstance() |
| 36 | |
{ |
| 37 | 1130 | return INSTANCE; |
| 38 | |
} |
| 39 | |
|
| 40 | 3 | private StringMorpher() |
| 41 | 1 | { |
| 42 | 4 | } |
| 43 | |
|
| 44 | |
public boolean equals( Object obj ) |
| 45 | |
{ |
| 46 | 12 | return INSTANCE == obj; |
| 47 | |
} |
| 48 | |
|
| 49 | |
public int hashCode() |
| 50 | |
{ |
| 51 | 12 | return 42 + getClass().hashCode(); |
| 52 | |
} |
| 53 | |
|
| 54 | |
public Object morph( Object value ) |
| 55 | |
{ |
| 56 | 96 | if( value == null ){ |
| 57 | 20 | return null; |
| 58 | |
} |
| 59 | |
|
| 60 | 76 | if( !supports( value.getClass() ) ){ |
| 61 | 4 | throw new MorphException( "Class not supported. " + value.getClass() ); |
| 62 | |
} |
| 63 | |
|
| 64 | 73 | if( String.class.isAssignableFrom( value.getClass() ) ){ |
| 65 | 24 | return (String) value; |
| 66 | |
} |
| 67 | |
|
| 68 | 48 | return String.valueOf( value ); |
| 69 | |
} |
| 70 | |
|
| 71 | |
public Class morphsTo() |
| 72 | |
{ |
| 73 | 2785 | return String.class; |
| 74 | |
} |
| 75 | |
|
| 76 | |
public boolean supports( Class clazz ) |
| 77 | |
{ |
| 78 | 140 | return !clazz.isArray(); |
| 79 | |
} |
| 80 | |
} |