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> 030 */ 031@FunctionalInterface 032public interface UncheckedPredicate<T> extends Predicate<T>, com.google.common.base.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 @Override 044 default boolean apply(final T elem) { 045 return test(elem); 046 } 047 048 /** 049 * The same semantic as {@link #test(Object)}, but allowed to throw a {@link RepositoryException} 050 * 051 * @param elem 052 * @return 053 * @throws RepositoryException 054 */ 055 boolean testThrows(T elem) throws RepositoryException; 056 057 /** 058 * @param p a lambda expression 059 * @return an unchecked version of that lambda 060 */ 061 static <T> UncheckedPredicate<T> uncheck(final UncheckedPredicate<T> p) { 062 return p; 063 } 064}