Coverage Report - org.apache.commons.flatfile.EntityMap
 
Classes in this File Line Coverage Branch Coverage Complexity
EntityMap
89%
24/27
67%
12/18
0
EntityMap$1
N/A
N/A
0
EntityMap$Child
86%
6/7
50%
1/2
0
EntityMap$ChildrenList
86%
6/7
N/A
0
EntityMap$ChildrenList$1
100%
2/2
N/A
0
 
 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;
 18  
 
 19  
 import java.io.Serializable;
 20  
 import java.util.ArrayList;
 21  
 import java.util.Collection;
 22  
 import java.util.Collections;
 23  
 import java.util.Iterator;
 24  
 import java.util.LinkedHashMap;
 25  
 import java.util.List;
 26  
 import java.util.Map;
 27  
 
 28  
 import org.apache.commons.collections.IteratorUtils;
 29  
 import org.apache.commons.collections.Transformer;
 30  
 import org.apache.commons.collections.list.AbstractSerializableListDecorator;
 31  
 
 32  
 import org.apache.commons.lang.ArrayUtils;
 33  
 
 34  
 /**
 35  
  * Basic implementation of NamedEntityCollection.
 36  
  * @version $Revision: 763974 $ $Date: 2009-04-10 11:23:57 -0500 (Fri, 10 Apr 2009) $
 37  
  */
 38  176
 public class EntityMap extends EntityCollectionSupport implements NamedEntityCollection {
 39  
 
 40  
     private static final long serialVersionUID = 3162898927743996952L;
 41  
 
 42  874
     private class Child implements Serializable {
 43  
 
 44  
         /** Serialization version */
 45  
         private static final long serialVersionUID = 2347729609002856564L;
 46  
 
 47  
         private String name;
 48  
         private Entity entity;
 49  
 
 50  
         /**
 51  
          * Create a new Child instance.
 52  
          * @param name of child
 53  
          * @param entity child
 54  
          */
 55  294
         public Child(String name, Entity entity) {
 56  294
             this.name = name;
 57  294
             if (entity == null) {
 58  0
                 throw new IllegalArgumentException();
 59  
             }
 60  294
             this.entity = entity;
 61  294
         }
 62  
 
 63  
     }
 64  
 
 65  
     /**
 66  
      * Expose the Child list as List<Entity>.
 67  
      */
 68  
     @SuppressWarnings("unchecked")
 69  88
     private class ChildrenList extends AbstractSerializableListDecorator {
 70  
         private static final long serialVersionUID = 3241482030276238909L;
 71  
 
 72  88
         private Transformer t = new Transformer() {
 73  88
             public Object transform(Object o) {
 74  496
                 return ((Child) o).entity;
 75  
             }
 76  
         };
 77  
 
 78  
         /**
 79  
          * Create our ChildrenList instance.
 80  
          */
 81  88
         private ChildrenList() {
 82  88
             super(children);
 83  88
         }
 84  
 
 85  
         /**
 86  
          * {@inheritDoc}
 87  
          */
 88  
         public Entity get(int index) {
 89  0
             return (Entity) t.transform(super.get(index));
 90  
         }
 91  
 
 92  
         /**
 93  
          * {@inheritDoc}
 94  
          */
 95  
         public Iterator<Entity> iterator() {
 96  186
             return IteratorUtils.transformedIterator(super.iterator(), t);
 97  
         }
 98  
 
 99  
     }
 100  
 
 101  
     /** All children */
 102  
     private List<Child> children;
 103  
 
 104  
     /** contains mapped children only */
 105  
     private Map<String, Entity> childMap;
 106  
 
 107  
     private List<Entity> exposeChildrenList;
 108  
 
 109  
     /**
 110  
      * Add a child.
 111  
      * @param name if null filler element
 112  
      * @param child non-null child Entity
 113  
      */
 114  
     @SuppressWarnings("unchecked")
 115  
     public synchronized void add(String name, Entity child) {
 116  294
         if (child == null) {
 117  0
             throw new IllegalArgumentException("child entity is null");
 118  
         }
 119  294
         if (children == null) {
 120  88
             children = new ArrayList<Child>();
 121  88
             exposeChildrenList = new ChildrenList();
 122  88
             childMap = new LinkedHashMap<String, Entity>();
 123  
         }
 124  294
         if (childMap.containsKey(name)) {
 125  0
             throw new IllegalArgumentException("cannot add > 1 child entity '" + name + "'");
 126  
         }
 127  294
         if (name != null) {
 128  261
             childMap.put(name, child);
 129  
         }
 130  294
         children.add(new Child(name, child));
 131  294
     }
 132  
 
 133  
     /**
 134  
      * Get a map of the children.
 135  
      * @return Map<String, Entity>
 136  
      */
 137  
     public synchronized Map<String, Entity> getChildMap() {
 138  262
         return childMap == null ? Collections.<String, Entity> emptyMap() : Collections
 139  
                 .unmodifiableMap(childMap);
 140  
     }
 141  
 
 142  
     /**
 143  
      * {@inheritDoc}
 144  
      * @see com.pgac.flatfile.NamedEntityCollection#getChildNames()
 145  
      */
 146  
     public synchronized String[] getChildNames() {
 147  931
         return childMap == null ? ArrayUtils.EMPTY_STRING_ARRAY : childMap.keySet().toArray(
 148  
                 new String[childMap.size()]);
 149  
     }
 150  
 
 151  
     /**
 152  
      * {@inheritDoc}
 153  
      * @see com.pgac.flatfile.NamedEntityCollection#getChild(java.lang.String)
 154  
      */
 155  
     public Entity getChild(String name) {
 156  258
         return getChildMap().get(name);
 157  
     }
 158  
 
 159  
     /**
 160  
      * {@inheritDoc}
 161  
      * @see com.pgac.flatfile.NamedEntityCollection#hasChild(java.lang.String)
 162  
      */
 163  
     public boolean hasChild(String name) {
 164  0
         return getChildMap().containsKey(name);
 165  
     }
 166  
 
 167  
     /**
 168  
      * {@inheritDoc}
 169  
      * @see com.pgac.flatfile.EntityCollection#getChildren()
 170  
      */
 171  
     public synchronized Collection<Entity> getChildren() {
 172  193
         return children == null ? Collections.<Entity> emptyList() : Collections
 173  
                 .unmodifiableList(exposeChildrenList);
 174  
     }
 175  
 
 176  
     /**
 177  
      * {@inheritDoc}
 178  
      * @see com.pgac.flatfile.EntitySupport#clone()
 179  
      */
 180  
     public synchronized EntityMap clone() {
 181  59
         EntityMap result = (EntityMap) super.clone();
 182  59
         result.children = null;
 183  59
         result.childMap = null;
 184  59
         result.exposeChildrenList = null;
 185  59
         if (children != null) {
 186  59
             for (Child child : children) {
 187  189
                 result.add(child.name, child.entity.clone());
 188  
             }
 189  
         }
 190  59
         return result;
 191  
     }
 192  
 }