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