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.util;
021
022/**
023 * This interface defines a <em>recursive</em> generic type {@code S}, which
024 * represents the type of the implementing class.
025 * <pre>{@code
026 * interface Foo<T extends Foo<T>> extends Self<T> {
027 *     // ...
028 * }
029 * }</pre>
030 * Using the {@code Self} interface in this case makes it clear that the generic
031 * type {@code T} of the interface {@code Foo} represents the concrete type of
032 * the class, implementing the interface {@code Foo}.
033 * <p>
034 * If the interface is used as intended, the following generic {@code min} method
035 * can be implemented as a <em>default</em> method.
036 * <pre>{@code
037 * interface Foo<A extends Foo<A>> extends Self<A>, Comparable<A> {
038 *     // ...
039 *
040 *     default A max(final A other) {
041 *         return compareTo(other) > 0 ? self() : other;
042 *     }
043 * }
044 * }</pre>
045 *
046 * @param <S> the type of the implementing class.
047 *
048 * @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
049 * @since 7.0
050 * @version 7.0
051 */
052public interface Self<S extends Self<S>> {
053
054        /**
055         * Return a reference of {@code this} object as the declared generic type
056         * {@code S}.
057         *
058         * @return the {@code this} reference as the generic type {@code S}
059         * @throws ClassCastException if the interface is not used as intended and
060         *         {@code (this instanceof S) == false}
061         */
062        @SuppressWarnings("unchecked")
063        default S self() {
064                return (S)this;
065        }
066
067}