/** * Unwrap to find the outermost function that is an instance of {@code clazz}. * @param <C> Type of class. * @param clazz Implementation class to search for. * @return outermost function that is an instance of {@code clazz}, * {@code null} if {@code clazz} is not implemented by {@code this} * or any function it wraps. * * @see WrappedFunction#unwrap(Class, Object) */ public <C> C unwrap(Class<C> clazz) { return unwrap(clazz, this); }
/** * Unwrap a function object to find the outermost function that implements {@code clazz}. * If a function object is not an instance of {@code clazz} but is an instance of * {@code WrappedFunction} then the test is repeated on the value of {@link #f()}. * * @param <C> Type of class. * @param clazz Implementation class to search for. * @param wf Function to unwrap * @return outermost function that implements {@code clazz}, {@code null} if * if {@code clazz} is not implemented by {@code wf} or any function it wraps. */ public static <C> C unwrap(Class<C> clazz, Object wf) { for (;;) { if (clazz.isInstance(wf)) return clazz.cast(wf); if (wf instanceof WrappedFunction) wf = ((WrappedFunction<?>) wf).f(); else return null; } } }
/** * Unwrap a function object to find the outermost function that implements {@code clazz}. * If a function object is not an instance of {@code clazz} but is an instance of * {@code WrappedFunction} then the test is repeated on the value of {@link #f()}. * * @param <C> Type of class. * @param clazz Implementation class to search for. * @param wf Function to unwrap * @return outermost function that implements {@code clazz}, {@code null} if * if {@code clazz} is not implemented by {@code wf} or any function it wraps. */ public static <C> C unwrap(Class<C> clazz, Object wf) { for (;;) { if (clazz.isInstance(wf)) return clazz.cast(wf); if (wf instanceof WrappedFunction) wf = ((WrappedFunction<?>) wf).f(); else return null; } } }
/** * Unwrap to find the outermost function that is an instance of {@code clazz}. * @param <C> Type of class. * @param clazz Implementation class to search for. * @return outermost function that is an instance of {@code clazz}, * {@code null} if {@code clazz} is not implemented by {@code this} * or any function it wraps. * * @see WrappedFunction#unwrap(Class, Object) */ public <C> C unwrap(Class<C> clazz) { return unwrap(clazz, this); }
/** * Close the function. * If {@code function} is an instance of {@code AutoCloseable} * then close is called, otherwise no action is taken. * @param function Function to be closed. * @throws Exception Error throwing function. */ public static void closeFunction(Object function) throws Exception { AutoCloseable closeable = WrappedFunction.unwrap(AutoCloseable.class, function); if (closeable != null) closeable.close(); }
/** * Close the function. * If {@code function} is an instance of {@code AutoCloseable} * then close is called, otherwise no action is taken. * @param function Function to be closed. * @throws Exception Error throwing function. */ public static void closeFunction(Object function) throws Exception { AutoCloseable closeable = WrappedFunction.unwrap(AutoCloseable.class, function); if (closeable != null) closeable.close(); }