001/**
002 * Copyright 2015 DuraSpace, Inc.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.fcrepo.http.commons.utils;
018
019import java.util.function.BiConsumer;
020import org.fcrepo.kernel.api.exception.RepositoryRuntimeException;
021
022/**
023 * It is sometimes convenient to use an operation that throws checked exceptions inside of a functional incantation.
024 * This type permits that. It should be used with great caution! Only uncheck exceptions for which no reasonable
025 * recovery is available.
026 *
027 * @author ajs6f
028 * @param <S> the type of the first argument to the operation
029 * @param <T> the type of the second argument to the operation
030 */
031@FunctionalInterface
032public interface UncheckedBiConsumer<S, T> extends BiConsumer<S, T> {
033
034    @Override
035    default void accept(final S first, final T second) {
036        try {
037            acceptThrows(first, second);
038        } catch (final Exception e) {
039            throw new RepositoryRuntimeException(e);
040        }
041    }
042
043    /**
044     * The same semantic as {@link #accept(Object, Object)}, but allowed to throw exceptions.
045     *
046     * @param first the first input argument
047     * @param second the second input argument
048     * @throws Exception the underlying exception
049     */
050    void acceptThrows(final S first, final T second) throws Exception;
051
052    /**
053     * A convenience method to construct <code>UncheckedBiConsumers</code> from lambda syntax.
054     *
055     * @param <S> the type of the first argument
056     * @param <T> the type of the second argument
057     * @param c an arity-2 lambda function
058     * @return an UncheckedBiConsumer
059     */
060    static <S, T> UncheckedBiConsumer<S, T> uncheck(final UncheckedBiConsumer<S, T> c) {
061        return c;
062    }
063}