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 import java.util.Objects;
028 import java.util.Random;
029 import java.util.function.Function;
030
031 /**
032 * Represents the result pair of one of the four {@code Mutator.mutate} calls.
033 *
034 * @see Mutator#mutate(Phenotype, long, double, Random)
035 * @see Mutator#mutate(Genotype, double, Random)
036 * @see Mutator#mutate(Chromosome, double, Random)
037 * @see Mutator#mutate(Gene, Random)
038 *
039 * @implSpec
040 * This class is immutable and thread-safe.
041 *
042 * @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
043 * @version 5.2
044 * @since 4.0
045 */
046 public final /*record*/ class MutatorResult<T> implements Serializable {
047
048 private static final long serialVersionUID = 1L;
049
050 private final T _result;
051 private final int _mutations;
052
053 private MutatorResult(final T result, final int mutations) {
054 if (mutations < 0) {
055 throw new IllegalArgumentException(
056 "Mutations must not be negative: " + mutations
057 );
058 }
059
060 _result = requireNonNull(result);
061 _mutations = mutations;
062 }
063
064 /**
065 * Maps this mutation result to type {@code B} using the given {@code mapper}.
066 *
067 * @param mapper the mutation result mapper
068 * @param <B> the new mutation result type
069 * @return a new mapped mutation result
070 * @throws NullPointerException if the given {@code mapper} is {@code null}
071 */
072 <B> MutatorResult<B> map(final Function<? super T, ? extends B> mapper) {
073 requireNonNull(mapper);
074 return of(mapper.apply(_result), _mutations);
075 }
076
077 /**
078 * Return the mutation result.
079 *
080 * @return the mutation result
081 */
082 public T result() {
083 return _result;
084 }
085
086 /**
087 * Return the mutation result.
088 *
089 * @return the mutation result
090 * @deprecated Use {@link #result()} instead
091 */
092 @Deprecated
093 public T getResult() {
094 return _result;
095 }
096
097 /**
098 * Return the number of mutations for this mutation result.
099 *
100 * @return the number of mutations
101 */
102 public int mutations() {
103 return _mutations;
104 }
105
106 /**
107 * Return the number of mutations for this mutation result.
108 *
109 * @return the number of mutations
110 * @deprecated Use {@link #mutations()} instead
111 */
112 @Deprecated
113 public int getMutations() {
114 return _mutations;
115 }
116
117 /**
118 * Create a new mutation result with the given values.
119 *
120 * @param result the mutation result
121 * @param mutations the number of mutations
122 * @param <T> the mutation result type
123 * @return a new mutation result
124 * @throws IllegalArgumentException if the given {@code mutations} is
125 * negative
126 * @throws NullPointerException if the given mutation result is {@code null}
127 */
128 public static <T> MutatorResult<T> of(final T result, final int mutations) {
129 return new MutatorResult<>(result, mutations);
130 }
131
132 /**
133 * Create a new mutation result with the given result. The number of
134 * mutations is set to zero.
135 *
136 * @param result the mutation result
137 * @param <T> the mutation result type
138 * @return a new mutation result
139 * @throws NullPointerException if the given mutation result is {@code null}
140 */
141 public static <T> MutatorResult<T> of(final T result) {
142 return new MutatorResult<>(result, 0);
143 }
144
145 @Override
146 public int hashCode() {
147 return hash(_result, hash(_mutations));
148 }
149
150 @Override
151 public boolean equals(final Object obj) {
152 return obj == this ||
153 obj instanceof MutatorResult &&
154 Objects.equals(((MutatorResult)obj)._result, _result) &&
155 ((MutatorResult)obj)._mutations == _mutations;
156 }
157
158 @Override
159 public String toString() {
160 return format("MutatorResult[%s, %s]", _result, _mutations);
161 }
162
163 }
|