001 /*
002 * Java Genetic Algorithm Library (jenetics-5.2.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;
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 5.2
037 */
038 public 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 default A min() {
051 return getMin();
052 }
053
054 /**
055 * Return the allowed min value.
056 *
057 * @return The allowed min value.
058 * @deprecated Use {@link #min()} ()} instead. Implementer must still
059 * implement this method.
060 */
061 @Deprecated
062 A getMin();
063
064 /**
065 * Return the allowed max value.
066 *
067 * @return The allowed max value.
068 */
069 default A max() {
070 return getMax();
071 }
072
073 /**
074 * Return the allowed max value.
075 *
076 * @return The allowed max value.
077 * @deprecated Use {@link #min()} ()} instead. Implementer must still
078 * implement this method.
079 */
080 @Deprecated
081 A getMax();
082
083 @Override
084 default boolean isValid() {
085 return
086 allele().compareTo(min()) >= 0 &&
087 allele().compareTo(max()) <= 0;
088 }
089
090 @Override
091 default int compareTo(final G other) {
092 return allele().compareTo(other.allele());
093 }
094
095 /**
096 * Create a new gene from the given {@code value} and the current bounds.
097 *
098 * @param value the value of the new gene.
099 * @return a new gene with the given value.
100 */
101 @Override
102 G newInstance(final A value);
103
104 }
|