MutatorResult.java
01 /*
02  * Java Genetic Algorithm Library (jenetics-8.0.0).
03  * Copyright (c) 2007-2024 Franz Wilhelmstötter
04  *
05  * Licensed under the Apache License, Version 2.0 (the "License");
06  * you may not use this file except in compliance with the License.
07  * You may obtain a copy of the License at
08  *
09  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * Author:
18  *    Franz Wilhelmstötter (franz.wilhelmstoetter@gmail.com)
19  */
20 package io.jenetics;
21 
22 import static java.util.Objects.requireNonNull;
23 
24 import java.io.Serial;
25 import java.io.Serializable;
26 import java.util.function.Function;
27 import java.util.random.RandomGenerator;
28 
29 import io.jenetics.internal.util.Requires;
30 
31 /**
32  * Represents the result pair of one of the four {@code Mutator.mutate} calls.
33  *
34  @see Mutator#mutate(Phenotype, long, double, RandomGenerator)
35  @see Mutator#mutate(Genotype, double, RandomGenerator)
36  @see Mutator#mutate(Chromosome, double, RandomGenerator)
37  @see Mutator#mutate(Gene, RandomGenerator)
38  *
39  @param <T> the mutation result type
40  @param result the mutation result
41  @param mutations the number of mutations applied while creating the mutation
42  *        result
43  *
44  @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
45  @version 7.0
46  @since 4.0
47  */
48 public record MutatorResult<T>(T result, int mutations)
49     implements Serializable
50 {
51 
52     @Serial
53     private static final long serialVersionUID = 2L;
54 
55     /**
56      * Create a new mutation result with the given values.
57      *
58      @param result the mutation result
59      @param mutations the number of mutations
60      @throws IllegalArgumentException if the given {@code mutations} is
61      *         negative
62      @throws NullPointerException if the given mutation result is {@code null}
63      */
64     public MutatorResult {
65         requireNonNull(result);
66         Requires.nonNegative(mutations);
67     }
68 
69     /**
70      * Maps this mutation result to type {@code B} using the given {@code mapper}.
71      *
72      @param mapper the mutation result mapper
73      @param <B> the new mutation result type
74      @return a new mapped mutation result
75      @throws NullPointerException if the given {@code mapper} is {@code null}
76      */
77     <B> MutatorResult<B> map(final Function<? super T, ? extends B> mapper) {
78         requireNonNull(mapper);
79         return new MutatorResult<>(mapper.apply(result), mutations);
80     }
81 
82 }