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