001/*
002 * Java Genetic Algorithm Library (jenetics-8.2.0).
003 * Copyright (c) 2007-2025 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 */
020package io.jenetics;
021
022import static java.util.Objects.requireNonNull;
023import static io.jenetics.internal.util.Hashes.hash;
024import static io.jenetics.util.RandomRegistry.random;
025
026import java.util.Objects;
027import java.util.function.Predicate;
028import java.util.function.Supplier;
029
030import io.jenetics.util.ISeq;
031import io.jenetics.util.IntRange;
032import io.jenetics.util.MSeq;
033
034/**
035 * {@code Gene} implementation, which allows creating genes without explicit
036 * implementing the {@code Gene} interface.
037 *
038 * {@snippet lang="java":
039 * class Main {
040 *     // First monday of 2015.
041 *     private static final LocalDate MIN_MONDAY = LocalDate.of(2015, 1, 5);
042 *
043 *     // Supplier of random 'LocalDate' objects. The implementation is responsible
044 *     // for guaranteeing the desired allele restriction. In this case, we will
045 *     // generate only mondays.
046 *     static LocalDate nextRandomMonday() {
047 *         return MIN_MONDAY.plusWeeks(RandomRegistry.getRandom().nextInt(1000));
048 *     }
049 *
050 *     // Create a new 'LocalDate' gene. All other genes, created with
051 *     // gene.newInstance(), are calling the 'newRandomMonday' method.
052 *     final AnyGene<LocalDate> gene = AnyGene.of(Main::nextRandomMonday);
053 * }
054 * }
055 * The example above shows how to create {@code LocalDate} genes from a random
056 * {@code LocalDate} supplier. It also shows how to implement a restriction on
057 * the created dates. The usage of the {@code AnyGene} class is useful for
058 * supporting custom allele types without explicit implementation of the
059 * {@code Gene} interface. But the {@code AnyGene} can only be used for a subset
060 * of the existing alterers.
061 *
062 * @see AnyChromosome
063 *
064 * @implNote
065 * This class is immutable and thread-safe.
066 *
067 * @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
068 * @version 6.0
069 * @since 3.3
070 */
071public final class AnyGene<A> implements Gene<A, AnyGene<A>> {
072
073        private final A _allele;
074        private final Supplier<? extends A> _supplier;
075        private final Predicate<? super A> _validator;
076
077        private AnyGene(
078                final A allele,
079                final Supplier<? extends A> supplier,
080                final Predicate<? super A> validator
081        ) {
082                _allele = allele;
083                _supplier = requireNonNull(supplier);
084                _validator = requireNonNull(validator);
085        }
086
087        @Override
088        public A allele() {
089                return _allele;
090        }
091
092        @Override
093        public AnyGene<A> newInstance() {
094                return new AnyGene<>(_supplier.get(), _supplier, _validator);
095        }
096
097        @Override
098        public AnyGene<A> newInstance(final A value) {
099                return new AnyGene<>(value, _supplier, _validator);
100        }
101
102        @Override
103        public boolean isValid() {
104                return _validator.test(_allele);
105        }
106
107        @Override
108        public int hashCode() {
109                return hash(_allele, hash(AnyGene.class));
110        }
111
112        @Override
113        public boolean equals(final Object obj) {
114                return obj instanceof AnyGene<?> other &&
115                        Objects.equals(other._allele, _allele);
116        }
117
118        @Override
119        public String toString() {
120                return Objects.toString(_allele);
121        }
122
123
124        /* *************************************************************************
125         *  Static factory methods.
126         * ************************************************************************/
127
128        /**
129         * Create a new {@code AnyGene} instance with the given parameters. New
130         * (random) genes are created with the given allele {@code supplier}.
131         *
132         * @param <A> the allele type
133         * @param allele the actual allele instance the created gene represents.
134         *        {@code null} values are allowed.
135         * @param supplier the allele-supplier which is used for creating new,
136         *        random alleles
137         * @param validator the validator used for validating the created gene. This
138         *        predicate is used in the {@link #isValid()} method.
139         * @return a new {@code AnyGene} with the given parameters
140         * @throws NullPointerException if the {@code supplier} or {@code validator}
141         *         is {@code null}
142         */
143        public static <A> AnyGene<A> of(
144                final A allele,
145                final Supplier<? extends A> supplier,
146                final Predicate<? super A> validator
147        ) {
148                return new AnyGene<>(allele, supplier, validator);
149        }
150
151        /**
152         * Create a new {@code AnyGene} instance with the given parameters. New
153         * (random) genes are created with the given allele {@code supplier}. The
154         * {@code validator} predicate of the generated gene will always return
155         * {@code true}.
156         *
157         * @param <A> the allele type
158         * @param allele the actual allele instance the created gene represents.
159         *        {@code null} values are allowed.
160         * @param supplier the allele-supplier which is used for creating new,
161         *        random alleles
162         * @return a new {@code AnyGene} with the given parameters
163         * @throws NullPointerException if the {@code suppler} is {@code null}
164         */
165        public static <A> AnyGene<A> of(
166                final A allele,
167                final Supplier<? extends A> supplier
168        ) {
169                return new AnyGene<>(allele, supplier, a -> true);
170        }
171
172        /**
173         * Create a new {@code AnyGene} instance with the given allele
174         * {@code supplier}. The {@code validator} predicate of the generated gene
175         * will always return {@code true}.
176         *
177         * @param <A> the allele type
178         * @param supplier the allele-supplier which is used for creating new,
179         *        random alleles
180         * @return a new {@code AnyGene} with the given parameters
181         * @throws NullPointerException if one of the parameters is {@code null}
182         */
183        public static <A> AnyGene<A> of(final Supplier<? extends A> supplier) {
184                return new AnyGene<>(supplier.get(), supplier, a -> true);
185        }
186
187        /**
188         * Create a new {@code AnyGene} instance with the given parameters. New
189         * (random) genes are created with the given allele {@code supplier}.
190         *
191         * @param <A> the allele type
192         * @param supplier the allele-supplier which is used for creating new,
193         *        random alleles
194         * @param validator the validator used for validating the created gene. This
195         *        predicate is used in the {@link #isValid()} method.
196         * @return a new {@code AnyGene} with the given parameters
197         * @throws NullPointerException if one of the parameters is {@code null}
198         */
199        public static <A> AnyGene<A> of(
200                final Supplier<? extends A> supplier,
201                final Predicate<? super A> validator
202        ) {
203                return new AnyGene<>(supplier.get(), supplier, validator);
204        }
205
206        // Create gene sequence.
207        static <A> ISeq<AnyGene<A>> seq(
208                final IntRange lengthRange,
209                final Supplier<? extends A> supplier,
210                final Predicate<? super A> validator
211        ) {
212                final var random = random();
213                final var length = random.nextInt(lengthRange.min(), lengthRange.max());
214
215                return MSeq.<AnyGene<A>>ofLength(length)
216                        .fill(() -> of(supplier.get(), supplier, validator))
217                        .toISeq();
218        }
219
220}