001/*
002 * Java Genetic Algorithm Library (jenetics-8.1.0).
003 * Copyright (c) 2007-2024 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
022/**
023 * Base interface for genes where the alleles are bound by a minimum and a
024 * maximum value.
025 *
026 * @implSpec
027 * <em>Jenetics</em> requires that the individuals ({@link Genotype} and
028 * {@link Phenotype}) are not changed after they have been created. Therefore,
029 * all implementations of the {@code BoundedGene} interface must also be
030 * <em>immutable</em>.
031 *
032 * @see BoundedChromosome
033 *
034 * @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
035 * @since 1.6
036 * @version 7.0
037 */
038public interface BoundedGene<
039        A extends Comparable<? super A>,
040        G extends BoundedGene<A, G>
041>
042        extends Gene<A, G>, Comparable<G>
043{
044
045        /**
046         * Return the allowed min value.
047         *
048         * @return The allowed min value.
049         */
050        A min();
051
052        /**
053         * Return the allowed max value.
054         *
055         * @return The allowed max value.
056         */
057        A max();
058
059        @Override
060        default boolean isValid() {
061                return
062                        allele().compareTo(min()) >= 0 &&
063                        allele().compareTo(max()) < 0;
064        }
065
066        @Override
067        default int compareTo(final G other) {
068                return allele().compareTo(other.allele());
069        }
070
071        /**
072         * Return the minimum value of {@code this} and the given {@code other}
073         * value.
074         *
075         * @since 7.0
076         *
077         * @param other the other value
078         * @return the minimum value of {@code this} and the given {@code other}
079         * @throws NullPointerException if {@code other} is {@code null}
080         */
081        default G min(final G other) {
082                return compareTo(other) < 0 ? self() : other;
083        }
084
085        /**
086         * Return the maximum value of {@code this} and the given {@code other}
087         * value.
088         *
089         * @since 7.0
090         *
091         * @param other the other value
092         * @return the maximum value of {@code this} and the given {@code other}
093         * @throws NullPointerException if {@code other} is {@code null}
094         */
095        default G max(final G other) {
096                return compareTo(other) > 0 ? self() : other;
097        }
098
099        /**
100         * Create a new gene from the given {@code value} and the current bounds.
101         *
102         * @param value the value of the new gene.
103         * @return a new gene with the given value.
104         */
105        @Override
106        G newInstance(final A value);
107
108}