1 /*-
2 * #%L
3 * io.earcam.unexceptional
4 * %%
5 * Copyright (C) 2016 - 2017 earcam
6 * %%
7 * SPDX-License-Identifier: (BSD-3-Clause OR EPL-1.0 OR Apache-2.0 OR MIT)
8 *
9 * You <b>must</b> choose to accept, in full - any individual or combination of
10 * the following licenses:
11 * <ul>
12 * <li><a href="https://opensource.org/licenses/BSD-3-Clause">BSD-3-Clause</a></li>
13 * <li><a href="https://www.eclipse.org/legal/epl-v10.html">EPL-1.0</a></li>
14 * <li><a href="https://www.apache.org/licenses/LICENSE-2.0">Apache-2.0</a></li>
15 * <li><a href="https://opensource.org/licenses/MIT">MIT</a></li>
16 * </ul>
17 * #L%
18 */
19 package io.earcam.unexceptional;
20
21 import static java.util.Objects.requireNonNull;
22
23 import java.io.Serializable;//NOSONAR stfu false positive (suspect Sonar Squid is looking at bytecode here? as erasure of all but left-most bound where multiple bounds occur, see casts of return types in maxBy/minBy)
24 import java.util.Comparator;
25 import java.util.Objects;
26
27 /**
28 * A checked parallel of {@link java.util.function.BinaryOperator}
29 *
30 * @param <T> arguments and return type
31 * @param <E> the type of Throwable declared
32 *
33 * @since 0.2.0
34 *
35 * @see java.util.function.BinaryOperator
36 */
37 @FunctionalInterface
38 public interface CheckedBinaryOperator<T, E extends Throwable> extends CheckedBiFunction<T, T, T, E> {
39
40 /**
41 * See {@link java.util.function.BinaryOperator#minBy(Comparator)}
42 *
43 * @param <T> arguments and return type
44 *
45 * @param comparator the comparator used to determine the minimum.
46 * @return a {@link CheckedBinaryOperator}
47 * @throws NullPointerException if {@code comparator} is {@code null}
48 */
49 @SuppressWarnings("squid:S1905") // SonarQube false positive
50 public static <T, E extends Throwable> CheckedBinaryOperator<T, E> minBy(/* @Nonnull */ CheckedComparator<? super T, E> comparator)
51 {
52 requireNonNull(comparator);
53 return (CheckedBinaryOperator<T, E> & Serializable) (a, b) -> comparator.compare(a, b) < 0 ? a : b;
54 }
55
56
57 /**
58 * See {@link java.util.function.BinaryOperator#maxBy(Comparator)}
59 *
60 * @param <T> arguments and return type
61 *
62 * @param comparator the comparator used to determine the maximum.
63 * @return a {@link CheckedBinaryOperator}
64 * @throws NullPointerException if {@code comparator} is {@code null}
65 */
66 @SuppressWarnings("squid:S1905") // SonarQube false positive
67 public static <T, E extends Throwable> CheckedBinaryOperator<T, E> maxBy(/* @Nonnull */ CheckedComparator<? super T, ? extends E> comparator)
68 {
69 Objects.requireNonNull(comparator);
70 return (CheckedBinaryOperator<T, E> & Serializable) (a, b) -> comparator.compare(a, b) > 0 ? a : b;
71 }
72 }