KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Ordered chain of NaturalSelectors. With this container you can plugin
20  * NaturalSelector implementations which will be performed either before (pre-)
21  * or after (post-selectors) registered genetic operations have been applied.
22  *
23  * @see Configuration
24  * @see Genotype
25  *
26  * @author Klaus Meffert
27  * @since 1.1
28  */

29 public class ChainOfSelectors
30     implements Serializable, ICloneable, Comparable JavaDoc {
31   /** String containing the CVS revision. Read out via reflection!*/
32   private final static String JavaDoc CVS_REVISION = "$Revision: 1.13 $";
33
34   /**
35    * Ordered list holding the NaturalSelector's.
36    * Intentionally used as a decorator and not via inheritance!
37    */

38   private Vector m_selectors;
39
40   public ChainOfSelectors() {
41     m_selectors = new Vector();
42   }
43
44   /**
45    * Adds a natural selector to the chain.
46    *
47    * @param a_selector the selector to be added
48    * @throws InvalidConfigurationException
49    *
50    * @author Klaus Meffert
51    * @since 1.1 (previously part of class Configuration)
52    */

53   public void addNaturalSelector(NaturalSelector a_selector)
54       throws InvalidConfigurationException {
55     if (a_selector == null) {
56       throw new InvalidConfigurationException(
57           "This Configuration object is locked. Settings may not be " +
58           "altered.");
59     }
60     m_selectors.add(a_selector);
61   }
62
63   /**
64    *
65    * @param a_c Collection to add all elements from
66    * @throws InvalidConfigurationException
67    *
68    * @author Klaus Meffert
69    * @since 1.1
70    */

71   public void addAll(Collection a_c)
72       throws InvalidConfigurationException {
73     Iterator it = a_c.iterator();
74     while (it.hasNext()) {
75       NaturalSelector selector = (NaturalSelector) it.next();
76       addNaturalSelector(selector);
77     }
78   }
79
80   /**
81    * @return number of selectors in list
82    *
83    * @author Klaus Meffert
84    * @since 1.1 (previously part of class Configuration)
85    */

86   public int size() {
87     return m_selectors.size();
88   }
89
90   /**
91    * @return true if number of selectors is zero
92    *
93    * @author Klaus Meffert
94    * @since 1.1
95    */

96   public boolean isEmpty() {
97     return size() == 0;
98   }
99
100   public int hashCode() {
101     return m_selectors.hashCode();
102   }
103
104   /**
105    *
106    * @param a_obj Object
107    * @return boolean
108    *
109    * @author Klaus Meffert
110    * @since 1.1
111    */

112   public boolean equals(final Object JavaDoc a_obj) {
113     try {
114       ChainOfSelectors c2 = (ChainOfSelectors) a_obj;
115       if (c2 == null) {
116         return false;
117       }
118       return m_selectors.equals(c2.m_selectors);
119     } catch (ClassCastException JavaDoc cex) {
120       return false;
121     }
122   }
123
124   /**
125    * Returns a Selector with specific index in the list.
126    *
127    * @param a_index the index of the Selector to read from the list
128    * @return NaturalSelector
129    *
130    * @author Klaus Meffert
131    * @since 1.1
132    */

133   public NaturalSelector get(final int a_index) {
134     return (NaturalSelector) m_selectors.get(a_index);
135   }
136
137   /**
138    *
139    * @author Klaus Meffert
140    * @since 1.1
141    *
142    */

143   public void clear() {
144     m_selectors.clear();
145   }
146
147   /**
148    * @return Iterator for iterating over list of selectors
149    *
150    * @author Klaus Meffert
151    * @since 1.1
152    */

153   public Iterator iterator() {
154     return m_selectors.iterator();
155   }
156
157   /**
158    * @return deep clone of this instance
159    *
160    * @author Klaus Meffert
161    * @since 3.2
162    */

163   public Object JavaDoc clone() {
164     ChainOfSelectors result = new ChainOfSelectors();
165     result.m_selectors = (Vector) m_selectors.clone();
166     return result;
167   }
168
169   /**
170    * The compareTo-method.
171    *
172    * @param a_other the other object to compare
173    * @return -1, 0, 1
174    *
175    * @author Klaus Meffert
176    * @since 3.2
177    */

178   public int compareTo(Object JavaDoc a_other) {
179     if (a_other == null) {
180       return 1;
181     }
182     else {
183       ChainOfSelectors other = (ChainOfSelectors) a_other;
184       return new CompareToBuilder()
185           .append(m_selectors.toArray(), other.m_selectors.toArray())
186           .toComparison();
187     }
188   }
189 }
190
Popular Tags