All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.tangosol.util.function.Remote Maven / Gradle / Ivy

There is a newer version: 24.09
Show newest version
/*
 * Copyright (c) 2000, 2020, Oracle and/or its affiliates.
 *
 * Licensed under the Universal Permissive License v 1.0 as shown at
 * http://oss.oracle.com/licenses/upl.
 */
package com.tangosol.util.function;


import com.tangosol.util.comparator.InverseComparator;
import com.tangosol.util.comparator.SafeComparator;

import java.io.Serializable;

import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.SortedMap;
import java.util.SortedSet;
import java.util.TreeMap;
import java.util.TreeSet;

import java.util.concurrent.Executors;
import java.util.function.BiFunction;
import java.util.function.BinaryOperator;


/**
 * Helper interfaces and methods that enable capture of standard JDK
 * functional interfaces as serializable lambdas.
 *
 * @author as  2014.07.16
 * @since 12.2.1
 */
public class Remote
    {
    // ---- Consumers -------------------------------------------------------

    /**
     * Represents an operation that accepts a single input argument and returns
     * no result. Unlike most other functional interfaces, {@code Consumer} is
     * expected to operate via side-effects.
     * 

*

This is a functional interface * whose functional method is {@link #accept(Object)}. * * @param the type of the input to the operation */ @FunctionalInterface public static interface Consumer extends java.util.function.Consumer, Serializable { /** * Returns a composed {@code Consumer} that performs, in sequence, this * operation followed by the {@code after} operation. If performing * either operation throws an exception, it is relayed to the caller of * the composed operation. If performing this operation throws an * exception, the {@code after} operation will not be performed. * * @param after the operation to perform after this operation * * @return a composed {@code Consumer} that performs in sequence this * operation followed by the {@code after} operation * * @throws NullPointerException if {@code after} is null */ default Consumer andThen(Consumer after) { Objects.requireNonNull(after); return (T t) -> { accept(t); after.accept(t); }; } } /** * Capture serializable Consumer. * * @param consumer lambda to capture * @param the type of the input to the operation * * @return serializable Consumer */ public static Consumer consumer(Consumer consumer) { return consumer; } /** * Represents an operation that accepts two input arguments and returns no * result. This is the two-arity specialization of {@link Consumer}. Unlike * most other functional interfaces, {@code BiConsumer} is expected to * operate via side-effects. *

*

This is a functional interface * whose functional method is {@link #accept(Object, Object)}. * * @param the type of the first argument to the operation * @param the type of the second argument to the operation * * @see Consumer */ @FunctionalInterface public static interface BiConsumer extends java.util.function.BiConsumer, Serializable { /** * Returns a composed {@code BiConsumer} that performs, in sequence, * this operation followed by the {@code after} operation. If performing * either operation throws an exception, it is relayed to the caller of * the composed operation. If performing this operation throws an * exception, the {@code after} operation will not be performed. * * @param after the operation to perform after this operation * * @return a composed {@code BiConsumer} that performs in sequence this * operation followed by the {@code after} operation * * @throws NullPointerException if {@code after} is null */ default BiConsumer andThen(BiConsumer after) { Objects.requireNonNull(after); return (l, r) -> { accept(l, r); after.accept(l, r); }; } } /** * Capture serializable BiConsumer. * * @param biConsumer lambda to capture * @param the type of the input to the operation * @param the type of the second argument to the operation * * @return serializable BiConsumer */ public static BiConsumer biConsumer(BiConsumer biConsumer) { return biConsumer; } /** * Represents an operation that accepts a single {@code double}-valued * argument and returns no result. This is the primitive type * specialization of {@link Consumer} for {@code double}. Unlike most other * functional interfaces, {@code DoubleConsumer} is expected to operate via * side-effects. *

*

This is a functional interface * whose functional method is {@link #accept(double)}. * * @see Consumer */ @FunctionalInterface public static interface DoubleConsumer extends java.util.function.DoubleConsumer, Serializable { /** * Returns a composed {@code DoubleConsumer} that performs, in sequence, * this operation followed by the {@code after} operation. If performing * either operation throws an exception, it is relayed to the caller of * the composed operation. If performing this operation throws an * exception, the {@code after} operation will not be performed. * * @param after the operation to perform after this operation * * @return a composed {@code DoubleConsumer} that performs in sequence * this operation followed by the {@code after} operation * * @throws NullPointerException if {@code after} is null */ default DoubleConsumer andThen(DoubleConsumer after) { Objects.requireNonNull(after); return (double t) -> { accept(t); after.accept(t); }; } } /** * Capture serializable DoubleConsumer. * * @param consumer lambda to capture * * @return serializable DoubleConsumer */ public static DoubleConsumer doubleConsumer(DoubleConsumer consumer) { return consumer; } /** * Represents an operation that accepts a single {@code int}-valued argument * and returns no result. This is the primitive type specialization of * {@link Consumer} for {@code int}. Unlike most other functional * interfaces, {@code IntConsumer} is expected to operate via side-effects. *

*

This is a functional interface * whose functional method is {@link #accept(int)}. * * @see Consumer */ @FunctionalInterface public static interface IntConsumer extends java.util.function.IntConsumer, Serializable { /** * Returns a composed {@code IntConsumer} that performs, in sequence, * this operation followed by the {@code after} operation. If performing * either operation throws an exception, it is relayed to the caller of * the composed operation. If performing this operation throws an * exception, the {@code after} operation will not be performed. * * @param after the operation to perform after this operation * * @return a composed {@code IntConsumer} that performs in sequence this * operation followed by the {@code after} operation * * @throws NullPointerException if {@code after} is null */ default IntConsumer andThen(IntConsumer after) { Objects.requireNonNull(after); return (int t) -> { accept(t); after.accept(t); }; } } /** * Capture serializable IntConsumer. * * @param consumer lambda to capture * * @return serializable IntConsumer */ public static IntConsumer intConsumer(IntConsumer consumer) { return consumer; } /** * Represents an operation that accepts a single {@code long}-valued * argument and returns no result. This is the primitive type * specialization of {@link Consumer} for {@code long}. Unlike most other * functional interfaces, {@code LongConsumer} is expected to operate via * side-effects. *

*

This is a functional interface * whose functional method is {@link #accept(long)}. * * @see Consumer */ @FunctionalInterface public static interface LongConsumer extends java.util.function.LongConsumer, Serializable { /** * Returns a composed {@code LongConsumer} that performs, in sequence, * this operation followed by the {@code after} operation. If performing * either operation throws an exception, it is relayed to the caller of * the composed operation. If performing this operation throws an * exception, the {@code after} operation will not be performed. * * @param after the operation to perform after this operation * * @return a composed {@code LongConsumer} that performs in sequence * this operation followed by the {@code after} operation * * @throws NullPointerException if {@code after} is null */ default LongConsumer andThen(LongConsumer after) { Objects.requireNonNull(after); return (long t) -> { accept(t); after.accept(t); }; } } /** * Capture serializable LongConsumer. * * @param consumer lambda to capture * * @return serializable LongConsumer */ public static LongConsumer longConsumer(LongConsumer consumer) { return consumer; } /** * Represents an operation that accepts an object-valued and a {@code * double}-valued argument, and returns no result. This is the {@code * (reference, double)} specialization of {@link BiConsumer}. Unlike most * other functional interfaces, {@code ObjDoubleConsumer} is expected to * operate via side-effects. *

*

This is a functional interface * whose functional method is {@link #accept(Object, double)}. * * @param the type of the object argument to the operation * * @see BiConsumer */ @FunctionalInterface public static interface ObjDoubleConsumer extends java.util.function.ObjDoubleConsumer, Serializable { } /** * Capture serializable ObjDoubleConsumer. * * @param consumer lambda to capture * @param the type of the object argument to the operation * * @return serializable ObjDoubleConsumer */ public static ObjDoubleConsumer objDoubleConsumer(ObjDoubleConsumer consumer) { return consumer; } /** * Represents an operation that accepts an object-valued and a {@code * int}-valued argument, and returns no result. This is the {@code * (reference, int)} specialization of {@link java.util.function.BiConsumer}. * Unlike most other functional interfaces, {@code ObjIntConsumer} is * expected to operate via side-effects. *

*

This is a functional interface * whose functional method is {@link #accept(Object, int)}. * * @param the type of the object argument to the operation * * @see BiConsumer */ @FunctionalInterface public static interface ObjIntConsumer extends java.util.function.ObjIntConsumer, Serializable { } /** * Capture serializable ObjIntConsumer. * * @param consumer lambda to capture * @param the type of the object argument to the operation * * @return serializable ObjIntConsumer */ public static ObjIntConsumer objIntConsumer(ObjIntConsumer consumer) { return consumer; } /** * Represents an operation that accepts an object-valued and a {@code * long}-valued argument, and returns no result. This is the {@code * (reference, long)} specialization of {@link BiConsumer}. Unlike most * other functional interfaces, {@code ObjLongConsumer} is expected to * operate via side-effects. *

*

This is a functional interface * whose functional method is {@link #accept(Object, long)}. * * @param the type of the object argument to the operation * * @see BiConsumer */ @FunctionalInterface public static interface ObjLongConsumer extends java.util.function.ObjLongConsumer, Serializable { } /** * Capture serializable ObjLongConsumer. * * @param consumer lambda to capture * @param the type of the object argument to the operation * * @return serializable ObjLongConsumer */ public static ObjLongConsumer objLongConsumer(ObjLongConsumer consumer) { return consumer; } // ---- Functions ------------------------------------------------------- /** * Represents a function that accepts one argument and produces a result. *

*

This is a functional interface * whose functional method is {@link #apply(Object)}. * * @param the type of the input to the function * @param the type of the result of the function */ @FunctionalInterface public static interface Function extends java.util.function.Function, Serializable { /** * Returns a composed function that first applies the {@code before} * function to its input, and then applies this function to the result. * If evaluation of either function throws an exception, it is relayed * to the caller of the composed function. * * @param the type of input to the {@code before} function, and * to the composed function * @param before the function to apply before this function is applied * * @return a composed function that first applies the {@code before} * function and then applies this function * * @throws NullPointerException if before is null * @see #andThen(Function) */ default Function compose(Function before) { Objects.requireNonNull(before); return (V v) -> apply(before.apply(v)); } /** * Returns a composed function that first applies this function to its * input, and then applies the {@code after} function to the result. If * evaluation of either function throws an exception, it is relayed to * the caller of the composed function. * * @param the type of output of the {@code after} function, and of * the composed function * @param after the function to apply after this function is applied * * @return a composed function that first applies this function and then * applies the {@code after} function * * @throws NullPointerException if after is null * @see #compose(Function) */ default Function andThen(Function after) { Objects.requireNonNull(after); return (T t) -> after.apply(apply(t)); } /** * Returns a function that always returns its input argument. * * @param the type of the input and output objects to the function * * @return a function that always returns its input argument */ static Function identity() { return t -> t; } } /** * Capture serializable Function. * * @param function lambda to capture * @param the type of the input to the function * @param the type of the result of the function * * @return serializable Function */ public static Function function(Function function) { return function; } /** * Represents a function that accepts two arguments and produces a result. * This is the two-arity specialization of {@link Function}. *

*

This is a functional interface * whose functional method is {@link #apply(Object, Object)}. * * @param the type of the first argument to the function * @param the type of the second argument to the function * @param the type of the result of the function * * @see Function */ @FunctionalInterface public static interface BiFunction extends java.util.function.BiFunction, Serializable { /** * Returns a composed function that first applies this function to its * input, and then applies the {@code after} function to the result. If * evaluation of either function throws an exception, it is relayed to * the caller of the composed function. * * @param the type of output of the {@code after} function, and of * the composed function * @param after the function to apply after this function is applied * * @return a composed function that first applies this function and then * applies the {@code after} function * * @throws NullPointerException if after is null */ default BiFunction andThen(Function after) { Objects.requireNonNull(after); return (T t, U u) -> after.apply(apply(t, u)); } } /** * Capture serializable BiFunction. * * @param biFunction lambda to capture * @param the type of the first argument to the function * @param the type of the second argument to the function * @param the type of the result of the function * * @return serializable BiFunction */ public static BiFunction biFunction(BiFunction biFunction) { return biFunction; } /** * Represents a function that accepts a double-valued argument and produces * a result. This is the {@code double}-consuming primitive specialization * for {@link Function}. *

*

This is a functional interface * whose functional method is {@link #apply(double)}. * * @param the type of the result of the function * * @see Function */ @FunctionalInterface public static interface DoubleFunction extends java.util.function.DoubleFunction, Serializable { } /** * Capture serializable DoubleFunction. * * @param function lambda to capture * @param the type of the result of the function * * @return serializable DoubleFunction */ public static DoubleFunction doubleFunction(DoubleFunction function) { return function; } /** * Represents a function that accepts a double-valued argument and produces * an int-valued result. This is the {@code double}-to-{@code int} * primitive specialization for {@link Function}. *

*

This is a functional interface * whose functional method is {@link #applyAsInt(double)}. * * @see Function */ @FunctionalInterface public static interface DoubleToIntFunction extends java.util.function.DoubleToIntFunction, Serializable { } /** * Capture serializable DoubleToIntFunction. * * @param function lambda to capture * * @return serializable DoubleToIntFunction */ public static DoubleToIntFunction doubleToIntFunction(DoubleToIntFunction function) { return function; } /** * Represents a function that accepts a double-valued argument and produces * a long-valued result. This is the {@code double}-to-{@code long} * primitive specialization for {@link Function}. *

*

This is a functional interface * whose functional method is {@link #applyAsLong(double)}. * * @see Function */ @FunctionalInterface public static interface DoubleToLongFunction extends java.util.function.DoubleToLongFunction, Serializable { } /** * Capture serializable DoubleToLongFunction. * * @param function lambda to capture * * @return serializable DoubleToLongFunction */ public static DoubleToLongFunction doubleToLongFunction(DoubleToLongFunction function) { return function; } /** * Represents a function that accepts an int-valued argument and produces a * result. This is the {@code int}-consuming primitive specialization for * {@link Function}. *

*

This is a functional interface * whose functional method is {@link #apply(int)}. * * @param the type of the result of the function * * @see Function */ @FunctionalInterface public static interface IntFunction extends java.util.function.IntFunction, Serializable { } /** * Capture serializable IntFunction. * * @param function lambda to capture * @param the type of the result of the function * * @return serializable IntFunction */ public static IntFunction intFunction(IntFunction function) { return function; } /** * Represents a function that accepts an int-valued argument and produces a * double-valued result. This is the {@code int}-to-{@code double} * primitive specialization for {@link Function}. *

*

This is a functional interface * whose functional method is {@link #applyAsDouble(int)}. * * @see Function */ @FunctionalInterface public static interface IntToDoubleFunction extends java.util.function.IntToDoubleFunction, Serializable { } /** * Capture serializable IntToDoubleFunction. * * @param function lambda to capture * * @return serializable IntToDoubleFunction */ public static IntToDoubleFunction intToDoubleFunction(IntToDoubleFunction function) { return function; } /** * Represents a function that accepts an int-valued argument and produces a * long-valued result. This is the {@code int}-to-{@code long} primitive * specialization for {@link Function}. *

*

This is a functional interface * whose functional method is {@link #applyAsLong(int)}. * * @see Function */ @FunctionalInterface public static interface IntToLongFunction extends java.util.function.IntToLongFunction, Serializable { } /** * Capture serializable IntToLongFunction. * * @param function lambda to capture * * @return serializable IntToLongFunction */ public static IntToLongFunction intToLongFunction(IntToLongFunction function) { return function; } /** * Represents a function that accepts a long-valued argument and produces a * result. This is the {@code long}-consuming primitive specialization for * {@link Function}. *

*

This is a functional interface * whose functional method is {@link #apply(long)}. * * @param the type of the result of the function * * @see Function */ @FunctionalInterface public static interface LongFunction extends java.util.function.LongFunction, Serializable { } /** * Capture serializable LongFunction. * * @param function lambda to capture * @param the type of the result of the function * * @return serializable LongFunction */ public static LongFunction longFunction(LongFunction function) { return function; } /** * Represents a function that accepts a long-valued argument and produces a * double-valued result. This is the {@code long}-to-{@code double} * primitive specialization for {@link Function}. *

*

This is a functional interface * whose functional method is {@link #applyAsDouble(long)}. * * @see Function */ @FunctionalInterface public static interface LongToDoubleFunction extends java.util.function.LongToDoubleFunction, Serializable { } /** * Capture serializable LongToDoubleFunction. * * @param function lambda to capture * * @return serializable LongToDoubleFunction */ public static LongToDoubleFunction longToDoubleFunction(LongToDoubleFunction function) { return function; } /** * Represents a function that accepts a long-valued argument and produces an * int-valued result. This is the {@code long}-to-{@code int} primitive * specialization for {@link Function}. *

*

This is a functional interface * whose functional method is {@link #applyAsInt(long)}. * * @see Function */ @FunctionalInterface public static interface LongToIntFunction extends java.util.function.LongToIntFunction, Serializable { } /** * Capture serializable LongToIntFunction. * * @param function lambda to capture * * @return serializable LongToIntFunction */ public static LongToIntFunction longToIntFunction(LongToIntFunction function) { return function; } /** * Represents a function that produces a double-valued result. This is the * {@code double}-producing primitive specialization for {@link Function}. *

*

This is a functional interface * whose functional method is {@link #applyAsDouble(Object)}. * * @param the type of the input to the function * * @see Function */ @FunctionalInterface public static interface ToDoubleFunction extends java.util.function.ToDoubleFunction, Serializable { } /** * Capture serializable ToDoubleFunction. * * @param function lambda to capture * @param the type of the input to the function * * @return serializable ToDoubleFunction */ public static ToDoubleFunction toDoubleFunction(ToDoubleFunction function) { return function; } /** * Represents a function that produces an int-valued result. This is the * {@code int}-producing primitive specialization for {@link Function}. *

*

This is a functional interface * whose functional method is {@link #applyAsInt(Object)}. * * @param the type of the input to the function * * @see Function */ @FunctionalInterface public static interface ToIntFunction extends java.util.function.ToIntFunction, Serializable { } /** * Capture serializable ToIntFunction. * * @param function lambda to capture * @param the type of the input to the function * * @return serializable ToIntFunction */ public static ToIntFunction toIntFunction(ToIntFunction function) { return function; } /** * Represents a function that produces a long-valued result. This is the * {@code long}-producing primitive specialization for {@link Function}. *

*

This is a functional interface * whose functional method is {@link #applyAsLong(Object)}. * * @param the type of the input to the function * * @see Function */ @FunctionalInterface public static interface ToLongFunction extends java.util.function.ToLongFunction, Serializable { } /** * Capture serializable ToLongFunction. * * @param function lambda to capture * @param the type of the input to the function * * @return serializable ToLongFunction */ public static ToLongFunction toLongFunction(ToLongFunction function) { return function; } /** * Represents a function that accepts two arguments and produces a * double-valued result. This is the {@code double}-producing primitive * specialization for {@link BiFunction}. *

*

This is a functional interface * whose functional method is {@link #applyAsDouble(Object, Object)}. * * @param the type of the first argument to the function * @param the type of the second argument to the function * * @see BiFunction */ @FunctionalInterface public static interface ToDoubleBiFunction extends java.util.function.ToDoubleBiFunction, Serializable { } /** * Capture serializable ToDoubleBiFunction. * * @param biFunction lambda to capture * @param the type of the first argument to the function * @param the type of the second argument to the function * * @return serializable ToDoubleBiFunction */ public static ToDoubleBiFunction toDoubleBiFunction(ToDoubleBiFunction biFunction) { return biFunction; } /** * Represents a function that accepts two arguments and produces an * int-valued result. This is the {@code int}-producing primitive * specialization for {@link BiFunction}. *

*

This is a functional interface * whose functional method is {@link #applyAsInt(Object, Object)}. * * @param the type of the first argument to the function * @param the type of the second argument to the function * * @see BiFunction */ @FunctionalInterface public static interface ToIntBiFunction extends java.util.function.ToIntBiFunction, Serializable { } /** * Capture serializable ToIntBiFunction. * * @param biFunction lambda to capture * @param the type of the first argument to the function * @param the type of the second argument to the function * * @return serializable ToIntBiFunction */ public static ToIntBiFunction toIntBiFunction(ToIntBiFunction biFunction) { return biFunction; } /** * Represents a function that accepts two arguments and produces a * long-valued result. This is the {@code long}-producing primitive * specialization for {@link BiFunction}. *

*

This is a functional interface * whose functional method is {@link #applyAsLong(Object, Object)}. * * @param the type of the first argument to the function * @param the type of the second argument to the function * * @see BiFunction */ @FunctionalInterface public static interface ToLongBiFunction extends java.util.function.ToLongBiFunction, Serializable { } /** * Capture serializable ToLongBiFunction. * * @param biFunction lambda to capture * @param the type of the first argument to the function * @param the type of the second argument to the function * * @return serializable ToLongBiFunction */ public static ToLongBiFunction toLongBiFunction(ToLongBiFunction biFunction) { return biFunction; } // ---- Predicates ------------------------------------------------------ /** * Represents a predicate (boolean-valued function) of one argument. *

*

This is a functional interface * whose functional method is {@link #test(Object)}. * * @param the type of the input to the predicate */ @FunctionalInterface public static interface Predicate extends java.util.function.Predicate, Serializable { /** * Returns a composed predicate that represents a short-circuiting * logical AND of this predicate and another. When evaluating the * composed predicate, if this predicate is {@code false}, then the * {@code other} predicate is not evaluated. *

* Any exceptions thrown during evaluation of either predicate are * relayed to the caller; if evaluation of this predicate throws an * exception, the {@code other} predicate will not be evaluated. * * @param other a predicate that will be logically-ANDed with this * predicate * * @return a composed predicate that represents the short-circuiting * logical AND of this predicate and the {@code other} predicate * * @throws NullPointerException if other is null */ default Predicate and(Predicate other) { Objects.requireNonNull(other); return (t) -> test(t) && other.test(t); } /** * Returns a predicate that represents the logical negation of this * predicate. * * @return a predicate that represents the logical negation of this * predicate */ default Predicate negate() { return (t) -> !test(t); } /** * Returns a composed predicate that represents a short-circuiting * logical OR of this predicate and another. When evaluating the * composed predicate, if this predicate is {@code true}, then the * {@code other} predicate is not evaluated. *

*

Any exceptions thrown during evaluation of either predicate are * relayed to the caller; if evaluation of this predicate throws an * exception, the {@code other} predicate will not be evaluated. * * @param other a predicate that will be logically-ORed with this * predicate * * @return a composed predicate that represents the short-circuiting * logical OR of this predicate and the {@code other} predicate * * @throws NullPointerException if other is null */ default Predicate or(Predicate other) { Objects.requireNonNull(other); return (t) -> test(t) || other.test(t); } /** * Returns a predicate that tests if two arguments are equal according * to {@link Objects#equals(Object, Object)}. * * @param the type of arguments to the predicate * @param targetRef the object reference with which to compare for * equality, which may be {@code null} * * @return a predicate that tests if two arguments are equal according * to {@link Objects#equals(Object, Object)} */ static Predicate isEqual(Object targetRef) { return (null == targetRef) ? Objects::isNull : targetRef::equals; } } /** * Capture serializable Predicate. * * @param predicate lambda to capture * @param the type of the input to the predicate * * @return serializable Predicate */ public static Predicate predicate(Predicate predicate) { return predicate; } /** * Represents a predicate (boolean-valued function) of two arguments. This * is the two-arity specialization of {@link Predicate}. *

*

This is a functional interface * whose functional method is {@link #test(Object, Object)}. * * @param the type of the first argument to the predicate * @param the type of the second argument the predicate * * @see Predicate */ @FunctionalInterface public static interface BiPredicate extends java.util.function.BiPredicate, Serializable { /** * Returns a composed predicate that represents a short-circuiting * logical AND of this predicate and another. When evaluating the * composed predicate, if this predicate is {@code false}, then the * {@code other} predicate is not evaluated. *

* Any exceptions thrown during evaluation of either predicate are * relayed to the caller; if evaluation of this predicate throws an * exception, the {@code other} predicate will not be evaluated. * * @param other a predicate that will be logically-ANDed with this * predicate * * @return a composed predicate that represents the short-circuiting * logical AND of this predicate and the {@code other} predicate * * @throws NullPointerException if other is null */ default BiPredicate and(BiPredicate other) { Objects.requireNonNull(other); return (T t, U u) -> test(t, u) && other.test(t, u); } /** * Returns a predicate that represents the logical negation of this * predicate. * * @return a predicate that represents the logical negation of this * predicate */ default BiPredicate negate() { return (T t, U u) -> !test(t, u); } /** * Returns a composed predicate that represents a short-circuiting * logical OR of this predicate and another. When evaluating the * composed predicate, if this predicate is {@code true}, then the * {@code other} predicate is not evaluated. *

* Any exceptions thrown during evaluation of either predicate are * relayed to the caller; if evaluation of this predicate throws an * exception, the {@code other} predicate will not be evaluated. * * @param other a predicate that will be logically-ORed with this * predicate * * @return a composed predicate that represents the short-circuiting * logical OR of this predicate and the {@code other} predicate * * @throws NullPointerException if other is null */ default BiPredicate or(BiPredicate other) { Objects.requireNonNull(other); return (T t, U u) -> test(t, u) || other.test(t, u); } } /** * Capture serializable BiPredicate. * * @param biPredicate lambda to capture * @param the type of the first argument to the predicate * @param the type of the second argument the predicate * * @return serializable BiPredicate */ public static BiPredicate biPredicate(BiPredicate biPredicate) { return biPredicate; } /** * Represents a predicate (boolean-valued function) of one {@code * double}-valued argument. This is the {@code double}-consuming primitive * type specialization of {@link Predicate}. *

*

This is a functional interface * whose functional method is {@link #test(double)}. * * @see Predicate */ @FunctionalInterface public static interface DoublePredicate extends java.util.function.DoublePredicate, Serializable { /** * Returns a composed predicate that represents a short-circuiting * logical AND of this predicate and another. When evaluating the * composed predicate, if this predicate is {@code false}, then the * {@code other} predicate is not evaluated. *

* Any exceptions thrown during evaluation of either predicate are * relayed to the caller; if evaluation of this predicate throws an * exception, the {@code other} predicate will not be evaluated. * * @param other a predicate that will be logically-ANDed with this * predicate * * @return a composed predicate that represents the short-circuiting * logical AND of this predicate and the {@code other} predicate * * @throws NullPointerException if other is null */ default DoublePredicate and(DoublePredicate other) { Objects.requireNonNull(other); return (value) -> test(value) && other.test(value); } /** * Returns a predicate that represents the logical negation of this * predicate. * * @return a predicate that represents the logical negation of this * predicate */ default DoublePredicate negate() { return (value) -> !test(value); } /** * Returns a composed predicate that represents a short-circuiting * logical OR of this predicate and another. When evaluating the * composed predicate, if this predicate is {@code true}, then the * {@code other} predicate is not evaluated. *

* Any exceptions thrown during evaluation of either predicate are * relayed to the caller; if evaluation of this predicate throws an * exception, the {@code other} predicate will not be evaluated. * * @param other a predicate that will be logically-ORed with this * predicate * * @return a composed predicate that represents the short-circuiting * logical OR of this predicate and the {@code other} predicate * * @throws NullPointerException if other is null */ default DoublePredicate or(DoublePredicate other) { Objects.requireNonNull(other); return (value) -> test(value) || other.test(value); } } /** * Capture serializable DoublePredicate. * * @param predicate lambda to capture * * @return serializable DoublePredicate */ public static DoublePredicate doublePredicate(DoublePredicate predicate) { return predicate; } /** * Represents a predicate (boolean-valued function) of one {@code * int}-valued argument. This is the {@code int}-consuming primitive type * specialization of {@link Predicate}. *

*

This is a functional interface * whose functional method is {@link #test(int)}. * * @see Predicate */ @FunctionalInterface public static interface IntPredicate extends java.util.function.IntPredicate, Serializable { /** * Returns a composed predicate that represents a short-circuiting * logical AND of this predicate and another. When evaluating the * composed predicate, if this predicate is {@code false}, then the * {@code other} predicate is not evaluated. *

* Any exceptions thrown during evaluation of either predicate are * relayed to the caller; if evaluation of this predicate throws an * exception, the {@code other} predicate will not be evaluated. * * @param other a predicate that will be logically-ANDed with this * predicate * * @return a composed predicate that represents the short-circuiting * logical AND of this predicate and the {@code other} predicate * * @throws NullPointerException if other is null */ default IntPredicate and(IntPredicate other) { Objects.requireNonNull(other); return (value) -> test(value) && other.test(value); } /** * Returns a predicate that represents the logical negation of this * predicate. * * @return a predicate that represents the logical negation of this * predicate */ default IntPredicate negate() { return (value) -> !test(value); } /** * Returns a composed predicate that represents a short-circuiting * logical OR of this predicate and another. When evaluating the * composed predicate, if this predicate is {@code true}, then the * {@code other} predicate is not evaluated. *

* Any exceptions thrown during evaluation of either predicate are * relayed to the caller; if evaluation of this predicate throws an * exception, the {@code other} predicate will not be evaluated. * * @param other a predicate that will be logically-ORed with this * predicate * * @return a composed predicate that represents the short-circuiting * logical OR of this predicate and the {@code other} predicate * * @throws NullPointerException if other is null */ default IntPredicate or(IntPredicate other) { Objects.requireNonNull(other); return (value) -> test(value) || other.test(value); } } /** * Capture serializable IntPredicate. * * @param predicate lambda to capture * * @return serializable IntPredicate */ public static IntPredicate intPredicate(IntPredicate predicate) { return predicate; } /** * Represents a predicate (boolean-valued function) of one {@code * long}-valued argument. This is the {@code long}-consuming primitive type * specialization of {@link Predicate}. *

*

This is a functional interface * whose functional method is {@link #test(long)}. * * @see Predicate */ @FunctionalInterface public static interface LongPredicate extends java.util.function.LongPredicate, Serializable { /** * Returns a composed predicate that represents a short-circuiting * logical AND of this predicate and another. When evaluating the * composed predicate, if this predicate is {@code false}, then the * {@code other} predicate is not evaluated. *

* Any exceptions thrown during evaluation of either predicate are * relayed to the caller; if evaluation of this predicate throws an * exception, the {@code other} predicate will not be evaluated. * * @param other a predicate that will be logically-ANDed with this * predicate * * @return a composed predicate that represents the short-circuiting * logical AND of this predicate and the {@code other} predicate * * @throws NullPointerException if other is null */ default LongPredicate and(LongPredicate other) { Objects.requireNonNull(other); return (value) -> test(value) && other.test(value); } /** * Returns a predicate that represents the logical negation of this * predicate. * * @return a predicate that represents the logical negation of this * predicate */ default LongPredicate negate() { return (value) -> !test(value); } /** * Returns a composed predicate that represents a short-circuiting * logical OR of this predicate and another. When evaluating the * composed predicate, if this predicate is {@code true}, then the * {@code other} predicate is not evaluated. *

* Any exceptions thrown during evaluation of either predicate are * relayed to the caller; if evaluation of this predicate throws an * exception, the {@code other} predicate will not be evaluated. * * @param other a predicate that will be logically-ORed with this * predicate * * @return a composed predicate that represents the short-circuiting * logical OR of this predicate and the {@code other} predicate * * @throws NullPointerException if other is null */ default LongPredicate or(LongPredicate other) { Objects.requireNonNull(other); return (value) -> test(value) || other.test(value); } } /** * Capture serializable LongPredicate. * * @param predicate lambda to capture * * @return serializable LongPredicate */ public static LongPredicate longPredicate(LongPredicate predicate) { return predicate; } // ---- Suppliers ------------------------------------------------------- /** * Represents a supplier of results. *

* There is no requirement that a new or distinct result be returned each * time the supplier is invoked. *

* This is a functional interface * whose functional method is {@link #get()}. * * @param the type of results supplied by this supplier */ @FunctionalInterface public static interface Supplier extends java.util.function.Supplier, Serializable { } /** * Capture serializable Supplier. * * @param supplier lambda to capture * @param the type of results supplied by this supplier * * @return serializable Supplier */ public static Supplier supplier(Supplier supplier) { return supplier; } /** * Represents a supplier of {@code boolean}-valued results. This is the * {@code boolean}-producing primitive specialization of {@link Supplier}. *

*

There is no requirement that a new or distinct result be returned each * time the supplier is invoked. *

*

This is a functional interface * whose functional method is {@link #getAsBoolean()}. * * @see Supplier */ @FunctionalInterface public static interface BooleanSupplier extends java.util.function.BooleanSupplier, Serializable { } /** * Capture serializable BooleanSupplier. * * @param supplier lambda to capture * * @return serializable BooleanSupplier */ public static BooleanSupplier booleanSupplier(BooleanSupplier supplier) { return supplier; } /** * Represents a supplier of {@code double}-valued results. This is the * {@code double}-producing primitive specialization of {@link Supplier}. *

*

There is no requirement that a distinct result be returned each time * the supplier is invoked. *

*

This is a functional interface * whose functional method is {@link #getAsDouble()}. * * @see Supplier */ @FunctionalInterface public static interface DoubleSupplier extends java.util.function.DoubleSupplier, Serializable { } /** * Capture serializable DoubleSupplier. * * @param supplier lambda to capture * * @return serializable DoubleSupplier */ public static DoubleSupplier doubleSupplier(DoubleSupplier supplier) { return supplier; } /** * Represents a supplier of {@code int}-valued results. This is the {@code * int}-producing primitive specialization of {@link Supplier}. *

*

There is no requirement that a distinct result be returned each time * the supplier is invoked. *

*

This is a functional interface * whose functional method is {@link #getAsInt()}. * * @see Supplier */ @FunctionalInterface public static interface IntSupplier extends java.util.function.IntSupplier, Serializable { } /** * Capture serializable IntSupplier. * * @param supplier lambda to capture * * @return serializable IntSupplier */ public static IntSupplier intSupplier(IntSupplier supplier) { return supplier; } /** * Represents a supplier of {@code long}-valued results. This is the {@code * long}-producing primitive specialization of {@link Supplier}. *

*

There is no requirement that a distinct result be returned each time * the supplier is invoked. *

*

This is a functional interface * whose functional method is {@link #getAsLong()}. * * @see Supplier */ @FunctionalInterface public static interface LongSupplier extends java.util.function.LongSupplier, Serializable { } /** * Capture serializable LongSupplier. * * @param supplier lambda to capture * * @return serializable LongSupplier */ public static LongSupplier longSupplier(LongSupplier supplier) { return supplier; } // ---- Binary Operators ------------------------------------------------ /** * Represents an operation upon two operands of the same type, producing a * result of the same type as the operands. This is a specialization of * {@link BiFunction} for the case where the operands and the result are all * of the same type. *

*

This is a functional interface * whose functional method is {@link #apply(Object, Object)}. * * @param the type of the operands and result of the operator * * @see BiFunction * @see UnaryOperator */ @FunctionalInterface public static interface BinaryOperator extends BiFunction, java.util.function.BinaryOperator, Serializable { /** * Returns a {@link BinaryOperator} which returns the lesser of two * elements according to the specified {@code Comparator}. * * @param the type of the input arguments of the comparator * @param comparator a {@code Comparator} for comparing the two values * * @return a {@code BinaryOperator} which returns the lesser of its * operands, according to the supplied {@code Comparator} * * @throws NullPointerException if the argument is null */ public static BinaryOperator minBy(Comparator comparator) { Objects.requireNonNull(comparator); return (a, b) -> comparator.compare(a, b) <= 0 ? a : b; } /** * Returns a {@link BinaryOperator} which returns the lesser of two * elements according to the specified {@code Comparator}. * * @param the type of the input arguments of the comparator * @param comparator a {@code Comparator} for comparing the two values * * @return a {@code BinaryOperator} which returns the lesser of its * operands, according to the supplied {@code Comparator} * * @throws NullPointerException if the argument is null */ public static BinaryOperator minBy(java.util.Comparator comparator) { Objects.requireNonNull(comparator); return (a, b) -> comparator.compare(a, b) <= 0 ? a : b; } /** * Returns a {@link BinaryOperator} which returns the greater of two * elements according to the specified {@code Comparator}. * * @param the type of the input arguments of the comparator * @param comparator a {@code Comparator} for comparing the two values * * @return a {@code BinaryOperator} which returns the greater of its * operands, according to the supplied {@code Comparator} * * @throws NullPointerException if the argument is null */ public static BinaryOperator maxBy(Comparator comparator) { Objects.requireNonNull(comparator); return (a, b) -> comparator.compare(a, b) >= 0 ? a : b; } /** * Returns a {@link BinaryOperator} which returns the greater of two * elements according to the specified {@code Comparator}. * * @param the type of the input arguments of the comparator * @param comparator a {@code Comparator} for comparing the two values * * @return a {@code BinaryOperator} which returns the greater of its * operands, according to the supplied {@code Comparator} * * @throws NullPointerException if the argument is null */ public static BinaryOperator maxBy(java.util.Comparator comparator) { Objects.requireNonNull(comparator); return (a, b) -> comparator.compare(a, b) >= 0 ? a : b; } /** * {@code BinaryOperator} that merges the contents of its right * argument into its left argument, using the provided merge function to * handle duplicate keys. * * @param type of the map keys * @param type of the map values * @param type of the map * @param mergeFunction A merge function suitable for {@link * Map#merge(Object, Object, java.util.function.BiFunction) Map.merge()} * * @return a merge function for two maps */ public static > BinaryOperator mapMerger(BinaryOperator mergeFunction) { return mapMerger((java.util.function.BinaryOperator) mergeFunction); } /** * {@code BinaryOperator} that merges the contents of its right * argument into its left argument, using the provided merge function to * handle duplicate keys. * * @param type of the map keys * @param type of the map values * @param type of the map * @param mergeFunction A merge function suitable for {@link * Map#merge(Object, Object, java.util.function.BiFunction) Map.merge()} * * @return a merge function for two maps */ public static > BinaryOperator mapMerger(java.util.function.BinaryOperator mergeFunction) { return (m1, m2) -> { for (Map.Entry e : m2.entrySet()) { m1.merge(e.getKey(), e.getValue(), mergeFunction); } return m1; }; } } /** * Capture serializable BinaryOperator. * * @param operator lambda to capture * @param the type of the operands and result of the operator * * @return serializable BinaryOperator */ public static BinaryOperator binaryOperator(BinaryOperator operator) { return operator; } /** * Represents an operation upon two {@code double}-valued operands and * producing a {@code double}-valued result. This is the primitive type * specialization of {@link BinaryOperator} for {@code double}. *

*

This is a functional interface * whose functional method is {@link #applyAsDouble(double, double)}. * * @see BinaryOperator * @see DoubleUnaryOperator */ @FunctionalInterface public static interface DoubleBinaryOperator extends java.util.function.DoubleBinaryOperator, Serializable { } /** * Capture serializable DoubleBinaryOperator. * * @param operator lambda to capture * * @return serializable DoubleBinaryOperator */ public static DoubleBinaryOperator doubleBinaryOperator(DoubleBinaryOperator operator) { return operator; } /** * Represents an operation upon two {@code int}-valued operands and * producing an {@code int}-valued result. This is the primitive type * specialization of {@link BinaryOperator} for {@code int}. *

*

This is a functional interface * whose functional method is {@link #applyAsInt(int, int)}. * * @see BinaryOperator * @see IntUnaryOperator */ @FunctionalInterface public static interface IntBinaryOperator extends java.util.function.IntBinaryOperator, Serializable { } /** * Capture serializable IntBinaryOperator. * * @param operator lambda to capture * * @return serializable IntBinaryOperator */ public static IntBinaryOperator intBinaryOperator(IntBinaryOperator operator) { return operator; } /** * Represents an operation upon two {@code long}-valued operands and * producing a {@code long}-valued result. This is the primitive type * specialization of {@link BinaryOperator} for {@code long}. *

*

This is a functional interface * whose functional method is {@link #applyAsLong(long, long)}. * * @see BinaryOperator * @see LongUnaryOperator */ @FunctionalInterface public static interface LongBinaryOperator extends java.util.function.LongBinaryOperator, Serializable { } /** * Capture serializable LongBinaryOperator. * * @param operator lambda to capture * * @return serializable LongBinaryOperator */ public static LongBinaryOperator longBinaryOperator(LongBinaryOperator operator) { return operator; } // ---- Unary Operators ------------------------------------------------- /** * Represents an operation on a single operand that produces a result of the * same type as its operand. This is a specialization of {@code Function} * for the case where the operand and result are of the same type. *

*

This is a functional interface * whose functional method is {@link #apply(Object)}. * * @param the type of the operand and result of the operator * * @see Function */ @FunctionalInterface public static interface UnaryOperator extends Function, java.util.function.UnaryOperator, Serializable { /** * Returns a unary operator that always returns its input argument. * * @param the type of the input and output of the operator * * @return a unary operator that always returns its input argument */ static UnaryOperator identity() { return t -> t; } } /** * Capture serializable UnaryOperator. * * @param operator lambda to capture * @param the type of the operand and result of the operator * * @return serializable UnaryOperator */ public static UnaryOperator unaryOperator(UnaryOperator operator) { return operator; } /** * Represents an operation on a single {@code double}-valued operand that * produces a {@code double}-valued result. This is the primitive type * specialization of {@link UnaryOperator} for {@code double}. *

*

This is a functional interface * whose functional method is {@link #applyAsDouble(double)}. * * @see UnaryOperator */ @FunctionalInterface public static interface DoubleUnaryOperator extends java.util.function.DoubleUnaryOperator, Serializable { /** * Returns a composed operator that first applies the {@code before} * operator to its input, and then applies this operator to the result. * If evaluation of either operator throws an exception, it is relayed * to the caller of the composed operator. * * @param before the operator to apply before this operator is applied * * @return a composed operator that first applies the {@code before} * operator and then applies this operator * * @throws NullPointerException if before is null * @see #andThen(DoubleUnaryOperator) */ default DoubleUnaryOperator compose(DoubleUnaryOperator before) { Objects.requireNonNull(before); return (double v) -> applyAsDouble(before.applyAsDouble(v)); } /** * Returns a composed operator that first applies this operator to its * input, and then applies the {@code after} operator to the result. If * evaluation of either operator throws an exception, it is relayed to * the caller of the composed operator. * * @param after the operator to apply after this operator is applied * * @return a composed operator that first applies this operator and then * applies the {@code after} operator * * @throws NullPointerException if after is null * @see #compose(DoubleUnaryOperator) */ default DoubleUnaryOperator andThen(DoubleUnaryOperator after) { Objects.requireNonNull(after); return (double t) -> after.applyAsDouble(applyAsDouble(t)); } /** * Returns a unary operator that always returns its input argument. * * @return a unary operator that always returns its input argument */ static DoubleUnaryOperator identity() { return t -> t; } } /** * Capture serializable DoubleUnaryOperator. * * @param operator lambda to capture * * @return serializable DoubleUnaryOperator */ public static DoubleUnaryOperator doubleUnaryOperator(DoubleUnaryOperator operator) { return operator; } /** * Represents an operation on a single {@code int}-valued operand that * produces an {@code int}-valued result. This is the primitive type * specialization of {@link UnaryOperator} for {@code int}. *

*

This is a functional interface * whose functional method is {@link #applyAsInt(int)}. * * @see UnaryOperator */ @FunctionalInterface public static interface IntUnaryOperator extends java.util.function.IntUnaryOperator, Serializable { /** * Returns a composed operator that first applies the {@code before} * operator to its input, and then applies this operator to the result. * If evaluation of either operator throws an exception, it is relayed * to the caller of the composed operator. * * @param before the operator to apply before this operator is applied * * @return a composed operator that first applies the {@code before} * operator and then applies this operator * * @throws NullPointerException if before is null * @see #andThen(IntUnaryOperator) */ default IntUnaryOperator compose(IntUnaryOperator before) { Objects.requireNonNull(before); return (int v) -> applyAsInt(before.applyAsInt(v)); } /** * Returns a composed operator that first applies this operator to its * input, and then applies the {@code after} operator to the result. If * evaluation of either operator throws an exception, it is relayed to * the caller of the composed operator. * * @param after the operator to apply after this operator is applied * * @return a composed operator that first applies this operator and then * applies the {@code after} operator * * @throws NullPointerException if after is null * @see #compose(IntUnaryOperator) */ default IntUnaryOperator andThen(IntUnaryOperator after) { Objects.requireNonNull(after); return (int t) -> after.applyAsInt(applyAsInt(t)); } /** * Returns a unary operator that always returns its input argument. * * @return a unary operator that always returns its input argument */ static IntUnaryOperator identity() { return t -> t; } } /** * Capture serializable IntUnaryOperator. * * @param operator lambda to capture * * @return serializable IntUnaryOperator */ public static IntUnaryOperator intUnaryOperator(IntUnaryOperator operator) { return operator; } /** * Represents an operation on a single {@code long}-valued operand that * produces a {@code long}-valued result. This is the primitive type * specialization of {@link UnaryOperator} for {@code long}. *

*

This is a functional interface * whose functional method is {@link #applyAsLong(long)}. * * @see UnaryOperator */ @FunctionalInterface public static interface LongUnaryOperator extends java.util.function.LongUnaryOperator, Serializable { /** * Returns a composed operator that first applies the {@code before} * operator to its input, and then applies this operator to the result. * If evaluation of either operator throws an exception, it is relayed * to the caller of the composed operator. * * @param before the operator to apply before this operator is applied * * @return a composed operator that first applies the {@code before} * operator and then applies this operator * * @throws NullPointerException if before is null * @see #andThen(LongUnaryOperator) */ default LongUnaryOperator compose(LongUnaryOperator before) { Objects.requireNonNull(before); return (long v) -> applyAsLong(before.applyAsLong(v)); } /** * Returns a composed operator that first applies this operator to its * input, and then applies the {@code after} operator to the result. If * evaluation of either operator throws an exception, it is relayed to * the caller of the composed operator. * * @param after the operator to apply after this operator is applied * * @return a composed operator that first applies this operator and then * applies the {@code after} operator * * @throws NullPointerException if after is null * @see #compose(LongUnaryOperator) */ default LongUnaryOperator andThen(LongUnaryOperator after) { Objects.requireNonNull(after); return (long t) -> after.applyAsLong(applyAsLong(t)); } /** * Returns a unary operator that always returns its input argument. * * @return a unary operator that always returns its input argument */ static LongUnaryOperator identity() { return t -> t; } } /** * Capture serializable LongUnaryOperator. * * @param operator lambda to capture * * @return serializable LongUnaryOperator */ public static LongUnaryOperator longUnaryOperator(LongUnaryOperator operator) { return operator; } // ---- Comparator ------------------------------------------------------ /** * A comparison function, which imposes a total ordering on some * collection of objects. Comparators can be passed to a sort method (such * as {@code Collections.sort} or {@code Arrays.sort}) to allow precise control * over the sort order. Comparators can also be used to control the order of * certain data structures (such as {@link SortedSet sorted sets} or {@link * SortedMap sorted maps}), or to provide an ordering for collections of * objects that don't have a {@link Comparable natural ordering}.

* * The ordering imposed by a comparator c on a set of elements * S is said to be consistent with equals if and only if * c.compare(e1, e2)==0 has the same boolean value as * e1.equals(e2) for every e1 and e2 in * S.

* * Caution should be exercised when using a comparator capable of imposing an * ordering inconsistent with equals to order a sorted set (or sorted map). * Suppose a sorted set (or sorted map) with an explicit comparator c * is used with elements (or keys) drawn from a set S. If the * ordering imposed by c on S is inconsistent with equals, * the sorted set (or sorted map) will behave "strangely." In particular the * sorted set (or sorted map) will violate the general contract for set (or * map), which is defined in terms of equals.

* * For example, suppose one adds two elements {@code a} and {@code b} such that * {@code (a.equals(b) && c.compare(a, b) != 0)} * to an empty {@code TreeSet} with comparator {@code c}. * The second {@code add} operation will return * true (and the size of the tree set will increase) because {@code a} and * {@code b} are not equivalent from the tree set's perspective, even though * this is contrary to the specification of the * {@link Set#add Set.add} method.

* * Note: It is generally a good idea for comparators to also implement * java.io.Serializable, as they may be used as ordering methods in * serializable data structures (like {@link TreeSet}, {@link TreeMap}). In * order for the data structure to serialize successfully, the comparator (if * provided) must implement Serializable.

* * For the mathematically inclined, the relation that defines the * imposed ordering that a given comparator c imposes on a * given set of objects S is:

     *       {(x, y) such that c.compare(x, y) <= 0}.
     * 
The quotient for this total order is:
     *       {(x, y) such that c.compare(x, y) == 0}.
     * 
* * It follows immediately from the contract for compare that the * quotient is an equivalence relation on S, and that the * imposed ordering is a total order on S. When we say that * the ordering imposed by c on S is consistent with * equals, we mean that the quotient for the ordering is the equivalence * relation defined by the objects' {@link Object#equals(Object) * equals(Object)} method(s):
     *     {(x, y) such that x.equals(y)}. 
* *

Unlike {@code Comparable}, a comparator may optionally permit * comparison of null arguments, while maintaining the requirements for * an equivalence relation. * *

This interface is a member of the Java Collections Framework. * * @param the type of objects that may be compared by this comparator */ @FunctionalInterface @SuppressWarnings("JavaDoc") public static interface Comparator extends java.util.Comparator, Serializable { /** * Returns a comparator that imposes the reverse ordering of this * comparator. * * @return a comparator that imposes the reverse ordering of this * comparator. */ default Comparator reversed() { return new InverseComparator(this); } /** * Returns a lexicographic-order comparator with another comparator. If * this {@code Comparator} considers two elements equal, i.e. {@code * compare(a, b) == 0}, {@code other} is used to determine the order. * * @param other the other comparator to be used when this comparator * compares two objects that are equal. * * @return a lexicographic-order comparator composed of this and then * the other comparator * * @throws NullPointerException if the argument is null. * * @apiNote For example, to sort a collection of {@code String} based on * the length and then case-insensitive natural ordering, the comparator * can be composed using following code, *

*

{@code
         *     Comparator cmp = Comparator.comparingInt(String::length)
         *             .thenComparing(String.CASE_INSENSITIVE_ORDER);
         * }
* @since 1.8 */ default Comparator thenComparing(Comparator other) { Objects.requireNonNull(other); return (c1, c2) -> { int res = compare(c1, c2); return (res != 0) ? res : other.compare(c1, c2); }; } /** * Returns a lexicographic-order comparator with a function that * extracts a key to be compared with the given {@code Comparator}. * * @param the type of the sort key * @param keyExtractor the function used to extract the sort key * @param keyComparator the {@code Comparator} used to compare the sort * key * * @return a lexicographic-order comparator composed of this comparator * and then comparing on the key extracted by the keyExtractor function * * @throws NullPointerException if either argument is null. * * @implSpec This default implementation behaves as if {@code * thenComparing(comparing(keyExtractor, cmp))} * . * @see #comparing(Function, Comparator) * @see #thenComparing(Comparator) */ default Comparator thenComparing( Function keyExtractor, Comparator keyComparator) { return thenComparing(comparing(keyExtractor, keyComparator)); } /** * Returns a lexicographic-order comparator with a function that * extracts a {@code Comparable} sort key. * * @param the type of the {@link Comparable} sort key * @param keyExtractor the function used to extract the {@link * Comparable} sort key * * @return a lexicographic-order comparator composed of this and then * the {@link Comparable} sort key. * * @throws NullPointerException if the argument is null. * * @implSpec This default implementation behaves as if {@code * thenComparing(comparing(keyExtractor))}. * * @see #comparing(Function) * @see #thenComparing(Comparator) */ default > Comparator thenComparing( Function keyExtractor) { return thenComparing(comparing(keyExtractor)); } /** * Returns a lexicographic-order comparator with a function that * extracts a {@code int} sort key. * * @param keyExtractor the function used to extract the integer sort * key * * @return a lexicographic-order comparator composed of this and then * the {@code int} sort key * * @throws NullPointerException if the argument is null. * * @implSpec This default implementation behaves as if {@code * thenComparing(comparingInt(keyExtractor))}. * * @see #comparingInt(ToIntFunction) * @see #thenComparing(Comparator) */ default Comparator thenComparingInt(ToIntFunction keyExtractor) { return thenComparing(comparingInt(keyExtractor)); } /** * Returns a lexicographic-order comparator with a function that * extracts a {@code long} sort key. * * @param keyExtractor the function used to extract the long sort key * * @return a lexicographic-order comparator composed of this and then * the {@code long} sort key * * @throws NullPointerException if the argument is null. * * @implSpec This default implementation behaves as if {@code * thenComparing(comparingLong(keyExtractor))}. * * @see #comparingLong(ToLongFunction) * @see #thenComparing(Comparator) */ default Comparator thenComparingLong(ToLongFunction keyExtractor) { return thenComparing(comparingLong(keyExtractor)); } /** * Returns a lexicographic-order comparator with a function that * extracts a {@code double} sort key. * * @param keyExtractor the function used to extract the double sort key * * @return a lexicographic-order comparator composed of this and then * the {@code double} sort key * * @throws NullPointerException if the argument is null. * * @implSpec This default implementation behaves as if {@code * thenComparing(comparingDouble(keyExtractor))}. * * @see #comparingDouble(ToDoubleFunction) * @see #thenComparing(Comparator) */ default Comparator thenComparingDouble(ToDoubleFunction keyExtractor) { return thenComparing(comparingDouble(keyExtractor)); } /** * Returns a comparator that imposes the reverse of the natural * ordering. * * @param the {@link Comparable} type of element to be compared * * @return a comparator that imposes the reverse of the natural * ordering on {@code Comparable} objects. * * @see Comparable */ public static > Comparator reverseOrder() { return new InverseComparator<>(); } /** * Returns a comparator that compares {@link Comparable} objects in * natural order. * * @param the {@link Comparable} type of element to be compared * * @return a comparator that imposes the natural ordering on * {@code Comparable} objects. * * @see Comparable * @since 1.8 */ @SuppressWarnings("unchecked") public static > Comparator naturalOrder() { return (Comparator) SafeComparator.INSTANCE; } /** * Returns a null-friendly comparator that considers {@code null} to be * less than non-null. When both are {@code null}, they are considered * equal. If both are non-null, the specified {@code Comparator} is used * to determine the order. If the specified comparator is {@code null}, * then the returned comparator considers all non-null values to be * equal. * * @param the type of the elements to be compared * @param comparator a {@code Comparator} for comparing non-null values * * @return a comparator that considers {@code null} to be less than * non-null, and compares non-null objects with the supplied {@code * Comparator}. */ public static Comparator nullsFirst(Comparator comparator) { return new SafeComparator<>(comparator, true); } /** * Returns a null-friendly comparator that considers {@code null} to be * greater than non-null. When both are {@code null}, they are * considered equal. If both are non-null, the specified {@code * Comparator} is used to determine the order. If the specified * comparator is {@code null}, then the returned comparator considers * all non-null values to be equal. * * @param the type of the elements to be compared * @param comparator a {@code Comparator} for comparing non-null values * * @return a comparator that considers {@code null} to be greater than * non-null, and compares non-null objects with the supplied {@code * Comparator}. */ public static Comparator nullsLast(Comparator comparator) { return new SafeComparator<>(comparator, false); } /** * Accepts a function that extracts a sort key from a type {@code T}, * and returns a {@code Comparator} that compares by that sort key * using the specified {@link Comparator}. * * @param the type of element to be compared * @param the type of the sort key * @param keyExtractor the function used to extract the sort key * @param keyComparator the {@code Comparator} used to compare the sort * key * * @return a comparator that compares by an extracted key using the * specified {@code Comparator} * * @throws NullPointerException if either argument is null * * @apiNote For example, to obtain a {@code Comparator} that compares * {@code Person} objects by their last name ignoring case differences, *

*

{@code
         *     Comparator cmp = Comparator.comparing(
         *             Person::getLastName,
         *             String.CASE_INSENSITIVE_ORDER);
         * }
*/ public static Comparator comparing( Function keyExtractor, Comparator keyComparator) { Objects.requireNonNull(keyExtractor); Objects.requireNonNull(keyComparator); return (c1, c2) -> keyComparator.compare(keyExtractor.apply(c1), keyExtractor.apply(c2)); } /** * Accepts a function that extracts a {@link java.lang.Comparable * Comparable} sort key from a type {@code T}, and returns a {@code * Comparator} that compares by that sort key. * * @param the type of element to be compared * @param the type of the {@code Comparable} sort key * @param keyExtractor the function used to extract the {@link * Comparable} sort key * * @return a comparator that compares by an extracted key * * @throws NullPointerException if the argument is null * * @apiNote For example, to obtain a {@code Comparator} that compares * {@code Person} objects by their last name, *

*

{@code
         *     Comparator byLastName = Comparator.comparing(Person::getLastName);
         * }
*/ public static > Comparator comparing( Function keyExtractor) { Objects.requireNonNull(keyExtractor); return (c1, c2) -> keyExtractor.apply(c1).compareTo(keyExtractor.apply(c2)); } /** * Accepts a function that extracts an {@code int} sort key from a type * {@code T}, and returns a {@code Comparator} that compares by that * sort key. * * @param the type of element to be compared * @param keyExtractor the function used to extract the integer sort * key * * @return a comparator that compares by an extracted key * * @throws NullPointerException if the argument is null * * @see #comparing(Function) */ public static Comparator comparingInt(ToIntFunction keyExtractor) { Objects.requireNonNull(keyExtractor); return (c1, c2) -> Integer.compare(keyExtractor.applyAsInt(c1), keyExtractor.applyAsInt(c2)); } /** * Accepts a function that extracts a {@code long} sort key from a type * {@code T}, and returns a {@code Comparator} that compares by that * sort key. * * @param the type of element to be compared * @param keyExtractor the function used to extract the long sort key * * @return a comparator that compares by an extracted key * * @throws NullPointerException if the argument is null * * @see #comparing(Function) */ public static Comparator comparingLong(ToLongFunction keyExtractor) { Objects.requireNonNull(keyExtractor); return (c1, c2) -> Long.compare(keyExtractor.applyAsLong(c1), keyExtractor.applyAsLong(c2)); } /** * Accepts a function that extracts a {@code double} sort key from a * type {@code T}, and returns a {@code Comparator} that compares by * that sort key. * * @param the type of element to be compared * @param keyExtractor the function used to extract the double sort key * * @return a comparator that compares by an extracted key * * @throws NullPointerException if the argument is null * * @see #comparing(Function) */ public static Comparator comparingDouble(ToDoubleFunction keyExtractor) { Objects.requireNonNull(keyExtractor); return (c1, c2) -> Double.compare(keyExtractor.applyAsDouble(c1), keyExtractor.applyAsDouble(c2)); } } /** * Capture serializable Comparator. * * @param comparator lambda to capture * * @return serializable Comparator */ public static Comparator comparator(Comparator comparator) { return comparator; } // ---- Runnable and Callable ------------------------------------------- /** * The Runnable interface should be implemented by any * class whose instances are intended to be executed by a thread. The * class must define a method of no arguments called run. *

* This interface is designed to provide a common protocol for objects that * wish to execute code while they are active. For example, * Runnable is implemented by class Thread. * Being active simply means that a thread has been started and has not * yet been stopped. *

* In addition, Runnable provides the means for a class to be * active while not subclassing Thread. A class that implements * Runnable can run without subclassing Thread * by instantiating a Thread instance and passing itself in * as the target. In most cases, the Runnable interface should * be used if you are only planning to override the run() * method and no other Thread methods. * This is important because classes should not be subclassed * unless the programmer intends on modifying or enhancing the fundamental * behavior of the class. */ @FunctionalInterface public static interface Runnable extends java.lang.Runnable, Serializable { } /** * Capture serializable Runnable. * * @param runnable lambda to capture * * @return serializable Runnable */ public static Runnable runnable(Runnable runnable) { return runnable; } /** * A task that returns a result and may throw an exception. * Implementors define a single method with no arguments called * {@code call}. * *

The {@code Callable} interface is similar to {@link * java.lang.Runnable}, in that both are designed for classes whose * instances are potentially executed by another thread. A * {@code Runnable}, however, does not return a result and cannot * throw a checked exception. * *

The {@link Executors} class contains utility methods to * convert from other common forms to {@code Callable} classes. * * @param the result type of method {@code call} */ @FunctionalInterface public static interface Callable extends java.util.concurrent.Callable, Serializable { } /** * Capture serializable Callable. * * @param callable lambda to capture * * @return serializable Callable */ public static Callable callable(Callable callable) { return callable; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy