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