1 10 package org.jgap.impl; 11 12 import org.jgap.*; 13 14 29 public class BooleanGene 30 extends BaseGene implements IPersistentRepresentation { 31 32 private final static String CVS_REVISION = "$Revision: 1.30 $"; 33 34 39 protected static final Boolean TRUE_BOOLEAN = Boolean.valueOf(true); 40 41 46 protected static final Boolean FALSE_BOOLEAN = Boolean.valueOf(false); 47 48 51 private Boolean m_value; 52 53 62 public BooleanGene() 63 throws InvalidConfigurationException { 64 this(Genotype.getStaticConfiguration()); 65 } 66 67 74 public BooleanGene(final Configuration a_config) 75 throws InvalidConfigurationException { 76 super(a_config); 77 } 78 79 87 public BooleanGene(final Configuration a_config, final boolean a_value) 88 throws InvalidConfigurationException { 89 super(a_config); 90 m_value = new Boolean (a_value); 91 } 92 93 101 public BooleanGene(final Configuration a_config, final Boolean a_value) 102 throws InvalidConfigurationException { 103 super(a_config); 104 if (a_value == null) { 105 throw new IllegalArgumentException ("Allele value may not be null. Use" 106 + " no argument constructor if you" 107 + " need to set allele to null" 108 + " initially."); 109 } 110 else { 111 m_value = a_value; 112 } 113 } 114 115 125 protected Gene newGeneInternal() { 126 try { 127 return new BooleanGene(getConfiguration()); 128 } 129 catch (InvalidConfigurationException iex) { 130 throw new IllegalStateException (iex.getMessage()); 131 } 132 } 133 134 140 public void setAllele(final Object a_newValue) { 141 m_value = (Boolean ) a_newValue; 142 } 143 144 158 public String getPersistentRepresentation() { 159 String s; 160 if (getInternalValue() == null) { 161 s = "null"; 162 } 163 else { 164 s = getInternalValue().toString(); 165 } 166 return s; 167 } 168 169 189 public void setValueFromPersistentRepresentation(String a_representation) 190 throws UnsupportedRepresentationException { 191 if (a_representation != null) { 192 if (a_representation.equals("null")) { 193 m_value = null; 194 } 195 else if (a_representation.equals("true")) { 196 m_value = TRUE_BOOLEAN; 197 } 198 else if (a_representation.equals("false")) { 199 m_value = FALSE_BOOLEAN; 200 } 201 else { 202 throw new UnsupportedRepresentationException( 203 "Unknown boolean gene representation: " + 204 a_representation); 205 } 206 } 207 else { 208 throw new UnsupportedRepresentationException( 209 "The input parameter must not be null!"); 210 } 211 } 212 213 219 public boolean booleanValue() { 220 return m_value.booleanValue(); 221 } 222 223 239 public void setToRandomValue(RandomGenerator a_numberGenerator) { 240 if (a_numberGenerator.nextBoolean() == true) { 241 m_value = TRUE_BOOLEAN; 242 } 243 else { 244 m_value = FALSE_BOOLEAN; 245 } 246 } 247 248 264 public int compareTo(Object a_other) { 265 BooleanGene otherBooleanGene = (BooleanGene) a_other; 266 if (otherBooleanGene == null) { 269 return 1; 270 } 271 else if (otherBooleanGene.m_value == null) { 272 if (m_value != null) { 276 return 1; 277 } 278 else { 279 if (isCompareApplicationData()) { 280 return compareApplicationData(getApplicationData(), 281 otherBooleanGene.getApplicationData()); 282 } 283 else { 284 return 0; 285 } 286 } 287 } 288 if (m_value == null) { 289 if (otherBooleanGene.m_value == null) { 290 if (isCompareApplicationData()) { 291 return compareApplicationData(getApplicationData(), 292 otherBooleanGene.getApplicationData()); 293 } 294 else { 295 return 0; 296 } 297 } 298 else { 299 return -1; 300 } 301 } 302 if (m_value.booleanValue() == false) { 306 if (otherBooleanGene.m_value.booleanValue() == false) { 307 if (isCompareApplicationData()) { 310 return compareApplicationData(getApplicationData(), 311 otherBooleanGene.getApplicationData()); 312 } 313 else { 314 return 0; 315 } 316 } 317 else { 318 return -1; 322 } 323 } 324 else if (otherBooleanGene.m_value.booleanValue() == true) { 325 if (isCompareApplicationData()) { 328 return compareApplicationData(getApplicationData(), 329 otherBooleanGene.getApplicationData()); 330 } 331 else { 332 return 0; 333 } 334 } 335 else { 336 return 1; 340 } 341 } 342 343 353 public void applyMutation(int a_index, double a_percentage) { 354 if (m_value == null) { 355 m_value = Boolean.valueOf(false); 356 } 357 else if (a_percentage > 0) { 358 if (!m_value.booleanValue()) { 361 m_value = Boolean.valueOf(true); 362 } 363 } 364 else if (a_percentage < 0) { 365 if (m_value.booleanValue()) { 368 m_value = Boolean.valueOf(false); 369 } 370 } 371 } 372 373 protected Object getInternalValue() { 374 return m_value; 375 } 376 377 385 public int hashCode() { 386 if (getInternalValue() == null) { 387 return -2; 388 } 389 else { 390 return super.hashCode(); 391 } 392 } 393 394 401 public String toString() { 402 String s = "BooleanGene" 403 + "="; 404 if (getInternalValue() == null) { 405 s += "null"; 406 } 407 else { 408 s += getInternalValue().toString(); 409 } 410 return s; 411 } 412 } 413 | Popular Tags |