Coverage Report - org.simpleframework.xml.convert.ConverterFactory
 
Classes in this File Line Coverage Branch Coverage Complexity
ConverterFactory
91%
21/23
60%
6/10
2.333
 
 1  
 /*
 2  
  * ConverterFactory.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 java.lang.reflect.Constructor;
 22  
 
 23  
 import org.simpleframework.xml.util.Cache;
 24  
 import org.simpleframework.xml.util.ConcurrentCache;
 25  
 
 26  
 /**
 27  
  * The <code>ConverterFactory</code> is used to instantiate objects
 28  
  * based on a provided type or annotation. This provides a single
 29  
  * point of creation for all converters within the framework. For
 30  
  * performance all the instantiated converters are cached against
 31  
  * the class for that converter. This ensures the converters can
 32  
  * be acquired without the overhead of instantiation.
 33  
  * 
 34  
  * @author Niall Gallagher
 35  
  * 
 36  
  * @see org.simpleframework.xml.convert.ConverterCache
 37  
  */
 38  
 class ConverterFactory {
 39  
    
 40  
    /**
 41  
     * This is the cache that is used to cache converter instances.
 42  
     */
 43  
    private final Cache<Converter> cache; 
 44  
    
 45  
    /**
 46  
     * Constructor for the <code>ConverterFactory</code> object. 
 47  
     * This will create an internal cache which is used to cache all
 48  
     * instantiations made by the factory. Caching the converters
 49  
     * ensures there is no overhead with instantiations.
 50  
     */
 51  22
    public ConverterFactory() {
 52  22
       this.cache = new ConcurrentCache<Converter>();
 53  22
    }
 54  
    
 55  
    /**
 56  
     * This is used to instantiate the converter based on the type
 57  
     * provided. If the type provided can not be instantiated for
 58  
     * some reason then an exception is thrown from this method.
 59  
     * 
 60  
     * @param type this is the converter type to be instantiated
 61  
     * 
 62  
     * @return this returns an instance of the provided type
 63  
     */
 64  
    public Converter getInstance(Class type) throws Exception {
 65  71
       Converter converter = cache.fetch(type);
 66  
       
 67  71
       if(converter == null) {
 68  30
          return getConverter(type);
 69  
       }
 70  41
       return converter;
 71  
    }
 72  
    
 73  
    /**
 74  
     * This is used to instantiate the converter based on the type
 75  
     * of the <code>Convert</code> annotation provided. If the type 
 76  
     * can not be instantiated for some reason then an exception is 
 77  
     * thrown from this method.
 78  
     * 
 79  
     * @param convert this is the annotation containing the type
 80  
     * 
 81  
     * @return this returns an instance of the provided type
 82  
     */
 83  
    public Converter getInstance(Convert convert) throws Exception {
 84  57
       Class type = convert.value();
 85  
       
 86  57
       if(type.isInterface()) {
 87  0
          throw new ConvertException("Can not instantiate %s", type);
 88  
       }
 89  57
       return getInstance(type);
 90  
    }
 91  
    
 92  
    /**
 93  
     * This is used to instantiate the converter based on the type
 94  
     * provided. If the type provided can not be instantiated for
 95  
     * some reason then an exception is thrown from this method.
 96  
     * 
 97  
     * @param type this is the converter type to be instantiated
 98  
     * 
 99  
     * @return this returns an instance of the provided type
 100  
     */
 101  
    private Converter getConverter(Class type) throws Exception {
 102  30
       Constructor factory = getConstructor(type);
 103  
       
 104  30
       if(factory == null){
 105  0
          throw new ConvertException("No default constructor for %s", type);
 106  
       }
 107  30
       return getConverter(type, factory);
 108  
    }
 109  
    
 110  
    /**
 111  
     * This is used to instantiate the converter based on the type
 112  
     * provided. If the type provided can not be instantiated for
 113  
     * some reason then an exception is thrown from this method.
 114  
     * 
 115  
     * @param type this is the converter type to be instantiated
 116  
     * @param factory this is the constructor used to instantiate
 117  
     * 
 118  
     * @return this returns an instance of the provided type
 119  
     */
 120  
    private Converter getConverter(Class type, Constructor factory) throws Exception {
 121  30
       Converter converter = (Converter)factory.newInstance();
 122  
       
 123  30
       if(converter != null){
 124  30
          cache.cache(type, converter);
 125  
       }
 126  30
       return converter;
 127  
    }
 128  
    
 129  
    /**
 130  
     * This is used to acquire the default no argument constructor
 131  
     * for the the provided type. If the constructor is not accessible
 132  
     * then it will be made accessible so that it can be instantiated.
 133  
     * 
 134  
     * @param type this is the type to acquire the constructor for
 135  
     * 
 136  
     * @return this returns the constructor for the type provided
 137  
     */
 138  
    private Constructor getConstructor(Class type) throws Exception {
 139  30
       Constructor factory = type.getDeclaredConstructor();
 140  
       
 141  30
       if(!factory.isAccessible()) {
 142  30
          factory.setAccessible(true);
 143  
       }
 144  30
       return factory;
 145  
    }
 146  
 }