001/*
002 * Licensed to DuraSpace under one or more contributor license agreements.
003 * See the NOTICE file distributed with this work for additional information
004 * regarding copyright ownership.
005 *
006 * DuraSpace licenses this file to you under the Apache License,
007 * Version 2.0 (the "License"); you may not use this file except in
008 * compliance with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.fcrepo.kernel.modeshape.utils;
019
020import java.util.function.Function;
021import javax.jcr.RepositoryException;
022
023import org.fcrepo.kernel.api.exception.RepositoryRuntimeException;
024
025/**
026 * Operations that throw {@link RepositoryException} cannot be used as lambdas without "unchecking" those exceptions.
027 *
028 * @author ajs6f
029 * @param <T> the type of the input to the function
030 */
031@FunctionalInterface
032public interface UncheckedFunction<T, R> extends Function<T, R> {
033
034    @Override
035    default R apply(final T elem) {
036        try {
037            return applyThrows(elem);
038        } catch (final RepositoryException e) {
039            throw new RepositoryRuntimeException(e);
040        }
041    }
042
043    /**
044     * The same semantic as {@link #apply(Object)}, but allowed to throw a {@link RepositoryException}
045     *
046     * @param elem the input argument
047     * @return the function result
048     * @throws RepositoryException the underlying repository error
049     */
050    R applyThrows(T elem) throws RepositoryException;
051
052    /**
053     * @param <T> the type of the input to the function
054     * @param <R> the type of the output of the function
055     * @param p a lambda expression
056     * @return an unchecked version of that lambda
057     */
058    static <T, R> UncheckedFunction<T, R> uncheck(final UncheckedFunction<T, R> p) {
059        return p;
060    }
061}