001/*
002 * Java Genetic Algorithm Library (jenetics-7.1.0).
003 * Copyright (c) 2007-2022 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 java.io.Serial;
023
024import io.jenetics.util.RandomRegistry;
025
026/**
027 * Implementation of a BitGene.
028 *
029 * @see BitChromosome
030 *
031 * @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
032 * @since 1.0
033 * @version 6.0
034 */
035public enum BitGene
036        implements
037                Gene<Boolean, BitGene>,
038                Comparable<BitGene>
039{
040
041        FALSE,
042        TRUE;
043
044        @Serial
045        private static final long serialVersionUID = 3L;
046
047        public static final BitGene ZERO = FALSE;
048        public static final BitGene ONE = TRUE;
049
050
051        /**
052         * Return the value of the BitGene.
053         *
054         * @see #allele()
055         * @see #booleanValue()
056         *
057         * @return The value of the BitGene.
058         */
059        public final boolean bit() {
060                return this == TRUE;
061        }
062
063        /**
064         * Return the {@code boolean} value of this gene.
065         *
066         * @see #allele()
067         * @see #bit()
068         *
069         * @return the {@code boolean} value of this gene.
070         */
071        public boolean booleanValue() {
072                return this == TRUE;
073        }
074
075        @Override
076        public Boolean allele() {
077                return this == TRUE;
078        }
079
080        /**
081         * Return always {@code true}.
082         *
083         * @return always {@code true}
084         */
085        @Override
086        public boolean isValid() {
087                return true;
088        }
089
090        /**
091         * Create a new, <em>random</em> gene.
092         */
093        @Override
094        public BitGene newInstance() {
095                return RandomRegistry.random().nextBoolean() ? TRUE : FALSE;
096        }
097
098        /**
099         * Create a new gene from the given {@code value}.
100         *
101         * @since 1.6
102         * @param value the value of the new gene.
103         * @return a new gene with the given value.
104         */
105        public BitGene newInstance(final Boolean value) {
106                return value ? TRUE : FALSE;
107        }
108
109        @Override
110        public String toString() {
111                return booleanValue() ? "true" : "false";
112        }
113
114        /**
115         * Return the corresponding {@code BitGene} for the given {@code boolean}
116         * value.
117         *
118         * @param value the value of the returned {@code BitGene}.
119         * @return the {@code BitGene} for the given {@code boolean} value.
120         */
121        public static BitGene of(final boolean value) {
122                return value ? TRUE : FALSE;
123        }
124
125}