001/*
002 * Java Genetic Algorithm Library (jenetics-8.0.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.moea;
021
022import java.util.Comparator;
023import java.util.function.Function;
024
025/**
026 * Defines the order of two elements of a given <em>vector</em> type {@code V}.
027 * The following example creates an {@code ElementComparator} function for a
028 * {@code double[] array}:
029 * {@snippet lang="java":
030 * final ElementComparator<double[]> comp =
031 *     (u, v, i) -> Double.compare(u[i], v[i]);
032 * }
033 *
034 * @param <V> the vector type
035 *
036 * @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
037 * @version 4.1
038 * @since 4.1
039 */
040@FunctionalInterface
041public interface ElementComparator<V> {
042
043        /**
044         * Compares the components of {@code a} and {@code b} at the given
045         * {@code index}. E.g.
046         * {@snippet lang="java":
047         * final ElementComparator<double[]> comp =
048         *     (u, v, i) -> Double.compare(u[i], v[i]);
049         * }
050         *
051         * @param u the first vector
052         * @param v the second vector
053         * @param index the vector index
054         * @return a negative integer, zero, or a positive integer as the
055         *         first argument is less than, equal to, or greater than the
056         *         second.
057         * @throws NullPointerException if either {@code a} or {@code b} is
058         *        {@code null}
059         * @throws IndexOutOfBoundsException if the index is out of range
060         *         {@code (index < 0 || index >= length(a)  || index >= length(b))}
061         */
062        int compare(final V u, final V v, final int index);
063
064        /**
065         * Returns a comparator that imposes the reverse ordering of this
066         * comparator.
067         *
068         * @return a comparator that imposes the reverse ordering of this
069         *         comparator.
070         */
071        default ElementComparator<V> reversed() {
072                return (u, v, i) -> compare(v, u, i);
073        }
074
075        /**
076         * Return an element comparator for the mapped type {@code T}.
077         *
078         * @param mapper the mapper function
079         * @param <T> the new comparator type
080         * @return an element comparator for the mapped type {@code T}
081         */
082        default <T> ElementComparator<T>
083        map(final Function<? super T, ? extends V> mapper) {
084                return (u, v, i) -> compare(mapper.apply(u), mapper.apply(v), i);
085        }
086
087        /**
088         * Return a comparator which takes the component at the give {@code index}
089         * for comparison two objects of type {@code T}.
090         *
091         * @param index the component index
092         * @return the component comparator for the given {@code index}
093         */
094        default Comparator<V> ofIndex(final int index) {
095                return (a, b) -> compare(a, b, index);
096        }
097
098}