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  
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.ByteMorpher;
23  
24  import org.apache.commons.lang.builder.EqualsBuilder;
25  import org.apache.commons.lang.builder.HashCodeBuilder;
26  
27  /**
28   * Morphs an array to a byte[].
29   *
30   * @author Andres Almiray <aalmiray@users.sourceforge.net>
31   */
32  public final class ByteArrayMorpher extends AbstractArrayMorpher
33  {
34     private static final Class BYTE_ARRAY_CLASS = byte[].class;
35     private byte defaultValue;
36  
37     public ByteArrayMorpher()
38     {
39        super( false );
40     }
41  
42     /**
43      * @param defaultValue return value if the value to be morphed is null
44      */
45     public ByteArrayMorpher( byte defaultValue )
46     {
47        super( true );
48        this.defaultValue = defaultValue;
49     }
50  
51     public boolean equals( Object obj )
52     {
53        if( this == obj ){
54           return true;
55        }
56        if( obj == null ){
57           return false;
58        }
59  
60        if( !(obj instanceof ByteArrayMorpher) ){
61           return false;
62        }
63  
64        ByteArrayMorpher other = (ByteArrayMorpher) obj;
65        EqualsBuilder builder = new EqualsBuilder();
66        if( isUseDefault() && other.isUseDefault() ){
67           builder.append( getDefaultValue(), other.getDefaultValue() );
68           return builder.isEquals();
69        }else if( !isUseDefault() && !other.isUseDefault() ){
70           return builder.isEquals();
71        }else{
72           return false;
73        }
74     }
75  
76     public byte getDefaultValue()
77     {
78        return defaultValue;
79     }
80  
81     public int hashCode()
82     {
83        HashCodeBuilder builder = new HashCodeBuilder();
84        if( isUseDefault() ){
85           builder.append( getDefaultValue() );
86        }
87        return builder.toHashCode();
88     }
89  
90     public Object morph( Object array )
91     {
92        if( array == null ){
93           return null;
94        }
95  
96        if( BYTE_ARRAY_CLASS.isAssignableFrom( array.getClass() ) ){
97           // no conversion needed
98           return (byte[]) array;
99        }
100 
101       if( array.getClass()
102             .isArray() ){
103          int length = Array.getLength( array );
104          int dims = getDimensions( array.getClass() );
105          int[] dimensions = createDimensions( dims, length );
106          Object result = Array.newInstance( byte.class, dimensions );
107          ByteMorpher morpher = isUseDefault() ? new ByteMorpher( defaultValue ) : new ByteMorpher();
108          if( dims == 1 ){
109             for( int index = 0; index < length; index++ ){
110                Array.set( result, index, new Byte( morpher.morph( Array.get( array, index ) ) ) );
111             }
112          }else{
113             for( int index = 0; index < length; index++ ){
114                Array.set( result, index, morph( Array.get( array, index ) ) );
115             }
116          }
117          return result;
118       }else{
119          throw new MorphException( "argument is not an array: " + array.getClass() );
120       }
121    }
122 
123    public Class morphsTo()
124    {
125       return BYTE_ARRAY_CLASS;
126    }
127 }