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