Coverage Report - org.apache.commons.flatfile.EntityMap
 
Classes in this File Line Coverage Branch Coverage Complexity
EntityMap
94%
49/52
70%
31/44
1.846
EntityMap$1
N/A
N/A
1.846
EntityMap$Child
90%
10/11
50%
2/4
1.846
EntityMap$ChildrenList
80%
16/20
N/A
1.846
EntityMap$ChildrenList$1
100%
5/5
N/A
1.846
 
 1  118
 /*
 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.AbstractList;
 21  
 import java.util.ArrayList;
 22  
 import java.util.Collection;
 23  
 import java.util.Collections;
 24  
 import java.util.Iterator;
 25  
 import java.util.LinkedHashMap;
 26  
 import java.util.List;
 27  
 import java.util.Map;
 28  
 
 29  
 import org.apache.commons.collections4.IteratorUtils;
 30  
 import org.apache.commons.collections4.Transformer;
 31  
 import org.apache.commons.lang3.ArrayUtils;
 32  
 import org.apache.commons.lang3.Validate;
 33  
 
 34  
 /**
 35  
  * Basic implementation of NamedEntityCollection.
 36  
  * @version $Revision: 1766123 $ $Date: 2016-10-21 15:40:45 -0500 (Fri, 21 Oct 2016) $
 37  
  */
 38  410
 public class EntityMap extends EntityCollectionSupport implements NamedEntityCollection {
 39  
 
 40  
     private static final long serialVersionUID = 3162898927743996952L;
 41  
 
 42  3496
     private static class Child implements Serializable {
 43  
 
 44  
         /** Serialization version */
 45  
         private static final long serialVersionUID = 2347729609002856564L;
 46  
 
 47  378
         private final String name;
 48  
         @SuppressWarnings("PMD.UnusedPrivateField") // false positive; this field is used in EntityMap#clone()
 49  1370
         private final Entity entity;
 50  
 
 51  
         /**
 52  
          * Create a new Child instance.
 53  
          * @param name of child
 54  
          * @param entity child
 55  1176
          */
 56  1764
         public Child(String name, Entity entity) {
 57  1764
             this.name = name;
 58  588
             if (entity == null) {
 59  0
                 throw new IllegalArgumentException();
 60  1176
             }
 61  1764
             this.entity = entity;
 62  588
         }
 63  
 
 64  
     }
 65  
 
 66  
     /**
 67  
      * Expose the Child list as List<Entity>. This is necessary because we permit nameless children,
 68  
      * which we want to account for in a layout but have no need of referencing later.
 69  116
      */
 70  
     private static class ChildrenList extends AbstractList<Entity> implements Serializable {
 71  
 
 72  
         private static final long serialVersionUID = -954784669450571284L;
 73  1988
 
 74  2
         private static final Transformer<Child, Entity> CHILD_TO_ENTITY = new Transformer<Child, Entity>() {
 75  
 
 76  1984
             public Entity transform(Child input) {
 77  992
                 return input.entity;
 78  
             }
 79  2
         };
 80  
 
 81  
         private final List<Child> wrapped;
 82  
 
 83  88
         /**
 84  88
          * Create a new {@link ChildrenList} wrapping the specified {@link List}.
 85  88
          * @param wrapped {@link List}
 86  
          */
 87  440
         private ChildrenList(List<Child> wrapped) {
 88  440
             super();
 89  440
             this.wrapped = wrapped;
 90  176
         }
 91  
 
 92  0
         /**
 93  
          * {@inheritDoc}
 94  
          */
 95  
         @Override
 96  0
         public Entity get(int index) {
 97  0
             return CHILD_TO_ENTITY.transform(wrapped.get(index));
 98  
         }
 99  
 
 100  186
         /**
 101  
          * {@inheritDoc}
 102  
          */
 103  
         @Override
 104  558
         public Iterator<Entity> iterator() {
 105  379
             return IteratorUtils.transformedIterator(wrapped.iterator(), CHILD_TO_ENTITY);
 106  
         }
 107  
 
 108  
         @Override
 109  21
         public int size() {
 110  14
             return wrapped.size();
 111  
         }
 112  
 
 113  
     }
 114  
 
 115  
     /** All children */
 116  
     private List<Child> children;
 117  
 
 118  
     /** contains mapped children only */
 119  
     private Map<String, Entity> childMap;
 120  
 
 121  
     private List<Entity> exposeChildrenList;
 122  
 
 123  
     /**
 124  294
      * Add a child.
 125  
      * @param name if {@code null} filler element
 126  294
      * @param child non-null child Entity
 127  88
      */
 128  970
     public synchronized void add(String name, Entity child) {
 129  676
         Validate.notNull(child, "child entity is null");
 130  882
 
 131  1146
         if (children == null) {
 132  440
             children = new ArrayList<Child>();
 133  440
             exposeChildrenList = new ChildrenList(children);
 134  470
             childMap = new LinkedHashMap<String, Entity>();
 135  1143
         }
 136  588
         if (childMap.containsKey(name)) {
 137  294
             throw new IllegalArgumentException("cannot add > 1 child entity '" + name + "'");
 138  1176
         }
 139  1371
         if (name != null) {
 140  522
             childMap.put(name, child);
 141  882
         }
 142  1470
         children.add(new Child(name, child));
 143  588
     }
 144  
 
 145  262
     /**
 146  
      * Get a map of the children.
 147  
      * @return Map<String, Entity>
 148  
      */
 149  786
     public synchronized Map<String, Entity> getChildMap() {
 150  524
         return childMap == null ? Collections.<String, Entity> emptyMap() : Collections.unmodifiableMap(childMap);
 151  
     }
 152  931
 
 153  
     /**
 154  
      * {@inheritDoc}
 155  
      */
 156  2793
     public synchronized String[] getChildNames() {
 157  3724
         return childMap == null ? ArrayUtils.EMPTY_STRING_ARRAY
 158  1862
             : childMap.keySet().toArray(new String[childMap.size()]);
 159  
     }
 160  258
 
 161  
     /**
 162  
      * {@inheritDoc}
 163  
      */
 164  774
     public Entity getChild(String name) {
 165  516
         return getChildMap().get(name);
 166  
     }
 167  0
 
 168  
     /**
 169  
      * {@inheritDoc}
 170  
      */
 171  0
     public boolean hasChild(String name) {
 172  0
         return getChildMap().containsKey(name);
 173  
     }
 174  193
 
 175  
     /**
 176  
      * {@inheritDoc}
 177  
      */
 178  579
     public synchronized Collection<Entity> getChildren() {
 179  386
         return children == null ? Collections.<Entity> emptyList() : Collections.unmodifiableList(exposeChildrenList);
 180  
     }
 181  
 
 182  59
     /**
 183  59
      * {@inheritDoc}
 184  59
      */
 185  59
     @Override
 186  236
     public synchronized EntityMap clone() {
 187  354
         EntityMap result = (EntityMap) super.clone();
 188  484
         result.children = null;
 189  484
         result.childMap = null;
 190  295
         result.exposeChildrenList = null;
 191  354
         if (children != null) {
 192  1181
             for (Child child : children) {
 193  945
                 result.add(child.name, child.entity.clone());
 194  
             }
 195  177
         }
 196  118
         return result;
 197  
     }
 198  
 }