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