@Override public Void call() throws Exception { out.call(item); return null; } }
@Override public void call(T item) throws Exception { for (Callback<T> callback : callbacks) { callback.call(item); } } }
/** * For every element in the iterable, invoke the given callback. * Stops if {@link CallbackRefusedException} is thrown. */ public static <T> void stream(Callback<T> callback, Iterable<T> iterable) throws Exception { for (T item : iterable) { try { callback.call(item); } catch (CallbackRefusedException e) { return; } } }
@Override public void call(A item) throws Exception { callback.call(transformer.apply(item)); } }
private void commitInternal() throws CallbackRefusedException { final List<T> outList = new ArrayList<T>(size); list.drainTo(outList); if (!outList.isEmpty()) { try { out.call(outList); } catch (final Exception e) { Throwables.propagateIfInstanceOf(e, CallbackRefusedException.class); throw Throwables.propagate(e); } } } }
callback.call(iter.next());
@Test public void testSimple() throws Exception { CallbackCollector<String> callback = new CallbackCollector<>(); Callback<Integer> tested = transform(callback, new Function<Integer, String>() { @Override @Nullable public String apply(@Nullable Integer input) { return input == null ? null : Integer.toHexString(input); } }); tested.call(null); tested.call(15); tested.call(0xDEADBEEF); assertEquals(Lists.newArrayList(null, "f", "deadbeef"), callback.getItems()); }
@Test public void testWildcards() throws Exception { CallbackCollector<Object> callback = new CallbackCollector<>(); Callback<String> strCallback = transform(callback, Functions.toStringFunction()); strCallback.call("foo"); strCallback.call("bar"); Function<Super, Class<?>> superFunction = new Function<Super, Class<?>>() { @Override @Nullable public Class<?> apply(@Nullable Super input) { return input.getClass(); } }; Callback<Super> superCallback = transform(callback, superFunction); Callback<Sub> subCallback = transform(callback, superFunction); superCallback.call(new Super()); superCallback.call(new Sub()); subCallback.call(new Sub()); assertEquals(Lists.newArrayList("foo", "bar", Super.class, Sub.class, Sub.class), callback.getItems()); }