Coverage Report - org.simpleframework.xml.convert.Registry
 
Classes in this File Line Coverage Branch Coverage Complexity
Registry
100%
18/18
75%
6/8
2
 
 1  
 /*
 2  
  * Registry.java January 2010
 3  
  *
 4  
  * Copyright (C) 2010, 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.convert;
 20  
 
 21  
 import org.simpleframework.xml.util.Cache;
 22  
 import org.simpleframework.xml.util.ConcurrentCache;
 23  
 
 24  
 /**
 25  
  * The <code>Registry</code> represents an object that is used to
 26  
  * register bindings between a class and a converter implementation.
 27  
  * Converter instances created by this registry are lazily created
 28  
  * and cached so that they are instantiated only once. This ensures 
 29  
  * that the overhead of serialization is reduced. 
 30  
  * 
 31  
  * @author Niall Gallagher
 32  
  * 
 33  
  * @see org.simpleframework.xml.convert.RegistryStrategy
 34  
  */
 35  
 public class Registry {
 36  
    
 37  
    /**
 38  
     * This is used to cache the converters based on object types.
 39  
     */
 40  
    private final Cache<Converter> cache;
 41  
    
 42  
    /**
 43  
     * This is used to bind converter types to serializable types.
 44  
     */
 45  
    private final RegistryBinder binder;
 46  
    
 47  
    /**
 48  
     * Constructor for the <code>Registry</code> object. This is used
 49  
     * to create a registry between classes and the converters that
 50  
     * should be used to serialize and deserialize the instances. All
 51  
     * converters are instantiated once and cached for reuse.
 52  
     */
 53  9
    public Registry() {
 54  9
       this.cache = new ConcurrentCache<Converter>();
 55  9
       this.binder = new RegistryBinder();
 56  9
    }
 57  
    
 58  
    /**
 59  
     * This is used to acquire a <code>Converter</code> instance from
 60  
     * the registry. All instances are cache to reduce the overhead
 61  
     * of lookups during the serialization process. Converters are
 62  
     * lazily instantiated and so are only created if demanded.
 63  
     * 
 64  
     * @param type this is the type to find the converter for
 65  
     * 
 66  
     * @return this returns the converter instance for the type
 67  
     */
 68  
    public Converter lookup(Class type) throws Exception {
 69  67
       Converter converter = cache.fetch(type);
 70  
       
 71  67
       if(converter == null) {
 72  46
          return create(type);
 73  
       }
 74  21
       return converter;
 75  
    }
 76  
    
 77  
    /**
 78  
     * This is used to acquire a <code>Converter</code> instance from
 79  
     * the registry. All instances are cached to reduce the overhead
 80  
     * of lookups during the serialization process. Converters are
 81  
     * lazily instantiated and so are only created if demanded.
 82  
     * 
 83  
     * @param type this is the type to find the converter for
 84  
     * 
 85  
     * @return this returns the converter instance for the type
 86  
     */
 87  
    private Converter create(Class type) throws Exception {
 88  46
       Converter converter = binder.lookup(type);
 89  
       
 90  46
       if(converter != null) {
 91  8
          cache.cache(type, converter);
 92  
       }
 93  46
       return converter;
 94  
    }
 95  
    
 96  
    /**
 97  
     * This is used to register a binding between a type and the
 98  
     * converter used to serialize and deserialize it. During the
 99  
     * serialization process the converters are retrieved and 
 100  
     * used to convert the object members to XML.
 101  
     * 
 102  
     * @param type this is the object type to bind to a converter
 103  
     * @param converter this is the converter class to be used
 104  
     * 
 105  
     * @return this will return this registry instance to use
 106  
     */
 107  
    public Registry bind(Class type, Class converter) throws Exception {
 108  8
       if(type != null) {
 109  8
          binder.bind(type, converter);
 110  
       }
 111  8
       return this;
 112  
    }
 113  
    
 114  
    /**
 115  
     * This is used to register a binding between a type and the
 116  
     * converter used to serialize and deserialize it. During the
 117  
     * serialization process the converters are retrieved and 
 118  
     * used to convert the object properties to XML.
 119  
     * 
 120  
     * @param type this is the object type to bind to a converter
 121  
     * @param converter this is the converter instance to be used
 122  
     * 
 123  
     * @return this will return this registry instance to use
 124  
     */
 125  
    public Registry bind(Class type, Converter converter) throws Exception {
 126  4
       if(type != null) {
 127  4
          cache.cache(type, converter);
 128  
       }
 129  4
       return this;
 130  
    }
 131  
 }