AltererResult.java
001 /*
002  * Java Genetic Algorithm Library (jenetics-4.2.0).
003  * Copyright (c) 2007-2018 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@gmail.com)
019  */
020 package io.jenetics;
021 
022 import static java.lang.String.format;
023 import static java.util.Objects.requireNonNull;
024 
025 import java.io.Serializable;
026 
027 import io.jenetics.util.ISeq;
028 import io.jenetics.util.Seq;
029 
030 /**
031  * Represents the result pair of a {@link Alterer#alter(Seq, long)} call, which
032  * consists of the altered population and the number of altered individuals.
033  *
034  @see Alterer
035  *
036  * @implSpec
037  * This class is immutable and thread-safe.
038  *
039  @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
040  @version 4.0
041  @since 4.0
042  */
043 public final class AltererResult<
044     extends Gene<?, G>,
045     extends Comparable<? super C>
046 >
047     implements Serializable
048 {
049     private static final long serialVersionUID = 1L;
050 
051     private final ISeq<Phenotype<G, C>> _population;
052     private final int _alterations;
053 
054     private AltererResult(
055         final ISeq<Phenotype<G, C>> population,
056         final int alterations
057     ) {
058         if (alterations < 0) {
059             throw new IllegalArgumentException(
060                 "Alterations must not be negative: " + alterations
061             );
062         }
063 
064         _population = requireNonNull(population);
065         _alterations = alterations;
066     }
067 
068     /**
069      * Return the altered population.
070      *
071      @return the altered population
072      */
073     public ISeq<Phenotype<G, C>> getPopulation() {
074         return _population;
075     }
076 
077     /**
078      * Return the number of altered individuals.
079      *
080      @return the number of altered individuals
081      */
082     public int getAlterations() {
083         return _alterations;
084     }
085 
086     @Override
087     public int hashCode() {
088         int hash = 17;
089         hash += 31*_population.hashCode() 37;
090         hash += 31*_alterations + 37;
091         return hash;
092     }
093 
094     @Override
095     public boolean equals(final Object obj) {
096         return obj == this ||
097             obj instanceof AltererResult &&
098             _alterations == ((AltererResult)obj)._alterations &&
099             _population.equals(((AltererResult)obj)._population);
100     }
101 
102     @Override
103     public String toString() {
104         return format("[%s, %s]", _population, _alterations);
105     }
106 
107     /**
108      * Return a new alter result for the given arguments.
109      *
110      @param population the altered population
111      @param alterations the number of altered individuals
112      @param <G> the gene type
113      @param <C> the result type
114      @return a new alterer for the given arguments
115      @throws NullPointerException if the given population is {@code null}
116      @throws IllegalArgumentException if the given {@code alterations} is
117      *         negative
118      */
119     public static <G extends Gene<?, G>, C extends Comparable<? super C>>
120     AltererResult<G, C> of(
121         final ISeq<Phenotype<G, C>> population,
122         final int alterations
123     ) {
124         return new AltererResult<>(population, alterations);
125     }
126 
127     /**
128      * Return a new alter result for the given arguments.
129      *
130      @param population the altered population
131      @param <G> the gene type
132      @param <C> the result type
133      @return a new alterer for the given arguments
134      @throws NullPointerException if the given population is {@code null}
135      */
136     public static <G extends Gene<?, G>, C extends Comparable<? super C>>
137     AltererResult<G, C> of(final ISeq<Phenotype<G, C>> population) {
138         return new AltererResult<>(population, 0);
139     }
140 
141 }