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