001 /*
002 * Java Genetic Algorithm Library (jenetics-5.2.0).
003 * Copyright (c) 2007-2020 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 5.2
042 * @since 4.0
043 */
044 public final /*record*/ class AltererResult<
045 G extends Gene<?, G>,
046 C 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>> population() {
075 return _population;
076 }
077
078 /**
079 * Return the altered population.
080 *
081 * @return the altered population
082 * @deprecated Use {@link #population()} instead
083 */
084 @Deprecated
085 public ISeq<Phenotype<G, C>> getPopulation() {
086 return _population;
087 }
088
089 /**
090 * Return the number of altered individuals.
091 *
092 * @return the number of altered individuals
093 */
094 public int alterations() {
095 return _alterations;
096 }
097
098 /**
099 * Return the number of altered individuals.
100 *
101 * @return the number of altered individuals
102 * @deprecated Use {@link #alterations()} instead
103 */
104 @Deprecated
105 public int getAlterations() {
106 return _alterations;
107 }
108
109 @Override
110 public int hashCode() {
111 return
112 hash(_population,
113 hash(_alterations));
114 }
115
116 @Override
117 public boolean equals(final Object obj) {
118 return obj == this ||
119 obj instanceof AltererResult &&
120 _alterations == ((AltererResult)obj)._alterations &&
121 _population.equals(((AltererResult)obj)._population);
122 }
123
124 @Override
125 public String toString() {
126 return format("[%s, %s]", _population, _alterations);
127 }
128
129 /**
130 * Return a new alter result for the given arguments.
131 *
132 * @param population the altered population
133 * @param alterations the number of altered individuals
134 * @param <G> the gene type
135 * @param <C> the result type
136 * @return a new alterer for the given arguments
137 * @throws NullPointerException if the given population is {@code null}
138 * @throws IllegalArgumentException if the given {@code alterations} is
139 * negative
140 */
141 public static <G extends Gene<?, G>, C extends Comparable<? super C>>
142 AltererResult<G, C> of(
143 final ISeq<Phenotype<G, C>> population,
144 final int alterations
145 ) {
146 return new AltererResult<>(population, alterations);
147 }
148
149 /**
150 * Return a new alter result for the given arguments.
151 *
152 * @param population the altered population
153 * @param <G> the gene type
154 * @param <C> the result type
155 * @return a new alterer for the given arguments
156 * @throws NullPointerException if the given population is {@code null}
157 */
158 public static <G extends Gene<?, G>, C extends Comparable<? super C>>
159 AltererResult<G, C>
160 of(final ISeq<Phenotype<G, C>> population) {
161 return new AltererResult<>(population, 0);
162 }
163
164 }
|