/** * 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; } } }