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.kernel.api.utils;
018
019import java.util.function.Function;
020import javax.jcr.RepositoryException;
021
022import org.fcrepo.kernel.api.exception.RepositoryRuntimeException;
023
024/**
025 * Operations that throw {@link RepositoryException} cannot be used as lambdas without "unchecking" those exceptions.
026 *
027 * @author ajs6f
028 * @param <T>
029 */
030@FunctionalInterface
031public interface UncheckedFunction<T, R> extends Function<T, R> {
032
033    @Override
034    default R apply(final T elem) {
035        try {
036            return applyThrows(elem);
037        } catch (final RepositoryException e) {
038            throw new RepositoryRuntimeException(e);
039        }
040    }
041
042    /**
043     * The same semantic as {@link #apply(Object)}, but allowed to throw a {@link RepositoryException}
044     *
045     * @param elem
046     * @return
047     * @throws RepositoryException
048     */
049    R applyThrows(T elem) throws RepositoryException;
050
051    /**
052     * @param p a lambda expression
053     * @return an unchecked version of that lambda
054     */
055    static <T, R> UncheckedFunction<T, R> uncheck(final UncheckedFunction<T, R> p) {
056        return p;
057    }
058}