001 /*
002 * Java Genetic Algorithm Library (jenetics-5.0.0).
003 * Copyright (c) 2007-2019 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 4.0
044 * @since 4.0
045 */
046 public final 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 getResult() {
083 return _result;
084 }
085
086 /**
087 * Return the number of mutations for this mutation result.
088 *
089 * @return the number of mutations
090 */
091 public int getMutations() {
092 return _mutations;
093 }
094
095 /**
096 * Create a new mutation result with the given values.
097 *
098 * @param result the mutation result
099 * @param mutations the number of mutations
100 * @param <T> the mutation result type
101 * @return a new mutation result
102 * @throws IllegalArgumentException if the given {@code mutations} is
103 * negative
104 * @throws NullPointerException if the given mutation result is {@code null}
105 */
106 public static <T> MutatorResult<T> of(final T result, final int mutations) {
107 return new MutatorResult<>(result, mutations);
108 }
109
110 /**
111 * Create a new mutation result with the given result. The number of
112 * mutations is set to zero.
113 *
114 * @param result the mutation result
115 * @param <T> the mutation result type
116 * @return a new mutation result
117 * @throws NullPointerException if the given mutation result is {@code null}
118 */
119 public static <T> MutatorResult<T> of(final T result) {
120 return new MutatorResult<>(result, 0);
121 }
122
123 @Override
124 public int hashCode() {
125 return hash(_result, hash(_mutations));
126 }
127
128 @Override
129 public boolean equals(final Object obj) {
130 return obj == this ||
131 obj instanceof MutatorResult &&
132 Objects.equals(((MutatorResult)obj)._result, _result) &&
133 ((MutatorResult)obj)._mutations == _mutations;
134 }
135
136 @Override
137 public String toString() {
138 return format("MutatorResult[%s, %s]", _result, _mutations);
139 }
140
141 }
|