@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 testGood() throws Exception { ExecutorService executor = MoreExecutors.sameThreadExecutor(); CallbackCollector<List<String>> out = new CallbackCollector<>(); BatchingCallback<String> batcher = BatchingCallback.batchInto(2, executor, out, false); batcher.call("a"); batcher.call("b"); batcher.call("c"); batcher.call("d"); batcher.call("e"); batcher.commit(); assertEquals(of(of("a", "b"), of("c", "d"), of("e")), out.getItems()); }
@Test public void testBatchingCallback() throws Exception { CallbackCollector<List<String>> collector = new CallbackCollector<>(); try (BatchingCallback<String> batcher = BatchingCallback.batchInto(2, collector)) { batcher.call("a"); batcher.call("b"); batcher.call("c"); batcher.commit(); batcher.call("d"); batcher.call("e"); batcher.call("f"); } assertEquals(of( of("a", "b"), of("c"), of("d", "e"), of("f") ), collector.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()); }