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.DoubleUnaryOperator; 022 023/** 024 * A functional interface like {@link DoubleUnaryOperator} that declares a {@link Throwable}. 025 * 026 * @param <E> The kind of thrown exception or error. 027 * @since 3.11 028 */ 029public interface FailableDoubleUnaryOperator<E extends Throwable> { 030 031 /** NOP singleton */ 032 @SuppressWarnings("rawtypes") 033 FailableDoubleUnaryOperator NOP = t -> 0d; 034 035 /** 036 * Returns a unary operator that always returns its input argument. 037 * 038 * @param <E> The kind of thrown exception or error. 039 * @return a unary operator that always returns its input argument 040 */ 041 static <E extends Throwable> FailableDoubleUnaryOperator<E> identity() { 042 return t -> t; 043 } 044 045 /** 046 * Returns The NOP singleton. 047 * 048 * @param <E> The kind of thrown exception or error. 049 * @return The NOP singleton. 050 */ 051 @SuppressWarnings("unchecked") 052 static <E extends Throwable> FailableDoubleUnaryOperator<E> nop() { 053 return NOP; 054 } 055 056 /** 057 * Returns a composed {@link FailableDoubleUnaryOperator} like 058 * {@link DoubleUnaryOperator#andThen(DoubleUnaryOperator)}. 059 * 060 * @param after the operator to apply after this one. 061 * @return a composed {@link FailableDoubleUnaryOperator} like 062 * {@link DoubleUnaryOperator#andThen(DoubleUnaryOperator)}. 063 * @throws NullPointerException if after is null. 064 * @see #compose(FailableDoubleUnaryOperator) 065 */ 066 default FailableDoubleUnaryOperator<E> andThen(final FailableDoubleUnaryOperator<E> after) { 067 Objects.requireNonNull(after); 068 return (final double t) -> after.applyAsDouble(applyAsDouble(t)); 069 } 070 071 /** 072 * Applies this operator to the given operand. 073 * 074 * @param operand the operand 075 * @return the operator result 076 * @throws E Thrown when a consumer fails. 077 */ 078 double applyAsDouble(double operand) throws E; 079 080 /** 081 * Returns a composed {@link FailableDoubleUnaryOperator} like 082 * {@link DoubleUnaryOperator#compose(DoubleUnaryOperator)}. 083 * 084 * @param before the operator to apply before this one. 085 * @return a composed {@link FailableDoubleUnaryOperator} like 086 * {@link DoubleUnaryOperator#compose(DoubleUnaryOperator)}. 087 * @throws NullPointerException if before is null. 088 * @see #andThen(FailableDoubleUnaryOperator) 089 */ 090 default FailableDoubleUnaryOperator<E> compose(final FailableDoubleUnaryOperator<E> before) { 091 Objects.requireNonNull(before); 092 return (final double v) -> applyAsDouble(before.applyAsDouble(v)); 093 } 094}