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