Coverage Report - org.simpleframework.xml.core.ModelMap
 
Classes in this File Line Coverage Branch Coverage Complexity
ModelMap
95%
23/24
80%
8/10
2.4
 
 1  
 /*
 2  
  * ModelMap.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.Iterator;
 22  
 import java.util.LinkedHashMap;
 23  
 
 24  
 /**
 25  
  * The <code>ModelMap</code> object represents a map that contains 
 26  
  * string model mappings. This is used for convenience as a typedef
 27  
  * like construct to avoid having declare the generic type whenever
 28  
  * it is referenced. Also this allows <code>ModelList</code> values 
 29  
  * from the map to be iterated within for each loops.
 30  
  * 
 31  
  * @author Niall Gallagher
 32  
  *
 33  
  * @see org.simpleframework.xml.core.Model
 34  
  * @see org.simpleframework.xml.core.ModelList
 35  
  */
 36  
 class ModelMap extends LinkedHashMap<String, ModelList> implements Iterable<ModelList>{
 37  
    
 38  
    /**
 39  
     * This is the detail associated with this model map instance.
 40  
     */
 41  
    private final Detail detail;
 42  
    
 43  
    /**
 44  
     * Constructor for the <code>ModelMap</code> object is used to 
 45  
     * create an empty map. This is used for convenience as a typedef
 46  
     * like construct which avoids having to use the generic type.
 47  
     *
 48  
     * @param detail this is the detail associated with the map
 49  
     */ 
 50  933578
    public ModelMap(Detail detail) {
 51  933578
       this.detail = detail;
 52  933578
    }
 53  
    
 54  
    /**
 55  
     * This method is used to clone the model map such that mappings
 56  
     * can be maintained in the original even if they are modified
 57  
     * in the clone. This is used to that the <code>Schema</code> can
 58  
     * remove mappings from the model map as they are visited. 
 59  
     *
 60  
     * @return this returns a cloned representation of this map
 61  
     */
 62  
    public ModelMap getModels() throws Exception {
 63  930837
       ModelMap map = new ModelMap(detail);
 64  
       
 65  930837
       for(String name : keySet()) {
 66  935
          ModelList list = get(name);
 67  
          
 68  935
          if(list != null) {
 69  935
             list = list.build();
 70  
          }
 71  935
          if(map.containsKey(name)) {
 72  0
             throw new PathException("Path with name '%s' is a duplicate in %s ", name, detail);
 73  
          }
 74  935
          map.put(name, list);
 75  935
       }         
 76  930837
       return map;    
 77  
    }
 78  
    
 79  
    /**
 80  
     * This method is used to look for a <code>Model</code> that
 81  
     * matches the specified element name. If no such model exists
 82  
     * then this will return null. This is as a convenient way
 83  
     * to find a model within the tree of models being built.
 84  
     * 
 85  
     * @param name this is the name of the model to be acquired
 86  
     * @param index this is the index used to order the model
 87  
     * 
 88  
     * @return this returns the model located by the expression
 89  
     */
 90  
    public Model lookup(String name, int index) {
 91  1535
       ModelList list = get(name);
 92  
       
 93  1535
       if(list != null) {
 94  1092
          return list.lookup(index);
 95  
       }
 96  443
       return null;
 97  
    }
 98  
    
 99  
    /**
 100  
     * This is used to register a <code>Model</code> within this
 101  
     * map. Registration of a model creates a tree of models that
 102  
     * can be used to represent an XML structure. Each model can
 103  
     * contain elements and attributes associated with a type.
 104  
     * 
 105  
     * @param name this is the name of the model to be registered
 106  
     * @param model this is the model that is to be registered
 107  
     */
 108  
    public void register(String name, Model model) {
 109  362
       ModelList list = get(name);
 110  
       
 111  362
       if(list == null) {
 112  327
          list = new ModelList();
 113  327
          put(name, list);
 114  
       }
 115  362
       list.register(model);      
 116  362
    }
 117  
    
 118  
    /**
 119  
     * This allows the <code>ModelList</code> objects within the model 
 120  
     * map to be iterated within for each loops. This will provide all
 121  
     * remaining model objects within the map. The iteration order is
 122  
     * not maintained so model objects may be given in any sequence.
 123  
     *
 124  
     * @return this returns an iterator for existing model objects
 125  
     */ 
 126  
    public Iterator<ModelList> iterator() {
 127  4848
       return values().iterator(); 
 128  
    }
 129  
 }