KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.jgap.*;
13
14 /**
15  * Provides a pooling mechanism for Chromosome instances so that
16  * discarded Chromosome instances can be recycled, thus saving memory and the
17  * overhead of constructing new ones from scratch each time.
18  *
19  * @author Neil Rotstan
20  * @author Klaus Meffert
21  * @since 1.0
22  */

23 public class ChromosomePool
24     implements IChromosomePool {
25   /** String containing the CVS revision. Read out via reflection!*/
26   private static final String JavaDoc CVS_REVISION = "$Revision: 1.11 $";
27
28   /**
29    * The internal pool in which the Chromosomes are stored.
30    */

31   private Pool m_chromosomePool;
32
33   /**
34    * Constructor.
35    *
36    * @author Neil Rostan
37    * @since 1.0
38    */

39   public ChromosomePool() {
40     m_chromosomePool = new Pool();
41   }
42
43   /**
44    * Attempts to acquire an Chromosome instance from the chromosome pool.
45    * It should be noted that nothing is guaranteed about the value of the
46    * Chromosome's genes and they should be treated as undefined.
47    *
48    * @return a Chromosome instance from the pool or null if no Chromosome
49    * instances are available in the pool
50    *
51    * @author Neil Rostan
52    * @since 1.0
53    */

54   public synchronized IChromosome acquireChromosome() {
55     return (IChromosome) m_chromosomePool.acquirePooledObject();
56   }
57
58   /**
59    * Releases a Chromosome to the pool. It's not required that the Chromosome
60    * originated from the pool--any Chromosome can be released to it. This
61    * method will invoke the cleanup() method on each of the Chromosome's
62    * genes prior to adding it back to the pool.
63    *
64    * @param a_chromosome the Chromosome instance to be released into the pool
65    *
66    * @author Neil Rostan
67    * @since 1.0
68    */

69   public synchronized void releaseChromosome(final IChromosome a_chromosome) {
70     if (a_chromosome == null) {
71       throw new IllegalArgumentException JavaDoc(
72           "Chromosome instance must not be null!");
73     }
74     // First cleanup the chromosome's genes before returning it back
75
// to the pool.
76
// -------------------------------------------------------------
77
Gene[] genes = a_chromosome.getGenes();
78     int size = a_chromosome.size();
79     for (int i = 0; i < size; i++) {
80       genes[i].cleanup();
81     }
82     // Now add it to the pool.
83
// -----------------------
84
m_chromosomePool.releaseObject(a_chromosome);
85   }
86 }
87
Popular Tags