Coverage Report - org.simpleframework.xml.strategy.ReadState
 
Classes in this File Line Coverage Branch Coverage Complexity
ReadState
100%
13/13
75%
3/4
2
 
 1  
 /*
 2  
  * ReadState.java April 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.strategy;
 20  
 
 21  
 import org.simpleframework.xml.util.WeakCache;
 22  
 
 23  
 
 24  
 /**
 25  
  * The <code>ReadState</code> object is used to store all graphs that
 26  
  * are currently been read with a given cycle strategy. The goal of
 27  
  * this object is to act as a temporary store for graphs such that 
 28  
  * when the persistence session has completed the read graph will be
 29  
  * garbage collected. This ensures that there are no lingering object
 30  
  * reference that could cause a memory leak. If a graph for the given
 31  
  * session key does not exist then a new one is created. 
 32  
  * 
 33  
  * @author Niall Gallagher
 34  
  * 
 35  
  * @see org.simpleframework.xml.util.WeakCache
 36  
  */
 37  
 class ReadState extends WeakCache<ReadGraph>{
 38  
    
 39  
    /** 
 40  
     * This is the contract that specifies the attributes to use.
 41  
     */
 42  
    private final Contract contract;
 43  
    
 44  
    /**
 45  
     * This is the loader used to load the classes for this.
 46  
     */
 47  
    private final Loader loader;
 48  
    
 49  
    /**
 50  
     * Constructor for the <code>ReadState</code> object. This is used
 51  
     * to create graphs that are used for reading objects from the XML
 52  
     * document. The specified strategy is used to acquire the names
 53  
     * of the special attributes used during the serialization.
 54  
     * 
 55  
     * @param contract this is name scheme used by the cycle strategy 
 56  
     */
 57  260
    public ReadState(Contract contract) {
 58  260
       this.loader = new Loader();
 59  260
       this.contract = contract;
 60  260
    }
 61  
 
 62  
    /**
 63  
     * This will acquire the graph using the specified session map. If
 64  
     * a graph does not already exist mapped to the given session then
 65  
     * one will be created and stored with the key provided. Once the
 66  
     * specified key is garbage collected then so is the graph object.
 67  
     * 
 68  
     * @param map this is typically the persistence session map used 
 69  
     * 
 70  
     * @return returns a graph used for reading the XML document
 71  
     */
 72  
    public ReadGraph find(Object map) throws Exception {
 73  26854
       ReadGraph read = fetch(map);
 74  
       
 75  26854
       if(read != null) {
 76  25346
          return read;
 77  
       }
 78  1508
       return create(map);
 79  
    }
 80  
    
 81  
    /**
 82  
     * This will acquire the graph using the specified session map. If
 83  
     * a graph does not already exist mapped to the given session then
 84  
     * one will be created and stored with the key provided. Once the
 85  
     * specified key is garbage collected then so is the graph object.
 86  
     * 
 87  
     * @param map this is typically the persistence session map used 
 88  
     * 
 89  
     * @return returns a graph used for reading the XML document
 90  
     */
 91  
    private ReadGraph create(Object map) throws Exception {
 92  1508
       ReadGraph read = fetch(map);
 93  
       
 94  1508
       if(read == null) {
 95  1508
          read = new ReadGraph(contract, loader);
 96  1508
          cache(map, read);
 97  
       }
 98  1508
       return read;
 99  
    }   
 100  
 }