| 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.lang.reflect.Method; |
| 20 | |
import java.util.ArrayList; |
| 21 | |
import java.util.Iterator; |
| 22 | |
import java.util.List; |
| 23 | |
|
| 24 | |
import net.sf.ezmorph.MorphException; |
| 25 | |
import net.sf.ezmorph.Morpher; |
| 26 | |
|
| 27 | |
import org.apache.commons.lang.builder.HashCodeBuilder; |
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
public final class ObjectListMorpher extends AbstractObjectMorpher |
| 35 | |
{ |
| 36 | |
private Object defaultValue; |
| 37 | |
private Morpher morpher; |
| 38 | |
private Method morphMethod; |
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | 105 | public ObjectListMorpher( Morpher morpher ) |
| 49 | 35 | { |
| 50 | 140 | setMorpher( morpher ); |
| 51 | 128 | } |
| 52 | |
|
| 53 | |
public ObjectListMorpher( Morpher morpher, Object defaultValue ) |
| 54 | |
{ |
| 55 | 128 | super(true); |
| 56 | 128 | this.defaultValue = defaultValue; |
| 57 | 128 | setMorpher( morpher ); |
| 58 | 128 | } |
| 59 | |
|
| 60 | |
public boolean equals( Object obj ) |
| 61 | |
{ |
| 62 | 28 | if( this == obj ){ |
| 63 | 8 | return true; |
| 64 | |
} |
| 65 | 20 | if( obj == null ){ |
| 66 | 4 | return false; |
| 67 | |
} |
| 68 | |
|
| 69 | 16 | if( !(obj instanceof ObjectListMorpher) ){ |
| 70 | 4 | return false; |
| 71 | |
} |
| 72 | |
|
| 73 | 12 | ObjectListMorpher other = (ObjectListMorpher) obj; |
| 74 | 12 | return morpher.equals( other.morpher ); |
| 75 | |
} |
| 76 | |
|
| 77 | |
public int hashCode() |
| 78 | |
{ |
| 79 | 42 | return new HashCodeBuilder().append( morpher ) |
| 80 | 18 | .toHashCode(); |
| 81 | |
} |
| 82 | |
|
| 83 | |
public Object morph( Object value ) |
| 84 | |
{ |
| 85 | 24 | if( value == null ){ |
| 86 | 4 | return null; |
| 87 | |
} |
| 88 | |
|
| 89 | 20 | if( !supports( value.getClass() ) ){ |
| 90 | 4 | throw new MorphException( value.getClass() + " is not supported" ); |
| 91 | |
} |
| 92 | |
|
| 93 | 16 | List list = new ArrayList(); |
| 94 | 64 | for( Iterator i = ((List) value).iterator(); i.hasNext(); ){ |
| 95 | 48 | Object object = i.next(); |
| 96 | 48 | if( object == null ){ |
| 97 | 24 | if( isUseDefault() ){ |
| 98 | 12 | list.add( defaultValue ); |
| 99 | |
}else{ |
| 100 | 12 | list.add( object ); |
| 101 | |
} |
| 102 | |
}else{ |
| 103 | 24 | if( !morpher.supports( object.getClass() ) ){ |
| 104 | 0 | throw new MorphException( object.getClass() + " is not supported" ); |
| 105 | |
} |
| 106 | |
try{ |
| 107 | 24 | list.add( morphMethod.invoke( morpher, new Object[] { object } ) ); |
| 108 | |
} |
| 109 | 0 | catch( MorphException me ){ |
| 110 | 0 | throw me; |
| 111 | |
} |
| 112 | 0 | catch( Exception e ){ |
| 113 | 0 | throw new MorphException( e ); |
| 114 | 6 | } |
| 115 | |
} |
| 116 | 12 | } |
| 117 | |
|
| 118 | 16 | return list; |
| 119 | |
} |
| 120 | |
|
| 121 | |
public Class morphsTo() |
| 122 | |
{ |
| 123 | 2 | return List.class; |
| 124 | |
} |
| 125 | |
|
| 126 | |
public boolean supports( Class clazz ) |
| 127 | |
{ |
| 128 | 20 | return clazz != null && List.class.isAssignableFrom( clazz ); |
| 129 | |
} |
| 130 | |
|
| 131 | |
private void setMorpher( Morpher morpher ) |
| 132 | |
{ |
| 133 | 268 | if( morpher == null ){ |
| 134 | 4 | throw new IllegalArgumentException( "morpher can not be null" ); |
| 135 | |
} |
| 136 | 264 | this.morpher = morpher; |
| 137 | |
|
| 138 | |
|
| 139 | |
try{ |
| 140 | 456 | morphMethod = morpher.getClass() |
| 141 | 198 | .getDeclaredMethod( "morph", new Class[] { Object.class } ); |
| 142 | |
} |
| 143 | 8 | catch( NoSuchMethodException nsme ){ |
| 144 | 8 | throw new IllegalArgumentException( nsme.getMessage() ); |
| 145 | 64 | } |
| 146 | 256 | } |
| 147 | |
} |