Coverage Report - org.simpleframework.xml.core.Session
 
Classes in this File Line Coverage Branch Coverage Complexity
Session
50%
11/22
N/A
1
 
 1  
 /*
 2  
  * Session.java February 2005
 3  
  *
 4  
  * Copyright (C) 2005, 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.HashMap;
 23  
 import java.util.Map;
 24  
 import java.util.Set;
 25  
 
 26  
 /**
 27  
  * The <code>Session</code> object represents a session with name
 28  
  * value pairs. The persister uses this to allow objects to add
 29  
  * or remove name value pairs to an from an internal map. This is
 30  
  * done so that the deserialized objects can set template values
 31  
  * as well as share information. In particular this is useful for
 32  
  * any <code>Strategy</code> implementation as it allows it so
 33  
  * store persistence state during the persistence process.
 34  
  * <p>
 35  
  * Another important reason for the session map is that it is
 36  
  * used to wrap the map that is handed to objects during callback
 37  
  * methods. This opens the possibility for those objects to grab
 38  
  * a reference to the map, which will cause problems for any of
 39  
  * the strategy implementations that wanted to use the session
 40  
  * reference for weakly storing persistence artifacts.
 41  
  * 
 42  
  * @author Niall Gallagher
 43  
  * 
 44  
  * @see org.simpleframework.xml.strategy.Strategy
 45  
  */ 
 46  
 final class Session implements Map {
 47  
 
 48  
    /**
 49  
     * This is the internal map that provides storage for pairs.
 50  
     */         
 51  
    private final Map map;      
 52  
    
 53  
    /**
 54  
     * This is used to determine if this session is a strict one.
 55  
     */
 56  
    private final boolean strict;
 57  
    
 58  
    /**
 59  
     * Constructor for the <code>Session</code> object. This is 
 60  
     * used to create a new session that makes use of a hash map
 61  
     * to store key value pairs which are maintained throughout
 62  
     * the duration of the persistence process this is used in.
 63  
     */ 
 64  
    public Session(){
 65  22
       this(true);
 66  22
    }
 67  
 
 68  
    /**
 69  
     * Constructor for the <code>Session</code> object. This is 
 70  
     * used to create a new session that makes use of a hash map
 71  
     * to store key value pairs which are maintained throughout
 72  
     * the duration of the persistence process this is used in.
 73  
     * 
 74  
     * @param strict this is used to determine the strictness
 75  
     */ 
 76  98469
    public Session(boolean strict){
 77  98469
       this.map = new HashMap();    
 78  98469
       this.strict = strict;
 79  98469
    }
 80  
    
 81  
    /**
 82  
     * This is used to determine if the deserialization mode is strict
 83  
     * or not. If this is not strict then deserialization will be done
 84  
     * in such a way that additional elements and attributes can be
 85  
     * ignored. This allows external XML formats to be used without 
 86  
     * having to match the object structure to the XML fully.
 87  
     * 
 88  
     * @return this returns true if the deserialization is strict
 89  
     */
 90  
    public boolean isStrict() {
 91  44
       return strict;
 92  
    }
 93  
 
 94  
    /**
 95  
     * This returns the inner map used by the session object. The
 96  
     * internal map is the <code>Map</code> instance that is used
 97  
     * for persister callbacks, a reference to this map can be
 98  
     * safely made by any object receiving a callback.
 99  
     * 
 100  
     * @return this returns the internal session map used
 101  
     */
 102  
    public Map getMap() {
 103  43
       return map;
 104  
    }     
 105  
 
 106  
    /**
 107  
     * This obviously enough provides the number of pairs that
 108  
     * have been inserted into the internal map. This acts as
 109  
     * a proxy method for the internal map <code>size</code>.
 110  
     *
 111  
     * @return this returns the number of pairs are available
 112  
     */ 
 113  
    public int size() {
 114  0
       return map.size();           
 115  
    }
 116  
 
 117  
    /**
 118  
     * This method is used to determine whether the session has 
 119  
     * any pairs available. If the <code>size</code> is zero then
 120  
     * the session is empty and this returns true. The is acts as 
 121  
     * a proxy the the <code>isEmpty</code> of the internal map.
 122  
     * 
 123  
     * @return this is true if there are no available pairs
 124  
     */ 
 125  
    public boolean isEmpty() {
 126  5
       return map.isEmpty();           
 127  
    }
 128  
 
 129  
    /**
 130  
     * This is used to determine whether a value representing the
 131  
     * name of a pair has been inserted into the internal map. The
 132  
     * object passed into this method is typically a string which
 133  
     * references a template variable but can be any object.
 134  
     *  
 135  
     * @param name this is the name of a pair within the map
 136  
     *
 137  
     * @return this returns true if the pair of that name exists
 138  
     */ 
 139  
    public boolean containsKey(Object name) {
 140  0
       return map.containsKey(name);           
 141  
    }
 142  
 
 143  
    /**
 144  
     * This method is used to determine whether any pair that has
 145  
     * been inserted into the internal map had the presented value.
 146  
     * If one or more pairs within the collected mappings contains
 147  
     * the value provided then this method will return true.
 148  
     * 
 149  
     * @param value this is the value that is to be searched for
 150  
     *
 151  
     * @return this returns true if any value is equal to this
 152  
     */    
 153  
    public boolean containsValue(Object value) {
 154  0
       return map.containsValue(value);           
 155  
    }
 156  
 
 157  
    /**
 158  
     * The <code>get</code> method is used to acquire the value for
 159  
     * a named pair. So if a mapping for the specified name exists
 160  
     * within the internal map the mapped entry value is returned.
 161  
     *
 162  
     * @param name this is a name used to search for the value
 163  
     *
 164  
     * @return this returns the value mapped to the given name     
 165  
     */ 
 166  
    public Object get(Object name) {
 167  200087
       return map.get(name);           
 168  
    }
 169  
    
 170  
    /**
 171  
     * The <code>put</code> method is used to insert the name and
 172  
     * value provided into the internal session map. The inserted
 173  
     * value will be available to all objects receiving callbacks.
 174  
     *
 175  
     * @param name this is the name the value is mapped under    
 176  
     * @param value this is the value to mapped with the name
 177  
     *
 178  
     * @return this returns the previous value if there was any
 179  
     */ 
 180  
    public Object put(Object name, Object value) {
 181  2
       return map.put(name, value);           
 182  
    }
 183  
 
 184  
    /**
 185  
     * The <code>remove</code> method is used to remove the named
 186  
     * mapping from the internal session map. This ensures that
 187  
     * the mapping is no longer available for persister callbacks.
 188  
     *
 189  
     * @param name this is a string used to search for the value
 190  
     *
 191  
     * @return this returns the value mapped to the given name   
 192  
     */ 
 193  
    public Object remove(Object name) {
 194  0
       return map.remove(name);           
 195  
    }
 196  
 
 197  
    /**
 198  
     * This method is used to insert a collection of mappings into 
 199  
     * the session map. This is used when another source of pairs
 200  
     * is required to populate the collection currently maintained
 201  
     * within this sessions internal map. Any pairs that currently
 202  
     * exist with similar names will be overwritten by this.
 203  
     * 
 204  
     * @param data this is the collection of pairs to be added
 205  
     */ 
 206  
    public void putAll(Map data) {
 207  0
       map.putAll(data);           
 208  0
    }
 209  
 
 210  
    /**
 211  
     * This is used to acquire the names for all the pairs that 
 212  
     * have currently been collected by this session. This is used
 213  
     * to determine which mappings are available within the map.
 214  
     *
 215  
     * @return the set of names for all mappings in the session    
 216  
     */ 
 217  
    public Set keySet() {
 218  0
       return map.keySet();           
 219  
    }
 220  
 
 221  
    /**
 222  
     * This method is used to acquire the value for all pairs that
 223  
     * have currently been collected by this session. This is used
 224  
     * to determine the values that are available in the session.
 225  
     *
 226  
     * @return the list of values for all mappings in the session   
 227  
     */ 
 228  
    public Collection values() {
 229  0
       return map.values();           
 230  
    }
 231  
 
 232  
    /**
 233  
     * This method is used to acquire the name and value pairs that
 234  
     * have currently been collected by this session. This is used
 235  
     * to determine which mappings are available within the session.
 236  
     *
 237  
     * @return thie set of mappings that exist within the session   
 238  
     */ 
 239  
    public Set entrySet() {
 240  0
       return map.entrySet();           
 241  
    }
 242  
 
 243  
    /**
 244  
     * The <code>clear</code> method is used to wipe out all the
 245  
     * currently existing pairs from the collection. This is used
 246  
     * when all mappings within the session should be erased.
 247  
     */ 
 248  
    public void clear() {
 249  0
       map.clear();           
 250  0
    }   
 251  
 }
 252