Clover coverage report - JGAP 3.1
Coverage timestamp: Mo Dez 11 2006 21:16:18 CET
file stats: LOC: 331   Methods: 17
NCLOC: 138   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
JGAPFactory.java 90,9% 98,1% 100% 96,8%
coverage coverage
 1    /*
 2    * This file is part of JGAP.
 3    *
 4    * JGAP offers a dual license model containing the LGPL as well as the MPL.
 5    *
 6    * For licencing information please see the file license.txt included with JGAP
 7    * or have a look at the top of class org.jgap.Chromosome which representatively
 8    * includes the JGAP license policy applicable for any file delivered with JGAP.
 9    */
 10    package org.jgap.impl;
 11   
 12    import java.util.*;
 13    import org.jgap.*;
 14    import org.jgap.util.*;
 15   
 16    /**
 17    * Central factory for creating default objects to use, e.g. random generators.
 18    * Could be made configurable. An instance of JGAPFactory can be acces via<p>
 19    * <code>Genotype.getConfiguration().getJGAPFactory();</code><p>
 20    * To use your own factory class instead, use:<p>
 21    * <code>
 22    * System.setProperty(Configuration.PROPERTY_JGAPFACTORY_CLASS, "myFactory");<p>
 23    * </code>
 24    * with "myFactory" representing the name of your class to use.
 25    *
 26    * @author Klaus Meffert
 27    * @since 2.6
 28    */
 29    public class JGAPFactory
 30    implements IJGAPFactory {
 31    /** String containing the CVS revision. Read out via reflection!*/
 32    private final static String CVS_REVISION = "$Revision: 1.12 $";
 33   
 34    private List m_parameters;
 35   
 36    private List m_cloneHandlers;
 37   
 38    private List m_initer;
 39   
 40    private List m_compareHandlers;
 41   
 42    private ICloneHandler m_defaultCloneHandler;
 43   
 44    private IInitializer m_defaultIniter;
 45   
 46    private ICompareToHandler m_defaultComparer;
 47   
 48    private IGeneticOperatorConstraint m_geneticOpConstraint;
 49   
 50    private transient LRUCache m_cache;
 51   
 52    private boolean m_useCaching;
 53   
 54  3695 public JGAPFactory(boolean a_useCaching) {
 55  3695 m_initer = new Vector();
 56  3695 m_cache = new LRUCache(50);
 57  3695 m_useCaching = a_useCaching;
 58  3695 m_cloneHandlers = new Vector();
 59  3695 m_compareHandlers = new Vector();
 60    // Construct default handlers at the beginning to avoid multi-threading
 61    // conflicts in getXXXFor methods.
 62  3695 m_defaultCloneHandler = new DefaultCloneHandler();
 63  3695 m_defaultIniter = new DefaultInitializer();
 64  3695 m_defaultComparer = new DefaultCompareToHandler();
 65    }
 66   
 67    /**
 68    * Allows setting (generic because unknown) parameters for creating objects.
 69    *
 70    * @param a_parameters Collection of generic parameters
 71    *
 72    * @author Klaus Meffert
 73    * @since 2.6
 74    */
 75  1 public void setParameters(final Collection a_parameters) {
 76  1 m_parameters = new Vector(a_parameters);
 77    }
 78   
 79    /**
 80    * @return Collection of generic parameters
 81    *
 82    * @author Klaus Meffert
 83    * @since 3.0
 84    */
 85  1 public Collection getParameters() {
 86  1 return m_parameters;
 87    }
 88   
 89  1 public RandomGenerator createRandomGenerator() {
 90  1 return new StockRandomGenerator();
 91    }
 92   
 93    /**
 94    * Registers a clone handler that could be retrieved by
 95    * getCloneHandlerFor(Class).
 96    *
 97    * @param a_cloneHandler the ICloneHandler to register
 98    * @return index of the added clone handler, needed when removeCloneHandler
 99    * will be called
 100    *
 101    * @author Klaus Meffert
 102    * @since 2.6
 103    */
 104  9 public int registerCloneHandler(final ICloneHandler a_cloneHandler) {
 105  9 m_cloneHandlers.add(a_cloneHandler);
 106  9 return m_cloneHandlers.size() - 1;
 107    }
 108   
 109    /**
 110    * Removes a clone handler at a given index (which is obtained from
 111    * registerCloneHandler).
 112    *
 113    * @param a_index the index of the clone handler to remove
 114    * @return the removed ICloneHandler, or Exception if not successfull
 115    *
 116    * @author Klaus Meffert
 117    * @since 2.6
 118    */
 119  2 public ICloneHandler removeCloneHandler(final int a_index) {
 120  2 return (ICloneHandler) m_cloneHandlers.remove(a_index);
 121    }
 122   
 123    /**
 124    * Retrieves a clone handler capable of clone the given class.
 125    *
 126    * @param a_obj the object to clone (maybe null)
 127    * @param a_classToClone the class to clone an object of (maybe null)
 128    * @return the clone handler found capable of clone the given
 129    * class, or null if none registered
 130    *
 131    * @author Klaus Meffert
 132    * @since 2.6
 133    */
 134  13 public ICloneHandler getCloneHandlerFor(final Object a_obj,
 135    final Class a_classToClone) {
 136  13 return (ICloneHandler) findHandlerFor(a_obj, a_classToClone,
 137    m_cloneHandlers,
 138    m_defaultCloneHandler,
 139    "clone");
 140    }
 141   
 142    /**
 143    * Registers an initializer that could be retrieved by
 144    * getInitializerFor(Class).
 145    *
 146    * @param a_chromIniter the IChromosomeInitializer to register
 147    * @return index of the added initializer, needed when
 148    * removeChromosomeInitializer will be called
 149    *
 150    * @author Klaus Meffert
 151    * @since 2.6
 152    */
 153  1 public int registerInitializer(final IInitializer a_chromIniter) {
 154  1 m_initer.add(a_chromIniter);
 155  1 return m_initer.size() - 1;
 156    }
 157   
 158    /**
 159    * Removes an initializer at a given index (which is obtained from
 160    * registerInitializer).
 161    *
 162    * @param a_index the index of the initializer to remove
 163    * @return the removed IInitializer, or null if not successfull
 164    *
 165    * @author Klaus Meffert
 166    * @since 2.6
 167    */
 168  2 public IInitializer removeInitializer(final int a_index) {
 169  2 return (IInitializer) m_initer.remove(a_index);
 170    }
 171   
 172    /**
 173    * Retrieves an initializer capable of initializing the Object of the given
 174    * class.
 175    *
 176    * @param a_obj the object to init (maybe null)
 177    * @param a_class the object class to init (maybe null)
 178    * @return new instance (should be!) of initialized object
 179    *
 180    * @author Klaus Meffert
 181    * @since 2.6
 182    */
 183  22 public IInitializer getInitializerFor(final Object a_obj,
 184    final Class a_class) {
 185  22 return (IInitializer) findHandlerFor(a_obj, a_class,
 186    m_initer,
 187    m_defaultIniter,
 188    "init");
 189    }
 190   
 191  3 public void setGeneticOperatorConstraint(final IGeneticOperatorConstraint
 192    a_constraint) {
 193  3 m_geneticOpConstraint = a_constraint;
 194    }
 195   
 196  42 public IGeneticOperatorConstraint getGeneticOperatorConstraint() {
 197  42 return m_geneticOpConstraint;
 198    }
 199   
 200    /**
 201    * Retrieves a handler capable of comparing two instances of the given class.
 202    *
 203    * @param a_obj the object to compare (maybe null)
 204    * @param a_classToCompareTo the class instances to compare (maybe null)
 205    * @return the handler found capable of comparing instances
 206    * of the given class, or null if none registered
 207    *
 208    * @author Klaus Meffert
 209    * @since 2.6
 210    */
 211  35 public ICompareToHandler getCompareToHandlerFor(Object a_obj,
 212    Class a_classToCompareTo) {
 213  35 return (ICompareToHandler) findHandlerFor(a_obj, a_classToCompareTo,
 214    m_compareHandlers,
 215    m_defaultComparer,
 216    "compare");
 217    }
 218   
 219    /**
 220    * Registers a compareTo-handler that could be retrieved by
 221    * getCompareToHandlerFor(Class).
 222    *
 223    * @param a_compareToHandler the ICompareToHandler to register
 224    * @return index of the added handler, needed when removeCompareToHandler
 225    * will be called
 226    *
 227    * @author Klaus Meffert
 228    * @since 2.6
 229    */
 230  1 public int registerCompareToHandler(ICompareToHandler a_compareToHandler) {
 231  1 m_compareHandlers.add(a_compareToHandler);
 232  1 return m_compareHandlers.size() - 1;
 233    }
 234   
 235    /**
 236    * Removes a compareTo-handler at a given index (which is obtained from
 237    * registerCompareToHandler).
 238    *
 239    * @param a_index the index of the handler to remove
 240    * @return the removed handler, or Exception if not successfull
 241    *
 242    * @author Klaus Meffert
 243    * @since 2.6
 244    */
 245  2 public ICompareToHandler removeCompareToHandler(final int a_index) {
 246  2 return (ICompareToHandler) m_compareHandlers.remove(a_index);
 247    }
 248   
 249    /**
 250    * Helper: Finds a handler for a given Object or Class, returns the default
 251    * handler, if one is provided. Uses an LRU cache to speedup things!
 252    *
 253    * @param a_obj the object to find a handler for (maybe null)
 254    * @param a_class the class to find a handler for (maybe null)
 255    * @param a_list list of available handlers
 256    * @param a_default a default handler to return in none other is found
 257    * @param a_listID arbitrary unique string for accessing the cache
 258    * @return the handler found, or null if none registered
 259    *
 260    * @author Klaus Meffert
 261    * @since 2.6
 262    */
 263  70 protected IHandler findHandlerFor(final Object a_obj,
 264    final Class a_class,
 265    final List a_list,
 266    final IHandler a_default,
 267    final String a_listID) {
 268  70 String key = null;
 269  70 String key1, key2;
 270  70 if (m_useCaching) {
 271    // Construct key for cache lookup:
 272    // Class name of list + a_class-Name + a_obj.hashCode()
 273  5 if (a_class == null) {
 274  1 key1 = "null";
 275    }
 276    else {
 277  4 key1 = a_class.getName();
 278    }
 279  5 if (a_obj == null) {
 280  5 key2 = "null";
 281    }
 282    else {
 283  0 key2 = a_obj.getClass().getName();
 284    }
 285  5 key = a_listID + "/" + key1 + "/" + key2;
 286    // Lookup cache.
 287    // -------------
 288  5 Object handler = m_cache.get(key);
 289  5 if (handler != null) {
 290  1 return (IHandler) handler;
 291    }
 292    // Not found in cache. Search initially.
 293    // -------------------------------------
 294    }
 295  69 IHandler result = null;
 296  69 Iterator it = a_list.iterator();
 297  69 while (it.hasNext()) {
 298  10 IHandler initer = (IHandler) it.next();
 299  10 if (initer.isHandlerFor(a_obj, a_class)) {
 300  3 result = initer;
 301  3 break;
 302    }
 303    }
 304  69 if (result == null) {
 305    // No registered handler found. Try the default handler.
 306    // -----------------------------------------------------
 307  66 if (a_default != null) {
 308  66 if (a_default.isHandlerFor(a_obj, a_class)) {
 309  59 result = a_default;
 310    }
 311    }
 312    }
 313  69 if (m_useCaching) {
 314    // Add to cache
 315  4 if (result != null) {
 316  1 m_cache.put(key, result);
 317    }
 318    }
 319  69 return result;
 320    }
 321   
 322    /**
 323    * @return true: caching sued, false: no caching used
 324    *
 325    * @author Klaus Meffert
 326    * @since 3.0
 327    */
 328  2 public boolean isUseCaching() {
 329  2 return m_useCaching;
 330    }
 331    }