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 numeric genes.
024 *
025 * @implSpec
026 * <em>Jenetics</em> requires that the individuals ({@link Genotype} and
027 * {@link Phenotype}) are not changed after they have been created. Therefore,
028 * all implementations of the {@code NumericGene} interface must also be
029 * <em>immutable</em>.
030 *
031 * @see NumericChromosome
032 *
033 * @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
034 * @since 1.6
035 * @version 3.0
036 */
037public interface NumericGene<
038        N extends Number & Comparable<? super N>,
039        G extends NumericGene<N, G>
040>
041        extends BoundedGene<N, G>
042{
043
044        /**
045         * Returns the value of the specified gene as a byte. This may involve
046         * rounding or truncation.
047         *
048         * @return the numeric value represented by this object after conversion to
049         *         type {@code byte}.
050         */
051        default byte byteValue() {
052                return allele().byteValue();
053        }
054
055        /**
056         * Returns the value of the specified gene as a short. This may involve
057         * rounding or truncation.
058         *
059         * @return the numeric value represented by this object after conversion to
060         *         type {@code short}.
061         */
062        default short shortValue() {
063                return allele().shortValue();
064        }
065
066        /**
067         * Returns the value of the specified gene as an int. This may involve
068         * rounding or truncation.
069         *
070         * @return the numeric value represented by this object after conversion to
071         *         type {@code int}.
072         */
073        default int intValue() {
074                return allele().intValue();
075        }
076
077        /**
078         * Returns the value of the specified gene as a long. This may involve
079         * rounding or truncation.
080         *
081         * @return the numeric value represented by this object after conversion to
082         *         type {@code long}.
083         */
084        default long longValue() {
085                return allele().longValue();
086        }
087
088        /**
089         * Returns the value of the specified gene as a float. This may involve
090         * rounding or truncation.
091         *
092         * @return the numeric value represented by this object after conversion to
093         *         type {@code float}.
094         */
095        default float floatValue() {
096                return allele().floatValue();
097        }
098
099        /**
100         * Returns the value of the specified gene as a double. This may involve
101         * rounding or truncation.
102         *
103         * @return the numeric value represented by this object after conversion to
104         *         type {@code double}.
105         */
106        default double doubleValue() {
107                return allele().doubleValue();
108        }
109
110        @Override
111        G newInstance(final Number number);
112
113}