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.Predicate;
020
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 predicate
030 */
031@FunctionalInterface
032public interface UncheckedPredicate<T> extends Predicate<T> {
033
034    @Override
035    default boolean test(final T elem) {
036        try {
037            return testThrows(elem);
038        } catch (final RepositoryException e) {
039            throw new RepositoryRuntimeException(e);
040        }
041    }
042
043    /**
044     * The same semantic as {@link #test(Object)}, but allowed to throw a {@link RepositoryException}
045     *
046     * @param elem the input argument
047     * @return true if the input matches the predicate, otherwise false
048     * @throws RepositoryException a repository-related exception
049     */
050    boolean testThrows(T elem) throws RepositoryException;
051
052    /**
053     * @param <T> the type of the input to the predicate
054     * @param p a lambda expression
055     * @return an unchecked version of that lambda
056     */
057    static <T> UncheckedPredicate<T> uncheck(final UncheckedPredicate<T> p) {
058        return p;
059    }
060}