Coverage Report - org.simpleframework.xml.core.Collector
 
Classes in this File Line Coverage Branch Coverage Complexity
Collector
96%
26/27
75%
6/8
1.556
Collector$1
N/A
N/A
1.556
Collector$Registry
100%
2/2
N/A
1.556
 
 1  
 /*
 2  
  * Collector.java December 2007
 3  
  *
 4  
  * Copyright (C) 2007, 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.Collection;
 22  
 import java.util.Iterator;
 23  
 import java.util.LinkedHashMap;
 24  
 
 25  
 /**
 26  
  * The <code>Collector</code> object is used to store variables for
 27  
  * a deserialized object. Each variable contains the label and value
 28  
  * for a field or method. The <code>Composite</code> object uses
 29  
  * this to store deserialized values before committing them to the
 30  
  * objects methods and fields. 
 31  
  * 
 32  
  * @author Niall Gallagher
 33  
  * 
 34  
  * @see org.simpleframework.xml.core.Composite
 35  
  */
 36  
 class Collector implements Criteria {
 37  
    
 38  
    /**
 39  
     * This is the registry containing all the variables collected.
 40  
     */
 41  
    private final Registry registry;
 42  
    
 43  
    /**
 44  
     * This is the registry that contains variables mapped to paths.
 45  
     */
 46  
    private final Registry alias;
 47  
    
 48  
    /**
 49  
     * Constructor for the <code>Collector</code> object. This is 
 50  
     * used to store variables for an objects fields and methods.
 51  
     * Each variable is stored using the name of the label.
 52  
     */
 53  935502
    public Collector() {
 54  935502
       this.registry = new Registry();
 55  935502
       this.alias = new Registry();
 56  935502
    }
 57  
 
 58  
    /**
 59  
     * This is used to get the <code>Variable</code> that represents
 60  
     * a deserialized object. The variable contains all the meta
 61  
     * data for the field or method and the value that is to be set
 62  
     * on the method or field.
 63  
     * 
 64  
     * @param key this is the key of the variable to be acquired
 65  
     * 
 66  
     * @return this returns the keyed variable if it exists
 67  
     */
 68  
    public Variable get(Object key) {
 69  1242
       return registry.get(key);
 70  
    } 
 71  
    
 72  
    /**
 73  
     * This is used to get the <code>Variable</code> that represents
 74  
     * a deserialized object. The variable contains all the meta
 75  
     * data for the field or method and the value that is to be set
 76  
     * on the method or field.
 77  
     * 
 78  
     * @param label this is the label to resolve the variable with
 79  
     * 
 80  
     * @return this returns the variable associated with the label
 81  
     */
 82  
    public Variable get(Label label) throws Exception {
 83  493583
       if(label != null) {
 84  493583
          Object key = label.getKey();
 85  
          
 86  493583
          return registry.get(key);
 87  
       }
 88  0
       return null;
 89  
    } 
 90  
    
 91  
    /**
 92  
     * This is used to resolve the <code>Variable</code> by using 
 93  
     * the union names of a label. This will also acquire variables
 94  
     * based on the actual name of the variable.
 95  
     * 
 96  
     * @param path this is the path of the variable to be acquired
 97  
     * 
 98  
     * @return this returns the variable mapped to the path
 99  
     */
 100  
    public Variable resolve(String path) {
 101  1174
       return alias.get(path);
 102  
    }
 103  
    
 104  
    /**
 105  
     * This is used to remove the <code>Variable</code> from this
 106  
     * criteria object. When removed, the variable will no longer be
 107  
     * used to set the method or field when the <code>commit</code>
 108  
     * method is invoked.
 109  
     * 
 110  
     * @param key this is the key associated with the variable
 111  
     * 
 112  
     * @return this returns the keyed variable if it exists
 113  
     */
 114  
    public Variable remove(Object key) throws Exception{
 115  544
       return registry.remove(key);
 116  
    }
 117  
    
 118  
    /**
 119  
     * This is used to acquire an iterator over the named variables.
 120  
     * Providing an <code>Iterator</code> allows the criteria to be
 121  
     * used in a for each loop. This is primarily for convenience.
 122  
     * 
 123  
     * @return this returns an iterator of all the variable names
 124  
     */
 125  
    public Iterator<Object> iterator() {
 126  332
       return registry.iterator();
 127  
    }
 128  
    
 129  
    /**
 130  
     * This is used to create a <code>Variable</code> and set it for
 131  
     * this criteria. The variable can be retrieved at a later stage
 132  
     * using the name of the label. This allows for repeat reads as
 133  
     * the variable can be used to acquire the labels converter.
 134  
     * 
 135  
     * @param label this is the label used to create the variable
 136  
     * @param value this is the value of the object to be read
 137  
     */
 138  
    public void set(Label label, Object value) throws Exception {
 139  2495563
       Variable variable = new Variable(label, value);
 140  
 
 141  2495563
       if(label != null) {
 142  2495563
          String[] paths = label.getPaths();
 143  2495563
          Object key = label.getKey();
 144  
          
 145  4992901
          for(String path : paths) {
 146  2497338
             alias.put(path,  variable);
 147  
          }
 148  2495563
          registry.put(key, variable);
 149  
       }
 150  2495563
    }
 151  
    
 152  
    /**
 153  
     * This is used to set the values for the methods and fields of
 154  
     * the specified object. Invoking this performs the population
 155  
     * of an object being deserialized. It ensures that each value 
 156  
     * is set after the XML element has been fully read.
 157  
     * 
 158  
     * @param source this is the object that is to be populated
 159  
     */
 160  
    public void commit(Object source) throws Exception {
 161  712954
       Collection<Variable> set = registry.values();
 162  
       
 163  712954
       for(Variable entry : set) { 
 164  2066682
          Contact contact = entry.getContact();
 165  2066682
          Object value = entry.getValue();
 166  
 
 167  2066682
          contact.set(source, value);
 168  2066682
       }
 169  712954
    }  
 170  
    
 171  
    /**
 172  
     * The <code>Registry</code> object is used to store variables 
 173  
     * for the collector. All variables are stored under its name so
 174  
     * that they can be later retrieved and used to populate the
 175  
     * object when deserialization of all variables has finished.
 176  
     * 
 177  
     * @author Niall Gallagher
 178  
     */
 179  3742008
    private static class Registry extends LinkedHashMap<Object, Variable> {
 180  
       
 181  
       /**
 182  
        * This is used to iterate over the names of the variables
 183  
        * in the registry. This is primarily used for convenience
 184  
        * so that the variables can be acquired in a for each loop.
 185  
        * 
 186  
        * @return an iterator containing the names of the variables
 187  
        */
 188  
       public Iterator<Object> iterator() {
 189  332
          return keySet().iterator();
 190  
       }
 191  
    }
 192  
 }