Coverage Report - org.apache.commons.flatfile.morph.ContainerToIndexedEntityCollectionCopier
 
Classes in this File Line Coverage Branch Coverage Complexity
ContainerToIndexedEntityCollectionCopier
81%
18/22
100%
4/4
1.25
ContainerToIndexedEntityCollectionCopier$1
N/A
N/A
1.25
ContainerToIndexedEntityCollectionCopier$SizeOneReflector
50%
1/2
N/A
1.25
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  * contributor license agreements.  See the NOTICE file distributed with
 4  
  * this work for additional information regarding copyright ownership.
 5  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 6  
  * (the "License"); you may not use this file except in compliance with
 7  
  * the License.  You may obtain a copy of the License at
 8  
  *
 9  
  *      http://www.apache.org/licenses/LICENSE-2.0
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.apache.commons.flatfile.morph;
 18  
 
 19  
 import java.util.Locale;
 20  
 
 21  
 import org.apache.commons.flatfile.IndexedEntityCollection;
 22  
 
 23  
 import net.sf.morph.reflect.Reflector;
 24  
 import net.sf.morph.reflect.SizableReflector;
 25  
 import net.sf.morph.reflect.reflectors.ArrayReflector;
 26  
 import net.sf.morph.reflect.reflectors.CollectionReflector;
 27  
 import net.sf.morph.reflect.reflectors.EnumerationReflector;
 28  
 import net.sf.morph.reflect.reflectors.IteratorReflector;
 29  
 import net.sf.morph.reflect.reflectors.ListReflector;
 30  
 import net.sf.morph.reflect.reflectors.ObjectReflector;
 31  
 import net.sf.morph.reflect.reflectors.SetReflector;
 32  
 import net.sf.morph.reflect.reflectors.SimpleDelegatingReflector;
 33  
 import net.sf.morph.reflect.reflectors.SortedSetReflector;
 34  
 import net.sf.morph.transform.copiers.ContainerCopier;
 35  
 import net.sf.morph.transform.TransformationException;
 36  
 
 37  
 /**
 38  
  * Copies containers to IndexedEntityCollections. Additionally recognizes <code>null</code> copy source as an empty
 39  
  * collection.
 40  
  * @version $Revision: 1301237 $ $Date: 2012-03-15 17:08:23 -0500 (Thu, 15 Mar 2012) $
 41  
  */
 42  
 public class ContainerToIndexedEntityCollectionCopier extends ContainerCopier {
 43  6
     private static final Class<?>[] DEST_CLASSES = new Class[] { IndexedEntityCollection.class };
 44  
 
 45  24
     private static class SizeOneReflector extends ObjectReflector implements SizableReflector {
 46  
         /**
 47  
          * {@inheritDoc}
 48  
          */
 49  
         @Override
 50  
         protected int getSizeImpl(Object container) throws Exception {
 51  0
             return 1;
 52  
         }
 53  
     }
 54  
 
 55  
     /**
 56  
      * Create a new ContainerToIndexedEntityCollectionCopier
 57  
      */
 58  12
     public ContainerToIndexedEntityCollectionCopier() {
 59  16
         setReflector(new SimpleDelegatingReflector(new Reflector[] {
 60  4
                 new EntityCollectionReflector(), new ListReflector(), new SortedSetReflector(),
 61  4
                 new SetReflector(), new EnumerationReflector(), new IteratorReflector(),
 62  4
                 new ArrayReflector(), new CollectionReflector(), new SizeOneReflector() }));
 63  12
     }
 64  
 
 65  
     /**
 66  
      * {@inheritDoc}
 67  
      */
 68  
     @Override
 69  
     public synchronized void setSourceClasses(@SuppressWarnings("rawtypes") Class[] sourceClasses) {
 70  0
         super.setSourceClasses(sourceClasses);
 71  0
     }
 72  
 
 73  
     /**
 74  
      * {@inheritDoc}
 75  
      */
 76  
     @Override
 77  
     protected Class<?>[] getSourceClassesImpl() throws Exception {
 78  12
         Class<?>[] c = getBeanReflector().getReflectableClasses();
 79  12
         Class<?>[] result = new Class[c.length + 1];
 80  12
         System.arraycopy(c, 0, result, 0, c.length);
 81  12
         return result;
 82  
     }
 83  
 
 84  
     /**
 85  
      * {@inheritDoc}
 86  
      */
 87  
     @Override
 88  
     public synchronized void setDestinationClasses(@SuppressWarnings("rawtypes") Class[] destinationClasses) {
 89  0
         super.setDestinationClasses(destinationClasses);
 90  0
     }
 91  
 
 92  
     /**
 93  
      * {@inheritDoc}
 94  
      */
 95  
     @Override
 96  
     protected Class<?>[] getDestinationClassesImpl() throws Exception {
 97  12
         return DEST_CLASSES;
 98  
     }
 99  
 
 100  
     /**
 101  
      * {@inheritDoc}
 102  
      */
 103  
     @Override
 104  
     protected void copyImpl(Object destination, Object source, Locale locale,
 105  
             Integer preferredTransformationType) throws TransformationException {
 106  18
         int size = source == null ? 0 : getBeanReflector().getSize(source);
 107  18
         ((IndexedEntityCollection) destination).setSize(size);
 108  18
         if (size > 0) {
 109  6
             super.copyImpl(destination, source, locale, preferredTransformationType);
 110  
         }
 111  18
     }
 112  
 
 113  
     /**
 114  
      * {@inheritDoc}
 115  
      */
 116  
     @Override
 117  
     protected boolean isAutomaticallyHandlingNulls() {
 118  6
         return false;
 119  
     }
 120  
 }