Coverage Report - org.apache.commons.flatfile.EntityArray
 
Classes in this File Line Coverage Branch Coverage Complexity
EntityArray
78%
56/72
78%
31/40
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.util.ArrayList;
 20  
 import java.util.Collection;
 21  
 import java.util.Collections;
 22  
 import java.util.List;
 23  
 
 24  
 /**
 25  
  * Entity collection with the following approach:  a size (number of children)
 26  
  * and a prototypical child are assigned.  The prototype child is cloned
 27  
  * <code>size</code> times, and these clones become the indexed child entities.
 28  
  */
 29  56
 public class EntityArray extends EntityCollectionSupport implements
 30  
         IndexedEntityCollection {
 31  
     private static final long serialVersionUID = 8716199462287161060L;
 32  38
     private int size = -1;
 33  38
     private int minimumSize = 0;
 34  38
     private int maximumSize = Integer.MAX_VALUE;
 35  38
     private boolean resizable = false;
 36  
     private List<Entity> children;
 37  
     private Entity prototype;
 38  
 
 39  
     /**
 40  
      * Create a new EntityArray.
 41  
      */
 42  0
     public EntityArray() {
 43  
         // default constructor
 44  0
     }
 45  
 
 46  
     /**
 47  
      * Create a new IndexedEntityCollection.
 48  
      * @param size number of occurrences of the prototypical Entity
 49  
      */
 50  0
     public EntityArray(int size) {
 51  0
         setSize(size);
 52  0
     }
 53  
 
 54  
     /**
 55  
      * Create a new EntityArray with the specified prototype.
 56  
      * @param prototype Entity
 57  
      */
 58  38
     public EntityArray(Entity prototype) {
 59  38
         setPrototype(prototype);
 60  38
     }
 61  
 
 62  
     /**
 63  
      * Create a new IndexedEntityCollection.
 64  
      * @param prototype Entity
 65  
      * @param size number of occurrences
 66  
      */
 67  
     public EntityArray(Entity prototype, int size) {
 68  0
         this(prototype);
 69  0
         setSize(size);
 70  0
     }
 71  
 
 72  
     /**
 73  
      * Get the child at the specified index.
 74  
      * @param index to return
 75  
      * @return Entity
 76  
      */
 77  
     public Entity getChild(int index) {
 78  49
         initialize();
 79  49
         return (Entity) children.get(index);
 80  
     }
 81  
 
 82  
     /**
 83  
      * {@inheritDoc}
 84  
      */
 85  
     public synchronized Collection<Entity> getChildren() {
 86  115
         initialize();
 87  114
         return Collections.unmodifiableCollection(children);
 88  
     }
 89  
 
 90  
     /**
 91  
      * Get the prototype.
 92  
      * @return Entity.
 93  
      */
 94  
     public synchronized Entity getPrototype() {
 95  12
         return prototype;
 96  
     }
 97  
 
 98  
     /**
 99  
      * Set the prototype.
 100  
      * @param prototype The Entity prototype to set.
 101  
      */
 102  
     public synchronized void setPrototype(Entity prototype) {
 103  38
         if (prototype == null) {
 104  0
             throw new IllegalArgumentException("prototype Entity was null");
 105  
         }
 106  38
         this.prototype = prototype;
 107  38
     }
 108  
 
 109  
     /**
 110  
      * {@inheritDoc}
 111  
      * @throws IllegalArgumentException if <code>size</code> is not valid.
 112  
      */
 113  
     public synchronized void setSize(int size) {
 114  46
         if (this.size >= 0 && !isResizable()) {
 115  2
             if (size == this.size()) {
 116  1
                 return;
 117  
             }
 118  1
             throw new IllegalStateException("size already set");
 119  
         }
 120  44
         if (size < minimumSize || size > maximumSize) {
 121  3
             throw new IllegalArgumentException("illegal collection size: "
 122  
                     + size + "; should be <= " + maximumSize + " and >= "
 123  
                     + minimumSize);
 124  
         }
 125  41
         this.size = size;
 126  41
     }
 127  
 
 128  
     /**
 129  
      * Get the size (# of child elements) of this IndexedEntityCollection.
 130  
      * @return int
 131  
      */
 132  
     public synchronized int size() {
 133  122
         return size;
 134  
     }
 135  
 
 136  
     /**
 137  
      * Learn whether this EntityArray's size can be set.
 138  
      * @return boolean
 139  
      */
 140  
     public synchronized boolean isSizable() {
 141  5
         return size == -1 || isResizable();
 142  
     }
 143  
 
 144  
     /**
 145  
      * Clone this EntityArray.
 146  
      * @return new EntityArray
 147  
      */
 148  
     @Override
 149  
     public synchronized EntityArray clone() {
 150  56
         EntityArray result = (EntityArray) super.clone();
 151  56
         if (children != null) {
 152  16
             result.children = new ArrayList<Entity>();
 153  16
             for (Entity e : children) {
 154  349
                 result.children.add(e.clone());
 155  
             }
 156  
         }
 157  56
         return result;
 158  
     }
 159  
 
 160  
     /**
 161  
      * Initialize this EntityArray.
 162  
      */
 163  
     private synchronized void initialize() {
 164  164
         if (children != null) {
 165  121
             return;
 166  
         }
 167  43
         if (size < 0) {
 168  1
             throw new IllegalStateException("EntityArray size not set");
 169  
         }
 170  42
         if (size == 0) {
 171  4
             children = Collections.<Entity>emptyList();
 172  4
             return;
 173  
         }
 174  38
         if (prototype == null) {
 175  0
             throw new IllegalStateException("Prototype child entity not set");
 176  
         }
 177  38
         children = new ArrayList<Entity>(size);
 178  38
         adjustSize();
 179  38
     }
 180  
 
 181  
     /**
 182  
      * Adjust the size of this EntityArray.
 183  
      */
 184  
     private synchronized void adjustSize() {
 185  38
         if (children.size() > size) {
 186  0
             children.subList(size, children.size()).clear();
 187  0
             return;
 188  
         }
 189  360
         for (int i = children.size(); i < size; i++) {
 190  322
             children.add(prototype.clone());
 191  
         }
 192  38
     }
 193  
 
 194  
     /**
 195  
      * Get the int maximumSize.
 196  
      * @return int
 197  
      */
 198  
     public synchronized int getMaximumSize() {
 199  5
         return maximumSize;
 200  
     }
 201  
 
 202  
     /**
 203  
      * Set the int maximumSize.
 204  
      * @param maximumSize int
 205  
      */
 206  
     public synchronized void setMaximumSize(int maximumSize) {
 207  3
         if (maximumSize > Integer.MAX_VALUE || maximumSize < getMinimumSize()) {
 208  0
             throw new IllegalArgumentException(Integer.toString(maximumSize));
 209  
         }
 210  3
         this.maximumSize = maximumSize;
 211  3
     }
 212  
 
 213  
     /**
 214  
      * Get the int minimumSize.
 215  
      * @return int
 216  
      */
 217  
     public synchronized int getMinimumSize() {
 218  5
         return minimumSize;
 219  
     }
 220  
 
 221  
     /**
 222  
      * Set the int minimumSize.
 223  
      * @param minimumSize int
 224  
      */
 225  
     public synchronized void setMinimumSize(int minimumSize) {
 226  3
         if (minimumSize < 0 || minimumSize > getMaximumSize()) {
 227  0
             throw new IllegalArgumentException(Integer.toString(minimumSize));
 228  
         }
 229  3
         this.minimumSize = minimumSize;
 230  3
     }
 231  
 
 232  
     /**
 233  
      * Get the boolean resizable.
 234  
      * @return boolean
 235  
      */
 236  
     public boolean isResizable() {
 237  3
         return resizable;
 238  
     }
 239  
 
 240  
     /**
 241  
      * Set the boolean resizable.
 242  
      * @param resizable boolean
 243  
      */
 244  
     public void setResizable(boolean resizable) {
 245  0
         this.resizable = resizable;
 246  0
     }
 247  
 
 248  
 }