Coverage Report - org.simpleframework.xml.util.Dictionary
 
Classes in this File Line Coverage Branch Coverage Complexity
Dictionary
88%
8/9
50%
1/2
1
Dictionary$Table
100%
2/2
N/A
1
 
 1  
 /*
 2  
  * Dictionary.java July 2006
 3  
  *
 4  
  * Copyright (C) 2006, 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.util;
 20  
 
 21  
 import java.util.AbstractSet;
 22  
 import java.util.HashMap;
 23  
 import java.util.Iterator;
 24  
 
 25  
 /**
 26  
  * The <code>Dictionary</code> object represents a mapped set of entry
 27  
  * objects that can be serialized and deserialized. This is used when
 28  
  * there is a need to load a list of objects that can be mapped using 
 29  
  * a name attribute. Using this object avoids the need to implement a
 30  
  * commonly required pattern of building a map of XML element objects.
 31  
  * <pre>
 32  
  *
 33  
  *    &lt;dictionary&gt;
 34  
  *       &lt;entry name="example"&gt;
 35  
  *          &lt;element&gt;example text&lt;/element&gt;
 36  
  *       &lt;/entry&gt;
 37  
  *       &lt;entry name="example"&gt;
 38  
  *          &lt;element&gt;example text&lt;/element&gt;
 39  
  *       &lt;/entry&gt;       
 40  
  *    &lt;/dictionary&gt;
 41  
  * 
 42  
  * </pre>
 43  
  * This can contain implementations of the <code>Entry</code> object 
 44  
  * which contains a required "name" attribute. Implementations of the
 45  
  * entry object can add further XML attributes an elements. This must
 46  
  * be annotated with the <code>ElementList</code> annotation in order
 47  
  * to be serialized and deserialized as an object field.
 48  
  * 
 49  
  * @author Niall Gallagher
 50  
  *
 51  
  * @see org.simpleframework.xml.util.Entry
 52  
  */ 
 53  590
 public class Dictionary<T extends Entry> extends AbstractSet<T> {
 54  
 
 55  
    /**
 56  
     * Used to map the entries to their configured names.
 57  
     */         
 58  
    protected final Table<T> map;
 59  
         
 60  
    /**
 61  
     * Constructor for the <code>Dictionary</code> object. This 
 62  
     * is used to create a set that contains entry objects mapped 
 63  
     * to an XML attribute name value. Entry objects added to this
 64  
     * dictionary can be retrieved using its name value.
 65  
     */ 
 66  34
    public Dictionary() {
 67  34
       this.map = new Table<T>();           
 68  34
    }
 69  
 
 70  
    /**
 71  
     * This method is used to add the provided entry to this set. If
 72  
     * an entry of the same name already existed within the set then
 73  
     * it is replaced with the specified <code>Entry</code> object.
 74  
     * 
 75  
     * @param item this is the entry object that is to be inserted
 76  
     */ 
 77  
    public boolean add(T item) {
 78  594
       return map.put(item.getName(), item) != null;           
 79  
    }
 80  
 
 81  
    /**
 82  
     * This returns the number of <code>Entry</code> objects within
 83  
     * the dictionary. This will use the internal map to acquire the
 84  
     * number of entry objects that have been inserted to the map.
 85  
     *
 86  
     * @return this returns the number of entry objects in the set
 87  
     */ 
 88  
    public int size() {
 89  1
       return map.size();            
 90  
    }
 91  
 
 92  
    /**
 93  
     * Returns an iterator of <code>Entry</code> objects which can be
 94  
     * used to remove items from this set. This will use the internal
 95  
     * map object and return the iterator for the map values.
 96  
     * 
 97  
     * @return this returns an iterator for the entry objects
 98  
     */ 
 99  
    public Iterator<T> iterator() {
 100  52
       return map.values().iterator();            
 101  
    }
 102  
 
 103  
    /**
 104  
     * This is used to acquire an <code>Entry</code> from the set by
 105  
     * its name. This uses the internal map to look for the entry, if
 106  
     * the entry exists it is returned, if not this returns null.
 107  
     * 
 108  
     * @param name this is the name of the entry object to retrieve
 109  
     *
 110  
     * @return this returns the entry mapped to the specified name
 111  
     */ 
 112  
    public T get(String name) {
 113  203
       return map.get(name);           
 114  
    }
 115  
 
 116  
    /**
 117  
     * This is used to remove an <code>Entry</code> from the set by
 118  
     * its name. This uses the internal map to look for the entry, if
 119  
     * the entry exists it is returned and removed from the map.
 120  
     * 
 121  
     * @param name this is the name of the entry object to remove
 122  
     *
 123  
     * @return this returns the entry mapped to the specified name
 124  
     */ 
 125  
    public T remove(String name) {
 126  0
       return map.remove(name);           
 127  
    }
 128  
  
 129  
    /**
 130  
     * The <code>Table</code> object is used to represent a map of
 131  
     * entries mapped to a string name. Each implementation of the
 132  
     * entry must contain a name attribute, which is used to insert
 133  
     * the entry into the map. This acts as a typedef.
 134  
     *
 135  
     * @see org.simpleframework.xml.util.Entry
 136  
     */
 137  
    private static class Table<T> extends HashMap<String, T> {
 138  
       
 139  
       /**
 140  
        * Constructor for the <code>Table</code> object. This will
 141  
        * create a map that is used to store the entry objects that
 142  
        * are serialized and deserialized to and from an XML source.
 143  
        */
 144  
       public Table() {
 145  34
          super();
 146  34
       }         
 147  
    }     
 148  
 }