View Javadoc
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  
18  package org.apache.commons.lang3.function;
19  
20  import java.util.Objects;
21  import java.util.function.DoubleUnaryOperator;
22  
23  /**
24   * A functional interface like {@link DoubleUnaryOperator} that declares a {@link Throwable}.
25   *
26   * @param <E> The kind of thrown exception or error.
27   * @since 3.11
28   */
29  public interface FailableDoubleUnaryOperator<E extends Throwable> {
30  
31      /** NOP singleton */
32      @SuppressWarnings("rawtypes")
33      FailableDoubleUnaryOperator NOP = t -> 0d;
34  
35      /**
36       * Returns a unary operator that always returns its input argument.
37       *
38       * @param <E> The kind of thrown exception or error.
39       * @return a unary operator that always returns its input argument
40       */
41      static <E extends Throwable> FailableDoubleUnaryOperator<E> identity() {
42          return t -> t;
43      }
44  
45      /**
46       * Returns The NOP singleton.
47       *
48       * @param <E> The kind of thrown exception or error.
49       * @return The NOP singleton.
50       */
51      @SuppressWarnings("unchecked")
52      static <E extends Throwable> FailableDoubleUnaryOperator<E> nop() {
53          return NOP;
54      }
55  
56      /**
57       * Returns a composed {@link FailableDoubleUnaryOperator} like
58       * {@link DoubleUnaryOperator#andThen(DoubleUnaryOperator)}.
59       *
60       * @param after the operator to apply after this one.
61       * @return a composed {@link FailableDoubleUnaryOperator} like
62       *         {@link DoubleUnaryOperator#andThen(DoubleUnaryOperator)}.
63       * @throws NullPointerException if after is null.
64       * @see #compose(FailableDoubleUnaryOperator)
65       */
66      default FailableDoubleUnaryOperator<E> andThen(final FailableDoubleUnaryOperator<E> after) {
67          Objects.requireNonNull(after);
68          return (final double t) -> after.applyAsDouble(applyAsDouble(t));
69      }
70  
71      /**
72       * Applies this operator to the given operand.
73       *
74       * @param operand the operand
75       * @return the operator result
76       * @throws E Thrown when a consumer fails.
77       */
78      double applyAsDouble(double operand) throws E;
79  
80      /**
81       * Returns a composed {@link FailableDoubleUnaryOperator} like
82       * {@link DoubleUnaryOperator#compose(DoubleUnaryOperator)}.
83       *
84       * @param before the operator to apply before this one.
85       * @return a composed {@link FailableDoubleUnaryOperator} like
86       *         {@link DoubleUnaryOperator#compose(DoubleUnaryOperator)}.
87       * @throws NullPointerException if before is null.
88       * @see #andThen(FailableDoubleUnaryOperator)
89       */
90      default FailableDoubleUnaryOperator<E> compose(final FailableDoubleUnaryOperator<E> before) {
91          Objects.requireNonNull(before);
92          return (final double v) -> applyAsDouble(before.applyAsDouble(v));
93      }
94  }