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.ext;
021
022import io.jenetics.Gene;
023
024import io.jenetics.ext.util.FlatTree;
025
026/**
027 * Representation of tree shaped gene. Since the genes are part of a chromosome,
028 * they are implementing the {@link FlatTree} interface, which makes the required
029 * storage layout explicit.
030 *
031 * @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
032 * @version 6.0
033 * @since 3.9
034 */
035public interface TreeGene<A, G extends TreeGene<A, G>>
036        extends
037                Gene<A, G>,
038                FlatTree<A, G>
039{
040
041        @Override
042        default A value() {
043                return allele();
044        }
045
046        /**
047         * Return a new tree gene with the given allele and the <em>local</em> tree
048         * structure.
049         *
050         * @param allele the actual gene allele
051         * @param childOffset the offset of the first node child within the
052         *        chromosome
053         * @param childCount the number of children of the new tree gene
054         * @return a new tree gene with the given parameters
055         * @throws IllegalArgumentException  if the {@code childCount} is smaller
056         *         than zero
057         */
058        G newInstance(
059                final A allele,
060                final int childOffset,
061                final int childCount
062        );
063
064        /**
065         * Return a new tree gene from the given flat tree node.
066         *
067         * @since 6.0
068         *
069         * @param tree the flat tree node
070         * @return a new tree gene from the given flat tree node
071         * @throws NullPointerException if the given {@code tree} node is
072         *         {@code null}
073         */
074        default G newInstance(final FlatTree<? extends A, ?> tree) {
075                return newInstance(
076                        tree.value(),
077                        tree.childOffset(),
078                        tree.childCount()
079                );
080        }
081
082}