FailableDoubleUnaryOperator.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */

  17. package org.apache.commons.lang3.function;

  18. import java.util.Objects;
  19. import java.util.function.DoubleUnaryOperator;

  20. /**
  21.  * A functional interface like {@link DoubleUnaryOperator} that declares a {@link Throwable}.
  22.  *
  23.  * @param <E> The kind of thrown exception or error.
  24.  * @since 3.11
  25.  */
  26. public interface FailableDoubleUnaryOperator<E extends Throwable> {

  27.     /** NOP singleton */
  28.     @SuppressWarnings("rawtypes")
  29.     FailableDoubleUnaryOperator NOP = t -> 0d;

  30.     /**
  31.      * Returns a unary operator that always returns its input argument.
  32.      *
  33.      * @param <E> The kind of thrown exception or error.
  34.      * @return a unary operator that always returns its input argument
  35.      */
  36.     static <E extends Throwable> FailableDoubleUnaryOperator<E> identity() {
  37.         return t -> t;
  38.     }

  39.     /**
  40.      * Returns The NOP singleton.
  41.      *
  42.      * @param <E> The kind of thrown exception or error.
  43.      * @return The NOP singleton.
  44.      */
  45.     @SuppressWarnings("unchecked")
  46.     static <E extends Throwable> FailableDoubleUnaryOperator<E> nop() {
  47.         return NOP;
  48.     }

  49.     /**
  50.      * Returns a composed {@link FailableDoubleUnaryOperator} like
  51.      * {@link DoubleUnaryOperator#andThen(DoubleUnaryOperator)}.
  52.      *
  53.      * @param after the operator to apply after this one.
  54.      * @return a composed {@link FailableDoubleUnaryOperator} like
  55.      *         {@link DoubleUnaryOperator#andThen(DoubleUnaryOperator)}.
  56.      * @throws NullPointerException if after is null.
  57.      * @see #compose(FailableDoubleUnaryOperator)
  58.      */
  59.     default FailableDoubleUnaryOperator<E> andThen(final FailableDoubleUnaryOperator<E> after) {
  60.         Objects.requireNonNull(after);
  61.         return (final double t) -> after.applyAsDouble(applyAsDouble(t));
  62.     }

  63.     /**
  64.      * Applies this operator to the given operand.
  65.      *
  66.      * @param operand the operand
  67.      * @return the operator result
  68.      * @throws E Thrown when a consumer fails.
  69.      */
  70.     double applyAsDouble(double operand) throws E;

  71.     /**
  72.      * Returns a composed {@link FailableDoubleUnaryOperator} like
  73.      * {@link DoubleUnaryOperator#compose(DoubleUnaryOperator)}.
  74.      *
  75.      * @param before the operator to apply before this one.
  76.      * @return a composed {@link FailableDoubleUnaryOperator} like
  77.      *         {@link DoubleUnaryOperator#compose(DoubleUnaryOperator)}.
  78.      * @throws NullPointerException if before is null.
  79.      * @see #andThen(FailableDoubleUnaryOperator)
  80.      */
  81.     default FailableDoubleUnaryOperator<E> compose(final FailableDoubleUnaryOperator<E> before) {
  82.         Objects.requireNonNull(before);
  83.         return (final double v) -> applyAsDouble(before.applyAsDouble(v));
  84.     }
  85. }