Coverage Report - org.simpleframework.xml.util.Dictionary
 
Classes in this File Line Coverage Branch Coverage Complexity
Dictionary
89%
8/9
100%
1/1
0
Dictionary$Table
100%
3/3
N/A
0
 
 1  
 /*
 2  
  * Dictionary.java July 2006
 3  
  *
 4  
  * Copyright (C) 2006, Niall Gallagher <niallg@users.sf.net>
 5  
  *
 6  
  * This library is free software; you can redistribute it and/or
 7  
  * modify it under the terms of the GNU Lesser General Public
 8  
  * License as published by the Free Software Foundation.
 9  
  *
 10  
  * This library is distributed in the hope that it will be useful,
 11  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 12  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 13  
  * GNU Lesser General Public License for more details.
 14  
  *
 15  
  * You should have received a copy of the GNU Lesser General 
 16  
  * Public License along with this library; if not, write to the 
 17  
  * Free Software Foundation, Inc., 59 Temple Place, Suite 330, 
 18  
  * Boston, MA  02111-1307  USA
 19  
  */
 20  
 
 21  
 package org.simpleframework.xml.util;
 22  
 
 23  
 import java.util.AbstractSet;
 24  
 import java.util.HashMap;
 25  
 import java.util.Iterator;
 26  
 
 27  
 /**
 28  
  * The <code>Dictionary</code> object represents a mapped set of entry
 29  
  * objects that can be serialized and deserialized. This is used when
 30  
  * there is a need to load a list of objects that can be mapped using 
 31  
  * a name attribute. Using this object avoids the need to implement a
 32  
  * commonly required pattern of building a map of XML element objects.
 33  
  * <pre>
 34  
  *
 35  
  *    &lt;dictionary&gt;
 36  
  *       &lt;entry name="example"&gt;
 37  
  *          &lt;element&gt;example text&lt;/element&gt;
 38  
  *       &lt;/entry&gt;
 39  
  *       &lt;entry name="example"&gt;
 40  
  *          &lt;element&gt;example text&lt;/element&gt;
 41  
  *       &lt;/entry&gt;       
 42  
  *    &lt;/dictionary&gt;
 43  
  * 
 44  
  * </pre>
 45  
  * This can contain implementations of the <code>Entry</code> object 
 46  
  * which contains a required "name" attribute. Implementations of the
 47  
  * entry object can add further XML attributes an elements. This must
 48  
  * be annotated with the <code>ElementList</code> annotation in order
 49  
  * to be serialized and deserialized as an object field.
 50  
  * 
 51  
  * @author Niall Gallagher
 52  
  *
 53  
  * @see org.simpleframework.xml.util.Entry
 54  
  */ 
 55  138
 public class Dictionary<E extends Entry> extends AbstractSet<E> {
 56  
 
 57  
    /**
 58  
     * Used to map the entries to their configured names.
 59  
     */         
 60  
    protected Table map;
 61  
         
 62  
    /**
 63  
     * Constructor for the <code>Dictionary</code> object. This 
 64  
     * is used to create a set that contains entry objects mapped 
 65  
     * to an XML attribute name value. Entry objects added to this
 66  
     * dictionary can be retrieved using its name value.
 67  
     */ 
 68  8
    public Dictionary() {
 69  8
       this.map = new Table();           
 70  8
    }
 71  
 
 72  
    /**
 73  
     * This method is used to add the provided entry to this set. If
 74  
     * an entry of the same name already existed within the set then
 75  
     * it is replaced with the specified <code>Entry</code> object.
 76  
     * 
 77  
     * @param item this is the entry object that is to be inserted
 78  
     */ 
 79  
    public boolean add(E item) {
 80  138
       return map.put(item.name, item) != null;           
 81  
    }
 82  
 
 83  
    /**
 84  
     * This returns the number of <code>Entry</code> objects within
 85  
     * the dictionary. This will use the internal map to acquire the
 86  
     * number of entry objects that have been inserted to the map.
 87  
     *
 88  
     * @return this returns the number of entry objects in the set
 89  
     */ 
 90  
    public int size() {
 91  1
       return map.size();            
 92  
    }
 93  
 
 94  
    /**
 95  
     * Returns an iterator of <code>Entry</code> objects which can be
 96  
     * used to remove items from this set. This will use the internal
 97  
     * map object and return the iterator for the map values.
 98  
     * 
 99  
     * @return this returns an iterator for the entry objects
 100  
     */ 
 101  
    public Iterator<E> iterator() {
 102  9
       return map.values().iterator();            
 103  
    }
 104  
 
 105  
    /**
 106  
     * This is used to acquire an <code>Entry</code> from the set by
 107  
     * its name. This uses the internal map to look for the entry, if
 108  
     * the entry exists it is returned, if not this returns null.
 109  
     * 
 110  
     * @param name this is the name of the entry object to retrieve
 111  
     *
 112  
     * @return this returns the entry mapped to the specified name
 113  
     */ 
 114  
    public E get(String name) {
 115  144
       return map.get(name);           
 116  
    }
 117  
 
 118  
    /**
 119  
     * This is used to remove an <code>Entry</code> from the set by
 120  
     * its name. This uses the internal map to look for the entry, if
 121  
     * the entry exists it is returned and removed from the map.
 122  
     * 
 123  
     * @param name this is the name of the entry object to remove
 124  
     *
 125  
     * @return this returns the entry mapped to the specified name
 126  
     */ 
 127  
    public E remove(String name) {
 128  0
       return map.remove(name);           
 129  
    }
 130  
  
 131  
    /**
 132  
     * The <code>Table</code> object is used to represent a map of
 133  
     * entries mapped to a string name. Each implementation of the
 134  
     * entry must contain a name attribute, which is used to insert
 135  
     * the entry into the map. This acts as a typedef.
 136  
     *
 137  
     * @see org.simpleframework.xml.util.Entry
 138  
     */
 139  
    private class Table extends HashMap<String, E> {
 140  
       
 141  
       /**
 142  
        * Constructor for the <code>Table</code> object. This will
 143  
        * create a map that is used to store the entry objects that
 144  
        * are serialized and deserialized to and from an XML source.
 145  
        */
 146  8
       public Table() {
 147  8
          super();
 148  8
       }         
 149  
    }     
 150  
 }