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