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  import java.lang.reflect.Method;
21  
22  import net.sf.ezmorph.MorphException;
23  import net.sf.ezmorph.Morpher;
24  
25  import org.apache.commons.lang.builder.HashCodeBuilder;
26  
27  /**
28   * Morphs an array to another array using a Morpher.
29   *
30   * @author Andres Almiray <aalmiray@users.sourceforge.net>
31   */
32  public final class ObjectArrayMorpher extends AbstractArrayMorpher
33  {
34     private Morpher morpher;
35     private Method morphMethod;
36     private Class target;
37     private Class targetArrayClass;
38  
39     /**
40      * Creates a new ArrayMorpher which will use another Morpher for its inner
41      * type.<br>
42      * The inner morpher can not morph to an array. Multiple dimension arrays are
43      * already handled by this class.
44      *
45      * @param morpher the Morpher that will handle the array's inner type.
46      */
47     public ObjectArrayMorpher( Morpher morpher )
48     {
49        super( false );
50        setMorpher( morpher );
51     }
52  
53     public boolean equals( Object obj )
54     {
55        if( this == obj ){
56           return true;
57        }
58        if( obj == null ){
59           return false;
60        }
61  
62        if( !(obj instanceof ObjectArrayMorpher) ){
63           return false;
64        }
65  
66        ObjectArrayMorpher other = (ObjectArrayMorpher) obj;
67        return morpher.equals( other.morpher );
68     }
69  
70     public int hashCode()
71     {
72        return new HashCodeBuilder().append( morpher )
73              .toHashCode();
74     }
75  
76     public Object morph( Object array )
77     {
78        if( array == null ){
79           return null;
80        }
81  
82        if( array.getClass()
83              .isArray() ){
84           int length = Array.getLength( array );
85           int dims = getDimensions( array.getClass() );
86           int[] dimensions = createDimensions( dims, length );
87           Object result = Array.newInstance( this.target, dimensions );
88  
89           if( dims == 1 ){
90              for( int index = 0; index < length; index++ ){
91                 try{
92                    Object value = Array.get( array, index );
93                    if( value != null && !morpher.supports( value.getClass() ) ){
94                       throw new MorphException( value.getClass() + " is not supported" );
95                    }
96                    Object morphed = morphMethod.invoke( morpher, new Object[] { value } );
97                    Array.set( result, index, morphed );
98                 }
99                 catch( MorphException me ){
100                   throw me;
101                }
102                catch( Exception e ){
103                   throw new MorphException( e );
104                }
105             }
106          }else{
107             for( int index = 0; index < length; index++ ){
108                Array.set( result, index, morph( Array.get( array, index ) ) );
109             }
110          }
111 
112          return result;
113       }else{
114          throw new MorphException( "argument is not an array: " + array.getClass() );
115       }
116    }
117 
118    public Class morphsTo()
119    {
120       return targetArrayClass;
121    }
122 
123    public boolean supports( Class clazz )
124    {
125       if( clazz != null && !clazz.isArray() ){
126          return false;
127       }
128       while( clazz.isArray() ){
129          clazz = clazz.getComponentType();
130       }
131       return morpher.supports( clazz );
132    }
133 
134    private void setMorpher( Morpher morpher )
135    {
136       if( morpher == null ){
137          throw new IllegalArgumentException( "morpher can not be null" );
138       }
139       if( morpher.morphsTo()
140             .isArray() ){
141          throw new IllegalArgumentException( "morpher target class can not be an array" );
142       }
143       this.morpher = morpher;
144       this.targetArrayClass = Array.newInstance( morpher.morphsTo(), 1 )
145             .getClass();
146       this.target = morpher.morphsTo();
147 
148       // cache the morph method
149       try{
150          morphMethod = morpher.getClass()
151                .getDeclaredMethod( "morph", new Class[] { Object.class } );
152       }
153       catch( NoSuchMethodException nsme ){
154          throw new IllegalArgumentException( nsme.getMessage() );
155       }
156    }
157 }