    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *      http://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 * Unless required by applicable law or agreed to in writing, software
    012 * distributed under the License is distributed on an "AS IS" BASIS,
    013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 * See the License for the specific language governing permissions and
    015 * limitations under the License.
    016 */
    017
    018package org.apache.commons.lang3.function;
    019
    020import java.util.Objects;
    021import java.util.function.IntUnaryOperator;
    022
    023/**
    024 * A functional interface like {@link IntUnaryOperator} that declares a {@code Throwable}.
    025 *
    026 * @param <E> Thrown exception.
    027 * @since 3.11
    028 */
    029public interface FailableIntUnaryOperator<E extends Throwable> {
    030
    031    /** NOP singleton */
    032    @SuppressWarnings("rawtypes")
    033    FailableIntUnaryOperator NOP = t -> 0;
    034
    035    /**
    036     * Returns a unary operator that always returns its input argument.
    037     *
    038     * @param <E> Thrown exception.
    039     * @return a unary operator that always returns its input argument
    040     */
    041    static <E extends Throwable> FailableIntUnaryOperator<E> identity() {
    042        return t -> t;
    043    }
    044
    045    /**
    046     * Returns The NOP singleton.
    047     *
    048     * @param <E> Thrown exception.
    049     * @return The NOP singleton.
    050     */
    051    static <E extends Throwable> FailableIntUnaryOperator<E> nop() {
    052        return NOP;
    053    }
    054
    055    /**
    056     * Returns a composed {@code FailableDoubleUnaryOperator} like {@link IntUnaryOperator#andThen(IntUnaryOperator)}.
    057     *
    058     * @param after the operator to apply after this one.
    059     * @return a composed {@code FailableIntUnaryOperator} like {@link IntUnaryOperator#andThen(IntUnaryOperator)}.
    060     * @throws NullPointerException if after is null.
    061     * @see #compose(FailableIntUnaryOperator)
    062     */
    063    default FailableIntUnaryOperator<E> andThen(final FailableIntUnaryOperator<E> after) {
    064        Objects.requireNonNull(after);
    065        return (final int t) -> after.applyAsInt(applyAsInt(t));
    066    }
    067
    068    /**
    069     * Applies this operator to the given operand.
    070     *
    071     * @param operand the operand
    072     * @return the operator result
    073     * @throws E Thrown when a consumer fails.
    074     */
    075    int applyAsInt(int operand) throws E;
    076
    077    /**
    078     * Returns a composed {@code FailableIntUnaryOperator} like {@link IntUnaryOperator#compose(IntUnaryOperator)}.
    079     *
    080     * @param before the operator to apply before this one.
    081     * @return a composed {@code FailableIntUnaryOperator} like {@link IntUnaryOperator#compose(IntUnaryOperator)}.
    082     * @throws NullPointerException if before is null.
    083     * @see #andThen(FailableIntUnaryOperator)
    084     */
    085    default FailableIntUnaryOperator<E> compose(final FailableIntUnaryOperator<E> before) {
    086        Objects.requireNonNull(before);
    087        return (final int v) -> applyAsInt(before.applyAsInt(v));
    088    }
    089}