AbstractAlterer.java
001 /*
002  * Java Genetic Algorithm Library (jenetics-2.0.2).
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 2.0 &mdash; <em>$Date: 2014-03-30 $</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      @param <G> the <i>gene</i> typ
042      @return an alterer which does nothing.
043      */
044     public static <G extends Gene<?, G>> Alterer<G> Null() {
045         return new Alterer<G>() {
046             @Override
047             public <C extends Comparable<? super C>> int alter(
048                 final Population<G, C> population,
049                 final int generation
050             ) {
051                 return 0;
052             }
053 
054             @Override
055             public int hashCode() {
056                 return HashBuilder.of(getClass()).value();
057             }
058 
059             @Override
060             public boolean equals(final Object obj) {
061                 return obj == this ||
062                         obj != null && obj.getClass() == getClass();
063             }
064 
065             @Override
066             public String toString() {
067                 return "Alterer.Null";
068             }
069         };
070     }
071 
072     public static final double DEFAULT_ALTER_PROBABILITY = 0.2;
073 
074     /**
075      * The altering probability.
076      */
077     protected final double _probability;
078 
079     /**
080      * Constructs an alterer with a given recombination probability.
081      *
082      @param probability The recombination probability.
083      @throws IllegalArgumentException if the {@code probability} is not in the
084      *         valid range of {@code [0, 1]}.
085      */
086     protected AbstractAlterer(final double probability) {
087         _probability = checkProbability(probability);
088     }
089 
090     /**
091      * Return the recombination/alter probability for this alterer.
092      *
093      @return The recombination probability.
094      */
095     public double getProbability() {
096         return _probability;
097     }
098 
099     @Override
100     public int hashCode() {
101         return HashBuilder.of(getClass()).and(_probability).value();
102     }
103 
104     @Override
105     public boolean equals(final Object obj) {
106         if (obj == this) {
107             return true;
108         }
109         if (!(obj instanceof AbstractAlterer<?>)) {
110             return false;
111         }
112 
113         final AbstractAlterer<?> alterer = (AbstractAlterer<?>)obj;
114         return eq(_probability, alterer._probability);
115     }
116 }