FailableLongUnaryOperator.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.LongUnaryOperator;

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

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

  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> FailableLongUnaryOperator<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> FailableLongUnaryOperator<E> nop() {
  47.         return NOP;
  48.     }

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

  61.     /**
  62.      * Applies this operator to the given operand.
  63.      *
  64.      * @param operand the operand
  65.      * @return the operator result
  66.      * @throws E Thrown when a consumer fails.
  67.      */
  68.     long applyAsLong(long operand) throws E;

  69.     /**
  70.      * Returns a composed {@link FailableLongUnaryOperator} like {@link LongUnaryOperator#compose(LongUnaryOperator)}.
  71.      *
  72.      * @param before the operator to apply before this one.
  73.      * @return a composed {@link FailableLongUnaryOperator} like {@link LongUnaryOperator#compose(LongUnaryOperator)}.
  74.      * @throws NullPointerException if before is null.
  75.      * @see #andThen(FailableLongUnaryOperator)
  76.      */
  77.     default FailableLongUnaryOperator<E> compose(final FailableLongUnaryOperator<E> before) {
  78.         Objects.requireNonNull(before);
  79.         return (final long v) -> applyAsLong(before.applyAsLong(v));
  80.     }
  81. }