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.Predicate;
021
022import javax.jcr.RepositoryException;
023
024import org.fcrepo.kernel.api.exception.RepositoryRuntimeException;
025
026/**
027 * Operations that throw {@link RepositoryException} cannot be used as lambdas without "unchecking" those exceptions.
028 *
029 * @author ajs6f
030 * @param <T> the type of the input to the predicate
031 */
032@FunctionalInterface
033public interface UncheckedPredicate<T> extends Predicate<T> {
034
035    @Override
036    default boolean test(final T elem) {
037        try {
038            return testThrows(elem);
039        } catch (final RepositoryException e) {
040            throw new RepositoryRuntimeException(e);
041        }
042    }
043
044    /**
045     * The same semantic as {@link #test(Object)}, but allowed to throw a {@link RepositoryException}
046     *
047     * @param elem the input argument
048     * @return true if the input matches the predicate, otherwise false
049     * @throws RepositoryException a repository-related exception
050     */
051    boolean testThrows(T elem) throws RepositoryException;
052
053    /**
054     * @param <T> the type of the input to the predicate
055     * @param p a lambda expression
056     * @return an unchecked version of that lambda
057     */
058    static <T> UncheckedPredicate<T> uncheck(final UncheckedPredicate<T> p) {
059        return p;
060    }
061}