AltererResult.java
001 /*
002  * Java Genetic Algorithm Library (jenetics-6.3.0).
003  * Copyright (c) 2007-2021 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 6.0
042  @since 4.0
043  */
044 public final /*record*/ 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 Seq<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).asISeq();
066         _alterations = alterations;
067     }
068 
069     /**
070      * Return the altered population.
071      *
072      @return the altered population
073      */
074     public ISeq<Phenotype<G, C>> population() {
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 alterations() {
084         return _alterations;
085     }
086 
087     @Override
088     public int hashCode() {
089         return
090             hash(_population,
091             hash(_alterations));
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 Seq<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>
138     of(final Seq<Phenotype<G, C>> population) {
139         return new AltererResult<>(population, 0);
140     }
141 
142 }