Coverage Report - org.simpleframework.xml.core.ModelList
 
Classes in this File Line Coverage Branch Coverage Complexity
ModelList
96%
28/29
90%
18/20
3.167
 
 1  
 /*
 2  
  * ModelList.java November 2010
 3  
  *
 4  
  * Copyright (C) 2010, Niall Gallagher <niallg@users.sf.net>
 5  
  *
 6  
  * Licensed under the Apache License, Version 2.0 (the "License");
 7  
  * you may not use this file except in compliance with the License.
 8  
  * You may obtain a copy of the License at
 9  
  *
 10  
  *     http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing, software
 13  
  * distributed under the License is distributed on an "AS IS" BASIS,
 14  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 
 15  
  * implied. See the License for the specific language governing 
 16  
  * permissions and limitations under the License.
 17  
  */
 18  
 
 19  
 package org.simpleframework.xml.core;
 20  
 
 21  
 import java.util.ArrayList;
 22  
 
 23  
 /**
 24  
  * The <code>ModelList</code> object is used to maintain an ordered
 25  
  * list of models. Models are maintained within the list in an 
 26  
  * sequenced manner, ordered by the index of the model. During the
 27  
  * building process models can be registered in any order, however
 28  
  * once building has finished the list must contain a complete
 29  
  * sequence of models, ordered by index.
 30  
  * 
 31  
  * @author Niall Gallagher
 32  
  */
 33  
 class ModelList extends ArrayList<Model> {
 34  
    
 35  
    /**
 36  
     * Constructor for the <code>ModelList</code> object. This is
 37  
     * used to construct a linked list that can take registrations
 38  
     * of <code>Model</code> objects out of sequence. Once complete
 39  
     * the list should contain a full ordered set of models.
 40  
     */
 41  
    public ModelList() {
 42  1262
       super();
 43  1262
    }
 44  
    
 45  
    /**
 46  
     * This is used when building a copy of the model. A copy is
 47  
     * required when serializing or deserializing so that the list
 48  
     * remains intact for the next time it is used  
 49  
     * 
 50  
     * @return this returns an exact copy of the model list
 51  
     */
 52  
    public ModelList build() {
 53  935
       ModelList list = new ModelList();
 54  
       
 55  935
       for(Model model : this) {
 56  1027
          list.register(model);
 57  
       }
 58  935
       return list;
 59  
    }
 60  
    
 61  
    /**
 62  
     * This is used to determine if the model list is empty. The
 63  
     * model list is considered empty if it does not contain any
 64  
     * models with element or attribute registrations. This does
 65  
     * not mean that the list itself does not contain models.
 66  
     * 
 67  
     * @return this returns true if there are no registrations
 68  
     */
 69  
    public boolean isEmpty() {
 70  1088
       for(Model model : this) {
 71  1086
          if(model != null) {
 72  1086
             if(!model.isEmpty()) {
 73  794
                return false;
 74  
             }
 75  
          }
 76  
       }
 77  294
       return true;
 78  
    }
 79  
    
 80  
    /**
 81  
     * This is used to find a model based on its index. If there
 82  
     * are no models at the specified index this will return 
 83  
     * null. Unlike the get method this does not throw exceptions.
 84  
     * 
 85  
     * @param index this is the index to acquire the model at
 86  
     * 
 87  
     * @return this returns the model if one exists at the index
 88  
     */
 89  
    public Model lookup(int index) {
 90  1092
       int size = size();
 91  
       
 92  1092
       if(index <= size) {
 93  1036
          return get(index-1);
 94  
       }
 95  56
       return null;
 96  
    }
 97  
    
 98  
    /**
 99  
     * This is used to register the model within the list. The model
 100  
     * is registered at the index provided. If the registration is
 101  
     * out of sequence all indexes that do not have models are
 102  
     * populated with null values to ensure each model resides in
 103  
     * its index position within the list.
 104  
     * 
 105  
     * @param model the model to be registered within the list
 106  
     */
 107  
    public void register(Model model) {
 108  1389
       int index = model.getIndex();
 109  1389
       int size = size(); 
 110  
 
 111  2906
       for(int i = 0; i < index; i++) {
 112  1517
          if(i >= size) {
 113  1390
             add(null); 
 114  
          }
 115  1517
          if(i == index -1) {           
 116  1389
             set(index-1, model);
 117  
          } 
 118  
       }      
 119  1389
    }
 120  
    
 121  
    /**
 122  
     * This is used to take the models from the model list at the
 123  
     * first index. This is used when iterating over the models
 124  
     * to ensure there is only ever one visit of a specific model
 125  
     * 
 126  
     * @return this returns the next model in the sequence
 127  
     */
 128  
    public Model take() {
 129  1015
       while(!isEmpty()) {
 130  793
          Model model = remove(0);         
 131  
          
 132  793
          if(!model.isEmpty()) {
 133  793
             return model;
 134  
          }
 135  0
       }
 136  222
       return null;
 137  
    }
 138  
 }