AbstractAlterer.java
001 /*
002  * Java Genetic Algorithm Library (jenetics-1.6.0).
003  * Copyright (c) 2007-2014 Franz Wilhelmstötter
004  *
005  * Licensed under the Apache License, Version 2.0 (the "License");
006  * you may not use this file except in compliance with the License.
007  * You may obtain a copy of the License at
008  *
009  *      http://www.apache.org/licenses/LICENSE-2.0
010  *
011  * Unless required by applicable law or agreed to in writing, software
012  * distributed under the License is distributed on an "AS IS" BASIS,
013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014  * See the License for the specific language governing permissions and
015  * limitations under the License.
016  *
017  * Author:
018  *    Franz Wilhelmstötter (franz.wilhelmstoetter@gmx.at)
019  */
020 package org.jenetics;
021 
022 import static org.jenetics.internal.util.object.checkProbability;
023 import static org.jenetics.internal.util.object.eq;
024 
025 import org.jenetics.internal.util.HashBuilder;
026 
027 /**
028  * Abstract implementation of the alterer interface.
029  *
030  @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
031  @since 1.0
032  @version 1.0 &mdash; <em>$Date: 2014-03-01 $</em>
033  */
034 public abstract class AbstractAlterer<G extends Gene<?, G>>
035     implements Alterer<G>
036 {
037 
038     /**
039      * Return an alterer which does nothing.
040      *
041      @return an alterer which does nothing.
042      */
043     public static <G extends Gene<?, G>> Alterer<G> Null() {
044         return new Alterer<G>() {
045             @Override
046             public <C extends Comparable<? super C>> int alter(
047                 final Population<G, C> population,
048                 final int generation
049             ) {
050                 return 0;
051             }
052 
053             @Override
054             public int hashCode() {
055                 return HashBuilder.of(getClass()).value();
056             }
057 
058             @Override
059             public boolean equals(final Object obj) {
060                 return obj == this ||
061                         obj != null && obj.getClass() == getClass();
062             }
063 
064             @Override
065             public String toString() {
066                 return "Alterer.Null";
067             }
068         };
069     }
070 
071     public static final double DEFAULT_ALTER_PROBABILITY = 0.2;
072 
073     /**
074      * The altering probability.
075      */
076     protected final double _probability;
077 
078     /**
079      * Constructs an alterer with a given recombination probability.
080      *
081      @param probability The recombination probability.
082      @throws IllegalArgumentException if the {@code probability} is not in the
083      *         valid range of {@code [0, 1]}.
084      */
085     protected AbstractAlterer(final double probability) {
086         _probability = checkProbability(probability);
087     }
088 
089     /**
090      * Return the recombination/alter probability for this alterer.
091      *
092      @return The recombination probability.
093      */
094     public double getProbability() {
095         return _probability;
096     }
097 
098     @Override
099     public int hashCode() {
100         return HashBuilder.of(getClass()).and(_probability).value();
101     }
102 
103     @Override
104     public boolean equals(final Object obj) {
105         if (obj == this) {
106             return true;
107         }
108         if (!(obj instanceof AbstractAlterer<?>)) {
109             return false;
110         }
111 
112         final AbstractAlterer<?> alterer = (AbstractAlterer<?>)obj;
113         return eq(_probability, alterer._probability);
114     }
115 }