1 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 31 public class JGAPFactory 32 implements IJGAPFactory, Serializable, ICloneable, Comparable { 33 34 private final static String 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 m_defaultCloneHandler = new DefaultCloneHandler(); 65 m_defaultIniter = new DefaultInitializer(); 66 m_defaultComparer = new DefaultCompareToHandler(); 67 } 68 69 77 public void setParameters(final Collection a_parameters) { 78 m_parameters = new Vector(a_parameters); 79 } 80 81 87 public Collection getParameters() { 88 return m_parameters; 89 } 90 91 public RandomGenerator createRandomGenerator() { 92 return new StockRandomGenerator(); 93 } 94 95 106 public int registerCloneHandler(final ICloneHandler a_cloneHandler) { 107 m_cloneHandlers.add(a_cloneHandler); 108 return m_cloneHandlers.size() - 1; 109 } 110 111 121 public ICloneHandler removeCloneHandler(final int a_index) { 122 return (ICloneHandler) m_cloneHandlers.remove(a_index); 123 } 124 125 136 public ICloneHandler getCloneHandlerFor(final Object a_obj, 137 final Class a_classToClone) { 138 return (ICloneHandler) findHandlerFor(a_obj, a_classToClone, 139 m_cloneHandlers, 140 m_defaultCloneHandler, 141 "clone"); 142 } 143 144 155 public int registerInitializer(final IInitializer a_chromIniter) { 156 m_initer.add(a_chromIniter); 157 return m_initer.size() - 1; 158 } 159 160 170 public IInitializer removeInitializer(final int a_index) { 171 return (IInitializer) m_initer.remove(a_index); 172 } 173 174 185 public IInitializer getInitializerFor(final Object a_obj, 186 final Class 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 213 public ICompareToHandler getCompareToHandlerFor(Object a_obj, 214 Class a_classToCompareTo) { 215 return (ICompareToHandler) findHandlerFor(a_obj, a_classToCompareTo, 216 m_compareHandlers, 217 m_defaultComparer, 218 "compare"); 219 } 220 221 232 public int registerCompareToHandler(ICompareToHandler a_compareToHandler) { 233 m_compareHandlers.add(a_compareToHandler); 234 return m_compareHandlers.size() - 1; 235 } 236 237 247 public ICompareToHandler removeCompareToHandler(final int a_index) { 248 return (ICompareToHandler) m_compareHandlers.remove(a_index); 249 } 250 251 265 protected IHandler findHandlerFor(final Object a_obj, 266 final Class a_class, 267 final List a_list, 268 final IHandler a_default, 269 final String a_listID) { 270 String key = null; 271 String key1, key2; 272 if (m_useCaching) { 273 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 Object handler = m_cache.get(key); 292 if (handler != null) { 293 return (IHandler) handler; 294 } 295 } 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 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 if (result != null) { 320 m_cache.put(key, result); 321 } 322 } 323 return result; 324 } 325 326 332 public boolean isUseCaching() { 333 return m_useCaching; 334 } 335 336 public Object clone() { 337 try { 338 339 return super.clone(); 340 } catch (CloneNotSupportedException cex) { 341 throw new CloneException(cex); 342 } 343 } 344 345 353 public boolean equals(Object a_other) { 354 try { 355 return compareTo(a_other) == 0; 356 } catch (ClassCastException cex) { 357 return false; 358 } 359 } 360 361 368 public int compareTo(Object a_other) { 369 if (a_other == null) { 370 return 1; 371 } 372 else { 373 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 |