Coverage Report - org.simpleframework.xml.strategy.WriteState
 
Classes in this File Line Coverage Branch Coverage Complexity
WriteState
100%
8/8
100%
2/2
1.5
 
 1  
 /*
 2  
  * WriteState.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  
  * The <code>WriteState</code> object is used to store all graphs that
 25  
  * are currently been written with a given cycle strategy. The goal of
 26  
  * this object is to act as a temporary store for graphs such that 
 27  
  * when the persistence session has completed the write graph will be
 28  
  * garbage collected. This ensures that there are no lingering object
 29  
  * reference that could cause a memory leak. If a graph for the given
 30  
  * session key does not exist then a new one is created. 
 31  
  * 
 32  
  * @author Niall Gallagher
 33  
  * 
 34  
  * @see org.simpleframework.xml.strategy.WriteGraph
 35  
  */
 36  
 class WriteState extends WeakCache<WriteGraph> {
 37  
 
 38  
    /**
 39  
     * This is the contract that specifies the attributes to use.
 40  
     */
 41  
    private Contract contract;
 42  
    
 43  
    /**
 44  
     * Constructor for the <code>WriteState</code> object. This is
 45  
     * used to create graphs that are used for writing objects to the
 46  
     * the XML document. The specified strategy is used to acquire the
 47  
     * names of the special attributes used during the serialization.
 48  
     * 
 49  
     * @param contract this is the name scheme used by the strategy 
 50  
     */
 51  260
    public WriteState(Contract contract) {
 52  260
       this.contract = contract;
 53  260
    }
 54  
 
 55  
    /**
 56  
     * This will acquire the graph using the specified session map. If
 57  
     * a graph does not already exist mapped to the given session then
 58  
     * one will be created and stored with the key provided. Once the
 59  
     * specified key is garbage collected then so is the graph object.
 60  
     * 
 61  
     * @param map this is typically the persistence session map used 
 62  
     * 
 63  
     * @return returns a graph used for writing the XML document
 64  
     */
 65  
    public WriteGraph find(Object map) {
 66  39766
       WriteGraph write = fetch(map);
 67  
       
 68  39766
       if(write == null) {
 69  1957
          write = new WriteGraph(contract);
 70  1957
          cache(map, write);
 71  
       }
 72  39766
       return write;
 73  
    }
 74  
 }