KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgap > impl > JGAPFactory


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.io.*;
13 import java.util.*;
14 import org.jgap.*;
15 import org.jgap.util.*;
16 import org.apache.commons.lang.builder.*;
17
18 /**
19  * Central factory for creating default objects to use, e.g. random generators.
20  * Could be made configurable. An instance of JGAPFactory can be accessed via<p>
21  * <code>Genotype.getConfiguration().getJGAPFactory();</code><p>
22  * To use your own factory class instead, use:<p>
23  * <code>
24  * System.setProperty(Configuration.PROPERTY_JGAPFACTORY_CLASS, "myFactory");<p>
25  * </code>
26  * with "myFactory" representing the name of your class to use.
27  *
28  * @author Klaus Meffert
29  * @since 2.6
30  */

31 public class JGAPFactory
32     implements IJGAPFactory, Serializable, ICloneable, Comparable JavaDoc {
33   /** String containing the CVS revision. Read out via reflection!*/
34   private final static String JavaDoc CVS_REVISION = "$Revision: 1.17 $";
35
36   private List m_parameters;
37
38   private List m_cloneHandlers;
39
40   private List m_initer;
41
42   private List m_compareHandlers;
43
44   private ICloneHandler m_defaultCloneHandler;
45
46   private IInitializer m_defaultIniter;
47
48   private ICompareToHandler m_defaultComparer;
49
50   private IGeneticOperatorConstraint m_geneticOpConstraint;
51
52   private transient LRUCache m_cache;
53
54   private boolean m_useCaching;
55
56   public JGAPFactory(boolean a_useCaching) {
57     m_initer = new Vector();
58     m_cache = new LRUCache(50);
59     m_useCaching = a_useCaching;
60     m_cloneHandlers = new Vector();
61     m_compareHandlers = new Vector();
62     // Construct default handlers at the beginning to avoid multi-threading
63
// conflicts in getXXXFor methods.
64
m_defaultCloneHandler = new DefaultCloneHandler();
65     m_defaultIniter = new DefaultInitializer();
66     m_defaultComparer = new DefaultCompareToHandler();
67   }
68
69   /**
70    * Allows setting (generic because unknown) parameters for creating objects.
71    *
72    * @param a_parameters Collection of generic parameters
73    *
74    * @author Klaus Meffert
75    * @since 2.6
76    */

77   public void setParameters(final Collection a_parameters) {
78     m_parameters = new Vector(a_parameters);
79   }
80
81   /**
82    * @return Collection of generic parameters
83    *
84    * @author Klaus Meffert
85    * @since 3.0
86    */

87   public Collection getParameters() {
88     return m_parameters;
89   }
90
91   public RandomGenerator createRandomGenerator() {
92     return new StockRandomGenerator();
93   }
94
95   /**
96    * Registers a clone handler that could be retrieved by
97    * getCloneHandlerFor(Class).
98    *
99    * @param a_cloneHandler the ICloneHandler to register
100    * @return index of the added clone handler, needed when removeCloneHandler
101    * will be called
102    *
103    * @author Klaus Meffert
104    * @since 2.6
105    */

106   public int registerCloneHandler(final ICloneHandler a_cloneHandler) {
107     m_cloneHandlers.add(a_cloneHandler);
108     return m_cloneHandlers.size() - 1;
109   }
110
111   /**
112    * Removes a clone handler at a given index (which is obtained from
113    * registerCloneHandler).
114    *
115    * @param a_index the index of the clone handler to remove
116    * @return the removed ICloneHandler, or Exception if not successfull
117    *
118    * @author Klaus Meffert
119    * @since 2.6
120    */

121   public ICloneHandler removeCloneHandler(final int a_index) {
122     return (ICloneHandler) m_cloneHandlers.remove(a_index);
123   }
124
125   /**
126    * Retrieves a clone handler capable of clone the given class.
127    *
128    * @param a_obj the object to clone (maybe null)
129    * @param a_classToClone the class to clone an object of (maybe null)
130    * @return the clone handler found capable of clone the given
131    * class, or null if none registered
132    *
133    * @author Klaus Meffert
134    * @since 2.6
135    */

136   public ICloneHandler getCloneHandlerFor(final Object JavaDoc a_obj,
137       final Class JavaDoc a_classToClone) {
138     return (ICloneHandler) findHandlerFor(a_obj, a_classToClone,
139         m_cloneHandlers,
140         m_defaultCloneHandler,
141         "clone");
142   }
143
144   /**
145    * Registers an initializer that could be retrieved by
146    * getInitializerFor(Class).
147    *
148    * @param a_chromIniter the IChromosomeInitializer to register
149    * @return index of the added initializer, needed when
150    * removeChromosomeInitializer will be called
151    *
152    * @author Klaus Meffert
153    * @since 2.6
154    */

155   public int registerInitializer(final IInitializer a_chromIniter) {
156     m_initer.add(a_chromIniter);
157     return m_initer.size() - 1;
158   }
159
160   /**
161    * Removes an initializer at a given index (which is obtained from
162    * registerInitializer).
163    *
164    * @param a_index the index of the initializer to remove
165    * @return the removed IInitializer, or null if not successfull
166    *
167    * @author Klaus Meffert
168    * @since 2.6
169    */

170   public IInitializer removeInitializer(final int a_index) {
171     return (IInitializer) m_initer.remove(a_index);
172   }
173
174   /**
175    * Retrieves an initializer capable of initializing the Object of the given
176    * class.
177    *
178    * @param a_obj the object to init (maybe null)
179    * @param a_class the object class to init (maybe null)
180    * @return new instance (should be!) of initialized object
181    *
182    * @author Klaus Meffert
183    * @since 2.6
184    */

185   public IInitializer getInitializerFor(final Object JavaDoc a_obj,
186                                         final Class JavaDoc a_class) {
187     return (IInitializer) findHandlerFor(a_obj, a_class,
188         m_initer,
189         m_defaultIniter,
190         "init");
191   }
192
193   public void setGeneticOperatorConstraint(final IGeneticOperatorConstraint
194       a_constraint) {
195     m_geneticOpConstraint = a_constraint;
196   }
197
198   public IGeneticOperatorConstraint getGeneticOperatorConstraint() {
199     return m_geneticOpConstraint;
200   }
201
202   /**
203    * Retrieves a handler capable of comparing two instances of the given class.
204    *
205    * @param a_obj the object to compare (maybe null)
206    * @param a_classToCompareTo the class instances to compare (maybe null)
207    * @return the handler found capable of comparing instances
208    * of the given class, or null if none registered
209    *
210    * @author Klaus Meffert
211    * @since 2.6
212    */

213   public ICompareToHandler getCompareToHandlerFor(Object JavaDoc a_obj,
214       Class JavaDoc a_classToCompareTo) {
215     return (ICompareToHandler) findHandlerFor(a_obj, a_classToCompareTo,
216         m_compareHandlers,
217         m_defaultComparer,
218         "compare");
219   }
220
221   /**
222    * Registers a compareTo-handler that could be retrieved by
223    * getCompareToHandlerFor(Class).
224    *
225    * @param a_compareToHandler the ICompareToHandler to register
226    * @return index of the added handler, needed when removeCompareToHandler
227    * will be called
228    *
229    * @author Klaus Meffert
230    * @since 2.6
231    */

232   public int registerCompareToHandler(ICompareToHandler a_compareToHandler) {
233     m_compareHandlers.add(a_compareToHandler);
234     return m_compareHandlers.size() - 1;
235   }
236
237   /**
238    * Removes a compareTo-handler at a given index (which is obtained from
239    * registerCompareToHandler).
240    *
241    * @param a_index the index of the handler to remove
242    * @return the removed handler, or Exception if not successfull
243    *
244    * @author Klaus Meffert
245    * @since 2.6
246    */

247   public ICompareToHandler removeCompareToHandler(final int a_index) {
248     return (ICompareToHandler) m_compareHandlers.remove(a_index);
249   }
250
251   /**
252    * Helper: Finds a handler for a given Object or Class, returns the default
253    * handler, if one is provided. Uses an LRU cache to speedup things!
254    *
255    * @param a_obj the object to find a handler for (maybe null)
256    * @param a_class the class to find a handler for (maybe null)
257    * @param a_list list of available handlers
258    * @param a_default a default handler to return in none other is found
259    * @param a_listID arbitrary unique string for accessing the cache
260    * @return the handler found, or null if none registered
261    *
262    * @author Klaus Meffert
263    * @since 2.6
264    */

265   protected IHandler findHandlerFor(final Object JavaDoc a_obj,
266                                     final Class JavaDoc a_class,
267                                     final List a_list,
268                                     final IHandler a_default,
269                                     final String JavaDoc a_listID) {
270     String JavaDoc key = null;
271     String JavaDoc key1, key2;
272     if (m_useCaching) {
273       // Construct key for cache lookup:
274
// Class name of list + a_class-Name + a_obj.hashCode()
275
// ----------------------------------------------------
276
if (a_class == null) {
277         key1 = "null";
278       }
279       else {
280         key1 = a_class.getName();
281       }
282       if (a_obj == null) {
283         key2 = "null";
284       }
285       else {
286         key2 = a_obj.getClass().getName();
287       }
288       key = a_listID + "/" + key1 + "/" + key2;
289       // Lookup cache.
290
// -------------
291
Object JavaDoc handler = m_cache.get(key);
292       if (handler != null) {
293         return (IHandler) handler;
294       }
295       // Not found in cache. Search initially.
296
// -------------------------------------
297
}
298     IHandler result = null;
299     Iterator it = a_list.iterator();
300     while (it.hasNext()) {
301       IHandler initer = (IHandler) it.next();
302       if (initer.isHandlerFor(a_obj, a_class)) {
303         result = initer;
304         break;
305       }
306     }
307     if (result == null) {
308       // No registered handler found. Try the default handler.
309
// -----------------------------------------------------
310
if (a_default != null) {
311         if (a_default.isHandlerFor(a_obj, a_class)) {
312           result = a_default;
313         }
314       }
315     }
316     if (m_useCaching) {
317       // Add to cache.
318
// -------------
319
if (result != null) {
320         m_cache.put(key, result);
321       }
322     }
323     return result;
324   }
325
326   /**
327    * @return true: caching used, false: no caching used
328    *
329    * @author Klaus Meffert
330    * @since 3.0
331    */

332   public boolean isUseCaching() {
333     return m_useCaching;
334   }
335
336   public Object JavaDoc clone() {
337     try {
338       /**@todo check if it works this way*/
339       return super.clone();
340     } catch (CloneNotSupportedException JavaDoc cex) {
341       throw new CloneException(cex);
342     }
343   }
344
345   /**
346    * The equals-method
347    * @param a_other sic
348    * @return sic
349    *
350    * @author Klaus Meffert
351    * @since 3.2
352    */

353   public boolean equals(Object JavaDoc a_other) {
354     try {
355       return compareTo(a_other) == 0;
356     } catch (ClassCastException JavaDoc cex) {
357       return false;
358     }
359   }
360
361   /**
362    * @param a_other other object to compare
363    * @return as always
364    *
365    * @author Klaus Meffert
366    * @since 3.2
367    */

368   public int compareTo(Object JavaDoc a_other) {
369     if (a_other == null) {
370       return 1;
371     }
372     else {
373       // Do not consider m_parameters, m_cache and m_useCaching.
374
// -------------------------------------------------------
375
JGAPFactory other = (JGAPFactory) a_other;
376       return new CompareToBuilder()
377           .append(m_cloneHandlers.toArray(), other.m_cloneHandlers.toArray())
378           .append(m_initer.toArray(), other.m_initer.toArray())
379           .append(m_compareHandlers.toArray(), other.m_compareHandlers.toArray())
380           .append(m_defaultCloneHandler, other.m_defaultCloneHandler)
381           .append(m_defaultComparer, other.m_defaultComparer)
382           .append(m_geneticOpConstraint, other.m_geneticOpConstraint)
383           .toComparison();
384     }
385   }
386 }
387
Popular Tags