| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
package net.sf.ezmorph.array; |
| 18 | |
|
| 19 | |
import java.lang.reflect.Array; |
| 20 | |
|
| 21 | |
import net.sf.ezmorph.MorphException; |
| 22 | |
import net.sf.ezmorph.primitive.FloatMorpher; |
| 23 | |
|
| 24 | |
import org.apache.commons.lang.builder.EqualsBuilder; |
| 25 | |
import org.apache.commons.lang.builder.HashCodeBuilder; |
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | 3 | public final class FloatArrayMorpher extends AbstractArrayMorpher |
| 33 | |
{ |
| 34 | 5 | private static final Class FLOAT_ARRAY_CLASS = float[].class; |
| 35 | |
private float defaultValue; |
| 36 | |
|
| 37 | |
public FloatArrayMorpher() |
| 38 | |
{ |
| 39 | 132 | super( false ); |
| 40 | 132 | } |
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
public FloatArrayMorpher( float defaultValue ) |
| 46 | |
{ |
| 47 | 637 | super( true ); |
| 48 | 637 | this.defaultValue = defaultValue; |
| 49 | 637 | } |
| 50 | |
|
| 51 | |
public boolean equals( Object obj ) |
| 52 | |
{ |
| 53 | 28 | if( this == obj ){ |
| 54 | 8 | return true; |
| 55 | |
} |
| 56 | 20 | if( obj == null ){ |
| 57 | 4 | return false; |
| 58 | |
} |
| 59 | |
|
| 60 | 16 | if( !(obj instanceof FloatArrayMorpher) ){ |
| 61 | 4 | return false; |
| 62 | |
} |
| 63 | |
|
| 64 | 12 | FloatArrayMorpher other = (FloatArrayMorpher) obj; |
| 65 | 12 | EqualsBuilder builder = new EqualsBuilder(); |
| 66 | 12 | if( isUseDefault() && other.isUseDefault() ){ |
| 67 | 4 | builder.append( getDefaultValue(), other.getDefaultValue() ); |
| 68 | 4 | return builder.isEquals(); |
| 69 | 8 | }else if( !isUseDefault() && !other.isUseDefault() ){ |
| 70 | 4 | return builder.isEquals(); |
| 71 | |
}else{ |
| 72 | 4 | return false; |
| 73 | |
} |
| 74 | |
} |
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
public float getDefaultValue() |
| 80 | |
{ |
| 81 | 20 | return defaultValue; |
| 82 | |
} |
| 83 | |
|
| 84 | |
public int hashCode() |
| 85 | |
{ |
| 86 | 24 | HashCodeBuilder builder = new HashCodeBuilder(); |
| 87 | 24 | if( isUseDefault() ){ |
| 88 | 12 | builder.append( getDefaultValue() ); |
| 89 | |
} |
| 90 | 24 | return builder.toHashCode(); |
| 91 | |
} |
| 92 | |
|
| 93 | |
public Object morph( Object array ) |
| 94 | |
{ |
| 95 | 76 | if( array == null ){ |
| 96 | 4 | return null; |
| 97 | |
} |
| 98 | |
|
| 99 | 72 | if( FLOAT_ARRAY_CLASS.isAssignableFrom( array.getClass() ) ){ |
| 100 | |
|
| 101 | 28 | return (float[]) array; |
| 102 | |
} |
| 103 | |
|
| 104 | 44 | if( array.getClass() |
| 105 | 33 | .isArray() ){ |
| 106 | 40 | int length = Array.getLength( array ); |
| 107 | 40 | int dims = getDimensions( array.getClass() ); |
| 108 | 40 | int[] dimensions = createDimensions( dims, length ); |
| 109 | 40 | Object result = Array.newInstance( float.class, dimensions ); |
| 110 | 70 | FloatMorpher morpher = isUseDefault() ? new FloatMorpher( defaultValue ) |
| 111 | 27 | : new FloatMorpher(); |
| 112 | 40 | if( dims == 1 ){ |
| 113 | 60 | for( int index = 0; index < length; index++ ){ |
| 114 | 44 | Array.set( result, index, new Float( morpher.morph( Array.get( array, index ) ) ) ); |
| 115 | |
} |
| 116 | |
}else{ |
| 117 | 60 | for( int index = 0; index < length; index++ ){ |
| 118 | 40 | Array.set( result, index, morph( Array.get( array, index ) ) ); |
| 119 | |
} |
| 120 | |
} |
| 121 | 36 | return result; |
| 122 | |
}else{ |
| 123 | 4 | throw new MorphException( "argument is not an array: " + array.getClass() ); |
| 124 | |
} |
| 125 | |
} |
| 126 | |
|
| 127 | |
public Class morphsTo() |
| 128 | |
{ |
| 129 | 1022 | return FLOAT_ARRAY_CLASS; |
| 130 | |
} |
| 131 | |
} |