Coverage Report - net.sf.ezmorph.array.ObjectArrayMorpher
 
Classes in this File Line Coverage Branch Coverage Complexity
ObjectArrayMorpher
90%
52/58
89%
34/38
5.143
 
 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  6384
       super( false );
 50  6384
       setMorpher( morpher );
 51  6372
    }
 52  
 
 53  
    public boolean equals( Object obj )
 54  
    {
 55  20
       if( this == obj ){
 56  8
          return true;
 57  
       }
 58  12
       if( obj == null ){
 59  4
          return false;
 60  
       }
 61  
 
 62  8
       if( !(obj instanceof ObjectArrayMorpher) ){
 63  4
          return false;
 64  
       }
 65  
 
 66  4
       ObjectArrayMorpher other = (ObjectArrayMorpher) obj;
 67  4
       return morpher.equals( other.morpher );
 68  
    }
 69  
 
 70  
    public int hashCode()
 71  
    {
 72  28
       return new HashCodeBuilder().append( morpher )
 73  12
             .toHashCode();
 74  
    }
 75  
 
 76  
    public Object morph( Object array )
 77  
    {
 78  280
       if( array == null ){
 79  88
          return null;
 80  
       }
 81  
 
 82  192
       if( array.getClass()
 83  144
             .isArray() ){
 84  188
          int length = Array.getLength( array );
 85  188
          int dims = getDimensions( array.getClass() );
 86  188
          int[] dimensions = createDimensions( dims, length );
 87  188
          Object result = Array.newInstance( this.target, dimensions );
 88  
 
 89  188
          if( dims == 1 ){
 90  388
             for( int index = 0; index < length; index++ ){
 91  
                try{
 92  220
                   Object value = Array.get( array, index );
 93  220
                   if( value != null && !morpher.supports( value.getClass() ) ){
 94  0
                      throw new MorphException( value.getClass() + " is not supported" );
 95  
                   }
 96  220
                   Object morphed = morphMethod.invoke( morpher, new Object[] { value } );
 97  220
                   Array.set( result, index, morphed );
 98  
                }
 99  0
                catch( MorphException me ){
 100  0
                   throw me;
 101  
                }
 102  0
                catch( Exception e ){
 103  0
                   throw new MorphException( e );
 104  55
                }
 105  
             }
 106  
          }else{
 107  52
             for( int index = 0; index < length; index++ ){
 108  32
                Array.set( result, index, morph( Array.get( array, index ) ) );
 109  
             }
 110  
          }
 111  
 
 112  188
          return result;
 113  
       }else{
 114  4
          throw new MorphException( "argument is not an array: " + array.getClass() );
 115  
       }
 116  
    }
 117  
 
 118  
    public Class morphsTo()
 119  
    {
 120  12508
       return targetArrayClass;
 121  
    }
 122  
 
 123  
    public boolean supports( Class clazz )
 124  
    {
 125  140
       if( clazz != null && !clazz.isArray() ){
 126  0
          return false;
 127  
       }
 128  280
       while( clazz.isArray() ){
 129  140
          clazz = clazz.getComponentType();
 130  
       }
 131  140
       return morpher.supports( clazz );
 132  
    }
 133  
 
 134  
    private void setMorpher( Morpher morpher )
 135  
    {
 136  6384
       if( morpher == null ){
 137  4
          throw new IllegalArgumentException( "morpher can not be null" );
 138  
       }
 139  6380
       if( morpher.morphsTo()
 140  4812
             .isArray() ){
 141  4
          throw new IllegalArgumentException( "morpher target class can not be an array" );
 142  
       }
 143  6376
       this.morpher = morpher;
 144  11185
       this.targetArrayClass = Array.newInstance( morpher.morphsTo(), 1 )
 145  4809
             .getClass();
 146  6376
       this.target = morpher.morphsTo();
 147  
 
 148  
       // cache the morph method
 149  
       try{
 150  11182
          morphMethod = morpher.getClass()
 151  4810
                .getDeclaredMethod( "morph", new Class[] { Object.class } );
 152  
       }
 153  4
       catch( NoSuchMethodException nsme ){
 154  4
          throw new IllegalArgumentException( nsme.getMessage() );
 155  1566
       }
 156  6372
    }
 157  
 }