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.IntUnaryOperator;
22  
23  /**
24   * A functional interface like {@link IntUnaryOperator} that declares a {@link Throwable}.
25   *
26   * @param <E> The kind of thrown exception or error.
27   * @since 3.11
28   */
29  public interface FailableIntUnaryOperator<E extends Throwable> {
30  
31      /** NOP singleton */
32      @SuppressWarnings("rawtypes")
33      FailableIntUnaryOperator NOP = t -> 0;
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> FailableIntUnaryOperator<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> FailableIntUnaryOperator<E> nop() {
53          return NOP;
54      }
55  
56      /**
57       * Returns a composed {@link FailableDoubleUnaryOperator} like {@link IntUnaryOperator#andThen(IntUnaryOperator)}.
58       *
59       * @param after the operator to apply after this one.
60       * @return a composed {@link FailableIntUnaryOperator} like {@link IntUnaryOperator#andThen(IntUnaryOperator)}.
61       * @throws NullPointerException if after is null.
62       * @see #compose(FailableIntUnaryOperator)
63       */
64      default FailableIntUnaryOperator<E> andThen(final FailableIntUnaryOperator<E> after) {
65          Objects.requireNonNull(after);
66          return (final int t) -> after.applyAsInt(applyAsInt(t));
67      }
68  
69      /**
70       * Applies this operator to the given operand.
71       *
72       * @param operand the operand
73       * @return the operator result
74       * @throws E Thrown when a consumer fails.
75       */
76      int applyAsInt(int operand) throws E;
77  
78      /**
79       * Returns a composed {@link FailableIntUnaryOperator} like {@link IntUnaryOperator#compose(IntUnaryOperator)}.
80       *
81       * @param before the operator to apply before this one.
82       * @return a composed {@link FailableIntUnaryOperator} like {@link IntUnaryOperator#compose(IntUnaryOperator)}.
83       * @throws NullPointerException if before is null.
84       * @see #andThen(FailableIntUnaryOperator)
85       */
86      default FailableIntUnaryOperator<E> compose(final FailableIntUnaryOperator<E> before) {
87          Objects.requireNonNull(before);
88          return (final int v) -> applyAsInt(before.applyAsInt(v));
89      }
90  }