Coverage Report - org.simpleframework.xml.convert.RegistryBinder
 
Classes in this File Line Coverage Branch Coverage Complexity
RegistryBinder
100%
11/11
100%
2/2
1.5
 
 1  
 /*
 2  
  * RegistryBinder.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>RegistryBinder</code> object is used acquire converters
 26  
  * using a binding between a type and its converter. All converters
 27  
  * instantiated are cached internally to ensure that the overhead
 28  
  * of acquiring a converter is reduced. Converters are created on
 29  
  * demand to ensure they are instantiated only if required.
 30  
  * 
 31  
  * @author Niall Gallagher
 32  
  * 
 33  
  * @see org.simpleframework.xml.convert.Registry
 34  
  */
 35  
 class RegistryBinder {
 36  
    
 37  
    /**
 38  
     * This is used to instantiate and cache the converter objects.
 39  
     */
 40  
    private final ConverterFactory factory;
 41  
    
 42  
    /**
 43  
     * This is used to cache bindings between types and converters.
 44  
     */
 45  
    private final Cache<Class> cache;
 46  
    
 47  
    /**
 48  
     * Constructor for the <code>RegistryBinder</code> object. This 
 49  
     * is used to create bindings between classes and the converters 
 50  
     * that should be used to serialize and deserialize the instances. 
 51  
     * All converters are instantiated once and cached for reuse.
 52  
     */
 53  9
    public RegistryBinder() {
 54  9
       this.cache = new ConcurrentCache<Class>();
 55  9
       this.factory = new ConverterFactory();
 56  9
    }
 57  
    
 58  
    /**
 59  
     * This is used to acquire a <code>Converter</code> instance from
 60  
     * this binder. All instances are cached 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  46
       Class result = cache.fetch(type);
 70  
       
 71  46
       if(result != null) {
 72  8
          return create(result);
 73  
       }
 74  38
       return null;
 75  
    }
 76  
    
 77  
    /**
 78  
     * This is used to acquire a <code>Converter</code> instance from
 79  
     * this binder. 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  8
       return factory.getInstance(type);
 89  
    }
 90  
    
 91  
    /**
 92  
     * This is used to register a binding between a type and the
 93  
     * converter used to serialize and deserialize it. During the
 94  
     * serialization process the converters are retrieved and 
 95  
     * used to convert the object properties to XML.
 96  
     * 
 97  
     * @param type this is the object type to bind to a converter
 98  
     * @param converter this is the converter class to be used
 99  
     */
 100  
    public void bind(Class type, Class converter) throws Exception {
 101  8
       cache.cache(type, converter);
 102  8
    }
 103  
 }