EvolutionInit.java
001 /*
002  * Java Genetic Algorithm Library (jenetics-6.1.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.engine;
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 import static io.jenetics.internal.util.SerialIO.readLong;
026 import static io.jenetics.internal.util.SerialIO.writeLong;
027 
028 import java.io.IOException;
029 import java.io.InvalidObjectException;
030 import java.io.ObjectInput;
031 import java.io.ObjectInputStream;
032 import java.io.ObjectOutput;
033 import java.io.Serializable;
034 import java.util.Objects;
035 
036 import io.jenetics.Gene;
037 import io.jenetics.Genotype;
038 import io.jenetics.internal.util.Requires;
039 import io.jenetics.util.ISeq;
040 
041 /**
042  * Represents the initialization value of an evolution stream/iterator.
043  *
044  @see EvolutionStart
045  @see EvolutionStreamable#stream(EvolutionInit)
046  *
047  @param <G> the gene type
048  *
049  * @implNote
050  * This class is immutable and thread-safe.
051  *
052  @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
053  @version 6.0
054  @since 4.1
055  */
056 public final /*record*/ class EvolutionInit<G extends Gene<?, G>>
057     implements Serializable
058 {
059 
060     private static final long serialVersionUID = 1L;
061 
062     private final ISeq<Genotype<G>> _population;
063     private final long _generation;
064 
065     private EvolutionInit(
066         final ISeq<Genotype<G>> population,
067         final long generation
068     ) {
069         _population = requireNonNull(population);
070         _generation = Requires.positive(generation);
071     }
072 
073     /**
074      * Return the initial population.
075      *
076      @return the initial population
077      */
078     public ISeq<Genotype<G>> population() {
079         return _population;
080     }
081 
082     /**
083      * Return the generation of the start population.
084      *
085      @return the start generation
086      */
087     public long generation() {
088         return _generation;
089     }
090 
091     @Override
092     public int hashCode() {
093         return hash(_generation, hash(_population));
094     }
095 
096     @Override
097     public boolean equals(final Object obj) {
098         return obj == this ||
099             obj instanceof EvolutionInit &&
100             _generation == ((EvolutionInit)obj)._generation &&
101             Objects.equals(_population, ((EvolutionInit)obj)._population);
102     }
103 
104     @Override
105     public String toString() {
106         return format(
107             "EvolutionStart[population-size=%d, generation=%d]",
108             _population.size(), _generation
109         );
110     }
111 
112     /**
113      * Create a new evolution start object with the given population and for the
114      * given generation.
115      *
116      @param <G> the gene type
117      @param population the start population.
118      @param generation the start generation of the population
119      @return a new evolution start object
120      @throws java.lang.NullPointerException if the given {@code population} is
121      *         {@code null}.
122      @throws IllegalArgumentException if the given {@code generation} is
123      *         smaller then one
124      */
125     public static <G extends Gene<?, G>>
126     EvolutionInit<G> of(
127         final ISeq<Genotype<G>> population,
128         final long generation
129     ) {
130         return new EvolutionInit<>(population, generation);
131     }
132 
133 
134     /* *************************************************************************
135      *  Java object serialization
136      * ************************************************************************/
137 
138     private Object writeReplace() {
139         return new Serial(Serial.EVOLUTION_INIT, this);
140     }
141 
142     private void readObject(final ObjectInputStream stream)
143         throws InvalidObjectException
144     {
145         throw new InvalidObjectException("Serialization proxy required.");
146     }
147 
148     void write(final ObjectOutput outthrows IOException {
149         out.writeObject(_population);
150         writeLong(_generation, out);
151     }
152 
153     @SuppressWarnings({"unchecked""rawtypes"})
154     static Object read(final ObjectInput in)
155         throws IOException, ClassNotFoundException
156     {
157         return new EvolutionInit(
158             (ISeq)in.readObject(),
159             readLong(in)
160         );
161     }
162 
163 }