Coverage Report - net.sf.ezmorph.object.ObjectListMorpher
 
Classes in this File Line Coverage Branch Coverage Complexity
ObjectListMorpher
90%
45/50
88%
28/32
3.875
 
 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.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  
  * Morphs a List to another List using a Morpher.
 31  
  *
 32  
  * @author Andres Almiray <aalmiray@users.sourceforge.net>
 33  
  */
 34  
 public final class ObjectListMorpher extends AbstractObjectMorpher
 35  
 {
 36  
    private Object defaultValue;
 37  
    private Morpher morpher;
 38  
    private Method morphMethod;
 39  
 
 40  
    /**
 41  
     * Creates a new ArrayMorpher which will use another Morpher for its inner
 42  
     * type.<br>
 43  
     * The inner morpher can not morph to an array. Multiple dimension arrays are
 44  
     * already handled by this class.
 45  
     *
 46  
     * @param morpher the Morpher that will handle the array's inner type.
 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  
       // cache the morph method
 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  
 }