From fc93b32955b3a30ad884e4a9b14ebe052b86f120 Mon Sep 17 00:00:00 2001 From: Paul Duncan Date: Sat, 8 Sep 2018 14:59:18 -0400 Subject: java: replace ResultHandler with Appendable interface --- java/src/main/java/org/pablotron/luigi/Cache.java | 11 ++++---- .../java/org/pablotron/luigi/ResultHandler.java | 13 --------- .../main/java/org/pablotron/luigi/Template.java | 31 +++++++++++----------- .../java/org/pablotron/luigi/tests/CacheTest.java | 7 ++--- .../org/pablotron/luigi/tests/FiltersTest.java | 1 - .../org/pablotron/luigi/tests/TemplateTest.java | 6 ++--- .../pablotron/luigi/tests/TestResultHandler.java | 21 ++++++++++++--- 7 files changed, 47 insertions(+), 43 deletions(-) delete mode 100644 java/src/main/java/org/pablotron/luigi/ResultHandler.java diff --git a/java/src/main/java/org/pablotron/luigi/Cache.java b/java/src/main/java/org/pablotron/luigi/Cache.java index b93d564..65849fa 100644 --- a/java/src/main/java/org/pablotron/luigi/Cache.java +++ b/java/src/main/java/org/pablotron/luigi/Cache.java @@ -2,6 +2,7 @@ package org.pablotron.luigi; import java.util.Map; import java.util.HashMap; +import java.io.IOException; import org.pablotron.luigi.Filter; import org.pablotron.luigi.Template; @@ -54,7 +55,7 @@ public final class Cache { public String run( final String key, final Map args - ) throws LuigiError { + ) throws LuigiError, IOException { // run template with args return get(key).run(args); } @@ -65,17 +66,17 @@ public final class Cache { * * @param key Template key. * @param args Template arguments map. - * @param rh Result handler. + * @param out Appendable output interface. * * @throws UnknownTemplateError if the given template does not exist. */ public void run( final String key, final Map args, - final ResultHandler rh - ) throws LuigiError { + final Appendable out + ) throws LuigiError, IOException { // run template with args and result handler - get(key).run(args, rh); + get(key).run(args, out); } /** diff --git a/java/src/main/java/org/pablotron/luigi/ResultHandler.java b/java/src/main/java/org/pablotron/luigi/ResultHandler.java deleted file mode 100644 index 65654e9..0000000 --- a/java/src/main/java/org/pablotron/luigi/ResultHandler.java +++ /dev/null @@ -1,13 +0,0 @@ -package org.pablotron.luigi; - -/** - * Result handler for streamed results from template runs. - */ -public interface ResultHandler { - /** - * Called during template run with each chunk of the result. - * - * @param chunk Chunk of result. - */ - public abstract void append(final String chunk); -}; diff --git a/java/src/main/java/org/pablotron/luigi/Template.java b/java/src/main/java/org/pablotron/luigi/Template.java index 73eb89d..5c40f3c 100644 --- a/java/src/main/java/org/pablotron/luigi/Template.java +++ b/java/src/main/java/org/pablotron/luigi/Template.java @@ -1,6 +1,7 @@ package org.pablotron.luigi; import java.util.Map; +import java.io.IOException; import org.pablotron.luigi.Parser; import org.pablotron.luigi.Filter; @@ -66,11 +67,11 @@ public final class Template { } /** - * Run this template with given arguments, and pass each result chunks - * to the given result handler. + * Run this template with given arguments, and pass each chunk of + # the result to the given output object. * * @param args Template arguments. - * @param rh Result handler. + * @param out Appendable output interface. * * @throws UnknownKeyError If a key specified in the template does not exist. * @throws UnknownFilterError If a filter specified in the template does not exist. @@ -78,10 +79,10 @@ public final class Template { */ public void run( final Map args, - final ResultHandler r - ) throws LuigiError { + final Appendable out + ) throws LuigiError, IOException { for (Action a: this.actions) { - r.append(a.run(this.filters, args)); + out.append(a.run(this.filters, args)); } } @@ -127,9 +128,9 @@ public final class Template { public static void run( final String template, final Map args, - final ResultHandler rh - ) throws LuigiError { - run(template, args, Filter.FILTERS, rh); + final Appendable out + ) throws LuigiError, IOException { + run(template, args, Filter.FILTERS, out); } /** @@ -156,13 +157,13 @@ public final class Template { } /** - * Create and run template with given arguments, filters, and result - * handler. + * Create and run template with given arguments, filters, and + * appendable. * * @param template Template string. * @param args Template arguments. * @param filters Template filters. - * @param rh Result handler that result chunks are passed to. + * @param out Appendable output interface. * * @throws UnknownKeyError If a key specified in the template does not exist. * @throws UnknownFilterError If a filter specified in the template does not exist. @@ -172,9 +173,9 @@ public final class Template { final String template, final Map args, final Map filters, - final ResultHandler rh - ) throws LuigiError { + final Appendable out + ) throws LuigiError, IOException { final Template t = new Template(template, filters); - t.run(args, rh); + t.run(args, out); } }; diff --git a/java/src/test/java/org/pablotron/luigi/tests/CacheTest.java b/java/src/test/java/org/pablotron/luigi/tests/CacheTest.java index f0e2ab5..f59d2b8 100644 --- a/java/src/test/java/org/pablotron/luigi/tests/CacheTest.java +++ b/java/src/test/java/org/pablotron/luigi/tests/CacheTest.java @@ -2,6 +2,7 @@ package org.pablotron.luigi.tests; import java.util.Map; import java.util.HashMap; +import java.io.IOException; import org.pablotron.luigi.Template; import org.pablotron.luigi.Filter; @@ -31,20 +32,20 @@ public final class CacheTest { }}; @Test - public void testCache() throws LuigiError { + public void testCache() throws LuigiError, IOException { final Cache cache = new Cache(TEST_TEMPLATES); assertEquals("foofoofoo", cache.run("foo", TEST_ARGS)); } @Test - public void testCacheWithCustomFilters() throws LuigiError { + public void testCacheWithCustomFilters() throws LuigiError, IOException { final Cache cache = new Cache(TEST_TEMPLATES, TEST_FILTERS); assertEquals("foo-custom-foo-filter-foo", cache.run("foo-custom", TEST_ARGS)); } @Test - public void testCacheGetWithResultHandler() throws LuigiError { + public void testCacheGetWithResultHandler() throws LuigiError, IOException { final Cache cache = new Cache(TEST_TEMPLATES); final StringBuilder sb = new StringBuilder(); final TestResultHandler rh = new TestResultHandler(sb); diff --git a/java/src/test/java/org/pablotron/luigi/tests/FiltersTest.java b/java/src/test/java/org/pablotron/luigi/tests/FiltersTest.java index 4800318..25034b0 100644 --- a/java/src/test/java/org/pablotron/luigi/tests/FiltersTest.java +++ b/java/src/test/java/org/pablotron/luigi/tests/FiltersTest.java @@ -5,7 +5,6 @@ import org.pablotron.luigi.Template; import org.pablotron.luigi.Filter; import org.pablotron.luigi.errors.LuigiError; import org.pablotron.luigi.errors.FilterError; -import org.pablotron.luigi.ResultHandler; import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.jupiter.api.Test; diff --git a/java/src/test/java/org/pablotron/luigi/tests/TemplateTest.java b/java/src/test/java/org/pablotron/luigi/tests/TemplateTest.java index 83a137d..9862ae1 100644 --- a/java/src/test/java/org/pablotron/luigi/tests/TemplateTest.java +++ b/java/src/test/java/org/pablotron/luigi/tests/TemplateTest.java @@ -2,10 +2,10 @@ package org.pablotron.luigi.tests; import java.util.Map; import java.util.HashMap; +import java.io.IOException; import org.pablotron.luigi.Template; import org.pablotron.luigi.errors.LuigiError; -import org.pablotron.luigi.ResultHandler; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; @@ -44,7 +44,7 @@ public final class TemplateTest { } @Test - public void testResultHandler() throws LuigiError { + public void testResultHandler() throws LuigiError, IOException { final Template t = new Template("foo%{bar}"); final StringBuilder sb = new StringBuilder(); final TestResultHandler rh = new TestResultHandler(sb); @@ -56,7 +56,7 @@ public final class TemplateTest { } @Test - public void testStaticResultHandler() throws LuigiError { + public void testStaticResultHandler() throws LuigiError, IOException { final StringBuilder sb = new StringBuilder(); final TestResultHandler rh = new TestResultHandler(sb); diff --git a/java/src/test/java/org/pablotron/luigi/tests/TestResultHandler.java b/java/src/test/java/org/pablotron/luigi/tests/TestResultHandler.java index a847e79..d924cff 100644 --- a/java/src/test/java/org/pablotron/luigi/tests/TestResultHandler.java +++ b/java/src/test/java/org/pablotron/luigi/tests/TestResultHandler.java @@ -1,15 +1,30 @@ package org.pablotron.luigi.tests; -import org.pablotron.luigi.ResultHandler; +import java.io.IOException; -public final class TestResultHandler implements ResultHandler { +public final class TestResultHandler implements Appendable { private final StringBuilder sb; public TestResultHandler(final StringBuilder sb) { this.sb = sb; } - public void append(final String s) { + public Appendable append(final char c) throws IOException { + sb.append(c); + return this; + } + + public Appendable append(final CharSequence s) throws IOException { sb.append(s); + return this; + } + + public Appendable append( + final CharSequence s, + final int start, + final int end + ) throws IOException { + sb.append(s, start, end); + return this; } }; -- cgit v1.2.3