From df558e0be789520db584db03013bc26ff69ba66c Mon Sep 17 00:00:00 2001 From: super132 Date: Thu, 16 Apr 2015 08:03:27 +0800 Subject: [PATCH 01/49] Concat implementation commit --- src/main/java/org/takes/misc/Concat.java | 93 +++++++++++++++++++ .../org/takes/misc/IterableTransform.java | 62 +++++++++++++ src/main/java/org/takes/rq/RqWithHeader.java | 17 ++-- .../java/org/takes/rq/RqWithoutHeader.java | 28 ++++-- src/main/java/org/takes/rs/RsWithHeader.java | 18 ++-- src/main/java/org/takes/rs/RsWithHeaders.java | 27 ++++-- src/main/java/org/takes/rs/RsWithStatus.java | 36 ++++--- .../java/org/takes/rs/RsWithoutHeader.java | 28 ++++-- 8 files changed, 250 insertions(+), 59 deletions(-) create mode 100644 src/main/java/org/takes/misc/Concat.java create mode 100644 src/main/java/org/takes/misc/IterableTransform.java diff --git a/src/main/java/org/takes/misc/Concat.java b/src/main/java/org/takes/misc/Concat.java new file mode 100644 index 000000000..fbe855339 --- /dev/null +++ b/src/main/java/org/takes/misc/Concat.java @@ -0,0 +1,93 @@ +/** + * The MIT License (MIT) + * + * Copyright (c) 2015 Yegor Bugayenko + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package org.takes.misc; + +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; + +/** + * Concat iterable. + * + * @author Yegor Bugayenko (yegor@teamed.io) + * @version $Id$ + * @since 0.32.1 + */ +public final class Concat implements Iterable { + + private final List storage = new LinkedList(); + + public static interface Condition { + /** + * Determine if an element should be added. + * @param element + * @return + */ + boolean add(T element); + } + + /** + * To produce an iterable collection combining a and b, with order of the elements in a first. + * @param a + * @param b + */ + public Concat(Iterable a, Iterable b) { + concat(a); + concat(b); + } + + /** + * To produce an iterable collection, determined by condition, combining a and b, with order of the elements in a first. + * @param a + * @param b + * @param cond + */ + public Concat(Iterable a, Iterable b, Condition cond) { + concat(a, cond); + concat(b, cond); + } + + private void concat(Iterable a, Condition cond) { + Iterator i = a.iterator(); + while(i.hasNext()) { + T element = i.next(); + if(cond.add(element)) { + this.storage.add(element); + } + } + } + + private void concat(Iterable a) { + Iterator i = a.iterator(); + while(i.hasNext()) { + this.storage.add(i.next()); + } + } + + @Override + public Iterator iterator() { + return this.storage.iterator(); + } + +} diff --git a/src/main/java/org/takes/misc/IterableTransform.java b/src/main/java/org/takes/misc/IterableTransform.java new file mode 100644 index 000000000..751969754 --- /dev/null +++ b/src/main/java/org/takes/misc/IterableTransform.java @@ -0,0 +1,62 @@ +/** + * The MIT License (MIT) + * + * Copyright (c) 2015 Yegor Bugayenko + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package org.takes.misc; + +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; + +/** + * Transform elements in an iterable into others. + * + * @author Yegor Bugayenko (yegor@teamed.io) + * @version $Id$ + * @since 0.32.1 + */ +public class IterableTransform implements Iterable { + + private final List storage = new LinkedList(); + + public static interface TransformAction { + T transform(T element); + } + + /** + * Transform elements in the supplied iterable by the action supplied. + * @param list + * @param action + */ + public IterableTransform(Iterable list, IterableTransform.TransformAction action) { + Iterator i = list.iterator(); + while(i.hasNext()) { + this.storage.add(action.transform(i.next())); + } + } + + @Override + public Iterator iterator() { + return this.storage.iterator(); + } + +} diff --git a/src/main/java/org/takes/rq/RqWithHeader.java b/src/main/java/org/takes/rq/RqWithHeader.java index 99c4c1df3..d15a1086c 100644 --- a/src/main/java/org/takes/rq/RqWithHeader.java +++ b/src/main/java/org/takes/rq/RqWithHeader.java @@ -25,10 +25,12 @@ import java.io.IOException; import java.io.InputStream; -import java.util.LinkedList; -import java.util.List; +import java.util.Collections; + import lombok.EqualsAndHashCode; + import org.takes.Request; +import org.takes.misc.Concat; /** * Request with extra header. @@ -62,13 +64,10 @@ public RqWithHeader(final Request req, final CharSequence header) { super( new Request() { @Override - public List head() throws IOException { - final List head = new LinkedList(); - for (final String hdr : req.head()) { - head.add(hdr); - } - head.add(header.toString()); - return head; + public Iterable head() throws IOException { + return new Concat ( + req.head(), + Collections.singleton(header.toString())); } @Override public InputStream body() throws IOException { diff --git a/src/main/java/org/takes/rq/RqWithoutHeader.java b/src/main/java/org/takes/rq/RqWithoutHeader.java index 07d429937..8176f8941 100644 --- a/src/main/java/org/takes/rq/RqWithoutHeader.java +++ b/src/main/java/org/takes/rq/RqWithoutHeader.java @@ -25,11 +25,13 @@ import java.io.IOException; import java.io.InputStream; -import java.util.Collection; -import java.util.LinkedList; +import java.util.Collections; import java.util.Locale; + import lombok.EqualsAndHashCode; + import org.takes.Request; +import org.takes.misc.Concat; /** * Request without a header (even if it was absent). @@ -53,17 +55,23 @@ public RqWithoutHeader(final Request req, final CharSequence name) { new Request() { @Override public Iterable head() throws IOException { - final Collection head = new LinkedList(); final String prefix = String.format( "%s:", name.toString().toLowerCase(Locale.ENGLISH) ); - for (final String header : req.head()) { - if (!header.toLowerCase(Locale.ENGLISH) - .startsWith(prefix)) { - head.add(header); - } - } - return head; + + return new Concat( + req.head(), + Collections.EMPTY_LIST, + new Concat.Condition() { + + @Override + public boolean add(String element) { + return !element.toLowerCase(Locale.ENGLISH) + .startsWith(prefix); + } + + }); + } @Override public InputStream body() throws IOException { diff --git a/src/main/java/org/takes/rs/RsWithHeader.java b/src/main/java/org/takes/rs/RsWithHeader.java index f6ab3fd82..3302f23e8 100644 --- a/src/main/java/org/takes/rs/RsWithHeader.java +++ b/src/main/java/org/takes/rs/RsWithHeader.java @@ -25,11 +25,15 @@ import java.io.IOException; import java.io.InputStream; +import java.util.Collections; import java.util.LinkedList; import java.util.List; import java.util.regex.Pattern; + import lombok.EqualsAndHashCode; + import org.takes.Response; +import org.takes.misc.Concat; /** * Response decorator, with an additional header. @@ -103,7 +107,7 @@ public RsWithHeader(final Response res, final CharSequence header) { super( new Response() { @Override - public List head() throws IOException { + public Iterable head() throws IOException { return RsWithHeader.extend(res.head(), header.toString()); } @Override @@ -121,7 +125,7 @@ public InputStream body() throws IOException { * @return Head with additional header * @throws IOException If fails */ - private static List extend(final Iterable head, + private static Iterable extend(final Iterable head, final String header) throws IOException { if (!HEADER.matcher(header).matches()) { throw new IllegalArgumentException( @@ -132,12 +136,10 @@ private static List extend(final Iterable head, ) ); } - final List headers = new LinkedList(); - for (final String hdr : head) { - headers.add(hdr); - } - headers.add(header); - return headers; + + return new Concat( + head, + Collections.singleton(header)); } } diff --git a/src/main/java/org/takes/rs/RsWithHeaders.java b/src/main/java/org/takes/rs/RsWithHeaders.java index 5cdc65af3..c2bd7ec4e 100644 --- a/src/main/java/org/takes/rs/RsWithHeaders.java +++ b/src/main/java/org/takes/rs/RsWithHeaders.java @@ -28,8 +28,12 @@ import java.util.Arrays; import java.util.LinkedList; import java.util.List; + import lombok.EqualsAndHashCode; + import org.takes.Response; +import org.takes.misc.Concat; +import org.takes.misc.IterableTransform; /** * Response decorator, with an additional headers. @@ -61,15 +65,20 @@ public RsWithHeaders(final Response res, final Iterable headers) { super( new Response() { @Override - public List head() throws IOException { - final List head = new LinkedList(); - for (final String hdr : res.head()) { - head.add(hdr); - } - for (final String header : headers) { - head.add(header.trim()); - } - return head; + public Iterable head() throws IOException { + + return new Concat( + res.head(), + new IterableTransform(headers, + new IterableTransform.TransformAction() { + + @Override + public String transform(String element) { + return element.trim(); + } + + } + )); } @Override public InputStream body() throws IOException { diff --git a/src/main/java/org/takes/rs/RsWithStatus.java b/src/main/java/org/takes/rs/RsWithStatus.java index db01955ac..4863b85e4 100644 --- a/src/main/java/org/takes/rs/RsWithStatus.java +++ b/src/main/java/org/takes/rs/RsWithStatus.java @@ -26,14 +26,15 @@ import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; -import java.util.Collection; import java.util.Collections; -import java.util.LinkedList; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; + import lombok.EqualsAndHashCode; + import org.takes.Response; +import org.takes.misc.Concat; /** * Response decorator, with status code. @@ -111,17 +112,26 @@ private static Iterable head(final Response origin, ) ); } - final Collection head = new LinkedList(); - head.add(String.format("HTTP/1.1 %d %s", status, reason)); - boolean first = true; - for (final String hdr : origin.head()) { - if (first) { - first = false; - } else { - head.add(hdr); - } - } - return head; + + return new Concat( + Collections.singleton(String.format("HTTP/1.1 %d %s", status, reason)), + new Concat(origin.head(), + Collections.EMPTY_LIST, + new Concat.Condition() { + + private boolean first = true; + + @Override + public boolean add(String element) { + if (first) { + first = false; + return false; + } else { + return true; + } + } + + })); } /** diff --git a/src/main/java/org/takes/rs/RsWithoutHeader.java b/src/main/java/org/takes/rs/RsWithoutHeader.java index 51c122e41..c53109ba1 100644 --- a/src/main/java/org/takes/rs/RsWithoutHeader.java +++ b/src/main/java/org/takes/rs/RsWithoutHeader.java @@ -25,11 +25,13 @@ import java.io.IOException; import java.io.InputStream; -import java.util.Collection; -import java.util.LinkedList; +import java.util.Collections; import java.util.Locale; + import lombok.EqualsAndHashCode; + import org.takes.Response; +import org.takes.misc.Concat; /** * Response decorator, without a header. @@ -53,17 +55,23 @@ public RsWithoutHeader(final Response res, final String name) { new Response() { @Override public Iterable head() throws IOException { - final Collection head = new LinkedList(); final String prefix = String.format( "%s:", name.toLowerCase(Locale.ENGLISH) ); - for (final String hdr : res.head()) { - if (!hdr.toLowerCase(Locale.ENGLISH) - .startsWith(prefix)) { - head.add(hdr); - } - } - return head; + + return new Concat( + res.head(), + Collections.EMPTY_LIST, + new Concat.Condition() { + + @Override + public boolean add(String element) { + return !element.toLowerCase(Locale.ENGLISH) + .startsWith(prefix); + } + + }); + } @Override public InputStream body() throws IOException { From 2c0ea83f4084df62ac4231b92ec0cc67863f002f Mon Sep 17 00:00:00 2001 From: super132 Date: Thu, 16 Apr 2015 10:34:09 +0800 Subject: [PATCH 02/49] Committing unit test cases --- src/main/java/org/takes/misc/Concat.java | 2 +- .../org/takes/misc/IterableTransform.java | 2 +- src/test/java/org/takes/misc/ConcatTest.java | 89 +++++++++++++++++++ .../org/takes/misc/IterableTransformTest.java | 59 ++++++++++++ 4 files changed, 150 insertions(+), 2 deletions(-) create mode 100644 src/test/java/org/takes/misc/ConcatTest.java create mode 100644 src/test/java/org/takes/misc/IterableTransformTest.java diff --git a/src/main/java/org/takes/misc/Concat.java b/src/main/java/org/takes/misc/Concat.java index fbe855339..ab5447912 100644 --- a/src/main/java/org/takes/misc/Concat.java +++ b/src/main/java/org/takes/misc/Concat.java @@ -36,7 +36,7 @@ */ public final class Concat implements Iterable { - private final List storage = new LinkedList(); + private final transient List storage = new LinkedList(); public static interface Condition { /** diff --git a/src/main/java/org/takes/misc/IterableTransform.java b/src/main/java/org/takes/misc/IterableTransform.java index 751969754..48ed7871c 100644 --- a/src/main/java/org/takes/misc/IterableTransform.java +++ b/src/main/java/org/takes/misc/IterableTransform.java @@ -36,7 +36,7 @@ */ public class IterableTransform implements Iterable { - private final List storage = new LinkedList(); + private final transient List storage = new LinkedList(); public static interface TransformAction { T transform(T element); diff --git a/src/test/java/org/takes/misc/ConcatTest.java b/src/test/java/org/takes/misc/ConcatTest.java new file mode 100644 index 000000000..b49c4d820 --- /dev/null +++ b/src/test/java/org/takes/misc/ConcatTest.java @@ -0,0 +1,89 @@ +/** + * The MIT License (MIT) + * + * Copyright (c) 2015 Yegor Bugayenko + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package org.takes.misc; + +import java.util.ArrayList; +import java.util.List; + +import org.hamcrest.MatcherAssert; + +import static org.hamcrest.Matchers.*; + +import org.junit.Test; + +/** + * Test case for {@link Concat}. + * @author Yegor Bugayenko (yegor@teamed.io) + * @version $Id$ + * @since 0.32.1 + */ +public class ConcatTest { + + @Test + public void concat() { + List a = new ArrayList(); + a.add("a1"); + a.add("a2"); + List b = new ArrayList(); + b.add("b1"); + b.add("b2"); + MatcherAssert.assertThat((Iterable)new Concat(a,b), hasItems("a1", "a2", "b1", "b2")); + } + + @Test + public void concatWithEmpty() { + List a = new ArrayList(); + a.add("a1"); + a.add("a2"); + List b = new ArrayList(); + + MatcherAssert.assertThat((Iterable)new Concat(a,b), hasItems("a1", "a2")); + MatcherAssert.assertThat((Iterable)new Concat(a,b), not(hasItems(""))); + //ensure concat empty lists will be empty + MatcherAssert.assertThat((Iterable)new Concat(b,b), emptyIterable()); + } + + @Test + public void concatWithCondition() { + List a = new ArrayList(); + a.add("a1"); + a.add("a2"); + List b = new ArrayList(); + b.add("b1"); + b.add("b2"); + + Iterable result = new Concat(a,b, new Concat.Condition() { + + @Override + public boolean add(String element) { + return element.endsWith("1"); + } + + }); + + MatcherAssert.assertThat(result, hasItems("a1", "b1")); + MatcherAssert.assertThat(result, not(hasItems("a2", "b2"))); + } + +} diff --git a/src/test/java/org/takes/misc/IterableTransformTest.java b/src/test/java/org/takes/misc/IterableTransformTest.java new file mode 100644 index 000000000..e88f628fd --- /dev/null +++ b/src/test/java/org/takes/misc/IterableTransformTest.java @@ -0,0 +1,59 @@ +/** + * The MIT License (MIT) + * + * Copyright (c) 2015 Yegor Bugayenko + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package org.takes.misc; + +import java.util.ArrayList; +import java.util.List; + +import org.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; +import org.junit.Test; + +/** + * Test case for {@link IterableTransform}. + * @author Yegor Bugayenko (yegor@teamed.io) + * @version $Id$ + * @since 0.32.1 + */ +public class IterableTransformTest { + + @Test + public void iterableTransform() { + List a = new ArrayList(); + a.add("a1"); + a.add("b1"); + a.add("c1"); + + MatcherAssert.assertThat(new IterableTransform(a, new IterableTransform.TransformAction() { + + @Override + public String transform(String element) { + return element.concat("t"); + } + + }), Matchers.hasItems("a1t", "b1t", "c1t")); + + } + +} From 86739ffb75daeb329b920bc8e96cdd8a8a5b544b Mon Sep 17 00:00:00 2001 From: Jason Wong Date: Thu, 16 Apr 2015 10:47:26 +0800 Subject: [PATCH 03/49] Suppressing warnings for EMPTY_LIST usage --- src/main/java/org/takes/rq/RqWithoutHeader.java | 1 + src/main/java/org/takes/rs/RsWithStatus.java | 1 + src/main/java/org/takes/rs/RsWithoutHeader.java | 1 + 3 files changed, 3 insertions(+) diff --git a/src/main/java/org/takes/rq/RqWithoutHeader.java b/src/main/java/org/takes/rq/RqWithoutHeader.java index 8176f8941..93bddb14f 100644 --- a/src/main/java/org/takes/rq/RqWithoutHeader.java +++ b/src/main/java/org/takes/rq/RqWithoutHeader.java @@ -54,6 +54,7 @@ public RqWithoutHeader(final Request req, final CharSequence name) { super( new Request() { @Override + @SuppressWarnings("unchecked") public Iterable head() throws IOException { final String prefix = String.format( "%s:", name.toString().toLowerCase(Locale.ENGLISH) diff --git a/src/main/java/org/takes/rs/RsWithStatus.java b/src/main/java/org/takes/rs/RsWithStatus.java index 4863b85e4..c72d0de2c 100644 --- a/src/main/java/org/takes/rs/RsWithStatus.java +++ b/src/main/java/org/takes/rs/RsWithStatus.java @@ -100,6 +100,7 @@ public InputStream body() throws IOException { * @return Head * @throws IOException If fails */ + @SuppressWarnings("unchecked") private static Iterable head(final Response origin, final int status, final String reason) throws IOException { // @checkstyle MagicNumber (1 line) diff --git a/src/main/java/org/takes/rs/RsWithoutHeader.java b/src/main/java/org/takes/rs/RsWithoutHeader.java index c53109ba1..c4c50efc8 100644 --- a/src/main/java/org/takes/rs/RsWithoutHeader.java +++ b/src/main/java/org/takes/rs/RsWithoutHeader.java @@ -54,6 +54,7 @@ public RsWithoutHeader(final Response res, final String name) { super( new Response() { @Override + @SuppressWarnings("unchecked") public Iterable head() throws IOException { final String prefix = String.format( "%s:", name.toLowerCase(Locale.ENGLISH) From 89db00cf91a4a79dd97da79ea3bd0f55076580d7 Mon Sep 17 00:00:00 2001 From: super132 Date: Thu, 16 Apr 2015 11:20:20 +0800 Subject: [PATCH 04/49] Fixing code styling issues --- src/main/java/org/takes/misc/Concat.java | 125 ++++++++++-------- .../org/takes/misc/IterableTransform.java | 57 ++++---- src/main/java/org/takes/rq/RqWithHeader.java | 5 +- .../java/org/takes/rq/RqWithoutHeader.java | 20 ++- src/main/java/org/takes/rs/RsWithHeader.java | 4 +- src/main/java/org/takes/rs/RsWithHeaders.java | 22 +-- src/main/java/org/takes/rs/RsWithStatus.java | 35 +++-- .../java/org/takes/rs/RsWithoutHeader.java | 23 ++-- src/test/java/org/takes/misc/ConcatTest.java | 96 +++++++------- .../org/takes/misc/IterableTransformTest.java | 38 +++--- 10 files changed, 228 insertions(+), 197 deletions(-) diff --git a/src/main/java/org/takes/misc/Concat.java b/src/main/java/org/takes/misc/Concat.java index ab5447912..c4d225e53 100644 --- a/src/main/java/org/takes/misc/Concat.java +++ b/src/main/java/org/takes/misc/Concat.java @@ -29,65 +29,86 @@ /** * Concat iterable. - * + * * @author Yegor Bugayenko (yegor@teamed.io) * @version $Id$ * @since 0.32.1 */ public final class Concat implements Iterable { - private final transient List storage = new LinkedList(); - - public static interface Condition { - /** - * Determine if an element should be added. - * @param element - * @return - */ - boolean add(T element); - } - - /** - * To produce an iterable collection combining a and b, with order of the elements in a first. - * @param a - * @param b - */ - public Concat(Iterable a, Iterable b) { - concat(a); - concat(b); - } - - /** - * To produce an iterable collection, determined by condition, combining a and b, with order of the elements in a first. - * @param a - * @param b - * @param cond - */ - public Concat(Iterable a, Iterable b, Condition cond) { - concat(a, cond); - concat(b, cond); - } + /** + * Internal storage to hold the elements from iterables. + */ + private final transient List storage = new LinkedList(); + + public interface Condition { + /** + * Determine if an element should be added. + * + * @param element The element in the iterables to examine. + * @return True to add the element, false to skip. + */ + boolean add(T element); + } + + /** + * To produce an iterable collection combining a and b, with order of the + * elements in a first. + * + * @param aitb First iterable to concat + * @param bitb Second iterable to conat + */ + public Concat(final Iterable aitb, final Iterable bitb) { + this.concat(aitb); + this.concat(bitb); + } + + /** + * To produce an iterable collection, determined by condition, combining a + * and b, with order of the elements in a first. + * + * @param aitb First iterable to concat + * @param bitb Second iterable to conat + * @param cond + * To determine which element in the iterables to add in the + * final iterable. + */ + public Concat(final Iterable aitb, final Iterable bitb, final Condition cond) { + this.concat(aitb, cond); + this.concat(bitb, cond); + } + + /** + * Adding an iterable into storage with condition. + * + * @param itb Iterable to add + * @param cond Condition to determine the element should be added + */ + private void concat(final Iterable itb, final Condition cond) { + Iterator itr = itb.iterator(); + while (itr.hasNext()) { + T element = itr.next(); + if (cond.add(element)) { + this.storage.add(element); + } + } + } - private void concat(Iterable a, Condition cond) { - Iterator i = a.iterator(); - while(i.hasNext()) { - T element = i.next(); - if(cond.add(element)) { - this.storage.add(element); - } - } - } + /** + * Adding an iterable into storage. + * + * @param itb Iterable to add + */ + private void concat(final Iterable itb) { + Iterator itr = itb.iterator(); + while (itr.hasNext()) { + this.storage.add(itr.next()); + } + } - private void concat(Iterable a) { - Iterator i = a.iterator(); - while(i.hasNext()) { - this.storage.add(i.next()); - } - } - - @Override - public Iterator iterator() { - return this.storage.iterator(); - } + @Override + public Iterator iterator() { + return this.storage.iterator(); + } } diff --git a/src/main/java/org/takes/misc/IterableTransform.java b/src/main/java/org/takes/misc/IterableTransform.java index 48ed7871c..b5bea7f45 100644 --- a/src/main/java/org/takes/misc/IterableTransform.java +++ b/src/main/java/org/takes/misc/IterableTransform.java @@ -29,34 +29,45 @@ /** * Transform elements in an iterable into others. - * + * * @author Yegor Bugayenko (yegor@teamed.io) * @version $Id$ * @since 0.32.1 */ public class IterableTransform implements Iterable { - private final transient List storage = new LinkedList(); - - public static interface TransformAction { - T transform(T element); - } - - /** - * Transform elements in the supplied iterable by the action supplied. - * @param list - * @param action - */ - public IterableTransform(Iterable list, IterableTransform.TransformAction action) { - Iterator i = list.iterator(); - while(i.hasNext()) { - this.storage.add(action.transform(i.next())); - } - } - - @Override - public Iterator iterator() { - return this.storage.iterator(); - } + /** + * Internal storage. + */ + private final transient List storage = new LinkedList(); + + public static interface TransformAction { + /** + * The transform action of the element. + * + * @param element Element of the iterable + * @return Transformed element + */ + T transform(T element); + } + + /** + * Transform elements in the supplied iterable by the action supplied. + * + * @param list Iterable to be transformed + * @param action The actual transformation implementation + */ + public IterableTransform(final Iterable list, + final IterableTransform.TransformAction action) { + Iterator itr = list.iterator(); + while (itr.hasNext()) { + this.storage.add(action.transform(itr.next())); + } + } + + @Override + public Iterator iterator() { + return this.storage.iterator(); + } } diff --git a/src/main/java/org/takes/rq/RqWithHeader.java b/src/main/java/org/takes/rq/RqWithHeader.java index d15a1086c..b06b965f2 100644 --- a/src/main/java/org/takes/rq/RqWithHeader.java +++ b/src/main/java/org/takes/rq/RqWithHeader.java @@ -65,9 +65,8 @@ public RqWithHeader(final Request req, final CharSequence header) { new Request() { @Override public Iterable head() throws IOException { - return new Concat ( - req.head(), - Collections.singleton(header.toString())); + return new Concat(req.head(), + Collections.singleton(header.toString())); } @Override public InputStream body() throws IOException { diff --git a/src/main/java/org/takes/rq/RqWithoutHeader.java b/src/main/java/org/takes/rq/RqWithoutHeader.java index 93bddb14f..ffb2e128b 100644 --- a/src/main/java/org/takes/rq/RqWithoutHeader.java +++ b/src/main/java/org/takes/rq/RqWithoutHeader.java @@ -60,18 +60,16 @@ public Iterable head() throws IOException { "%s:", name.toString().toLowerCase(Locale.ENGLISH) ); - return new Concat( - req.head(), - Collections.EMPTY_LIST, - new Concat.Condition() { + return new Concat(req.head(), Collections.EMPTY_LIST, + new Concat.Condition() { - @Override - public boolean add(String element) { - return !element.toLowerCase(Locale.ENGLISH) - .startsWith(prefix); - } - - }); + @Override + public boolean add(final String element) { + return !element.toLowerCase(Locale.ENGLISH) + .startsWith(prefix); + } + + }); } @Override diff --git a/src/main/java/org/takes/rs/RsWithHeader.java b/src/main/java/org/takes/rs/RsWithHeader.java index 3302f23e8..863acf05a 100644 --- a/src/main/java/org/takes/rs/RsWithHeader.java +++ b/src/main/java/org/takes/rs/RsWithHeader.java @@ -137,9 +137,7 @@ private static Iterable extend(final Iterable head, ); } - return new Concat( - head, - Collections.singleton(header)); + return new Concat(head, Collections.singleton(header)); } } diff --git a/src/main/java/org/takes/rs/RsWithHeaders.java b/src/main/java/org/takes/rs/RsWithHeaders.java index c2bd7ec4e..8d2a9c9fb 100644 --- a/src/main/java/org/takes/rs/RsWithHeaders.java +++ b/src/main/java/org/takes/rs/RsWithHeaders.java @@ -68,17 +68,17 @@ public RsWithHeaders(final Response res, final Iterable headers) { public Iterable head() throws IOException { return new Concat( - res.head(), - new IterableTransform(headers, - new IterableTransform.TransformAction() { - - @Override - public String transform(String element) { - return element.trim(); - } - - } - )); + res.head(), + new IterableTransform( + headers, + new IterableTransform.TransformAction() { + + @Override + public String transform(final String element) { + return element.trim(); + } + + })); } @Override public InputStream body() throws IOException { diff --git a/src/main/java/org/takes/rs/RsWithStatus.java b/src/main/java/org/takes/rs/RsWithStatus.java index c72d0de2c..4946dbd40 100644 --- a/src/main/java/org/takes/rs/RsWithStatus.java +++ b/src/main/java/org/takes/rs/RsWithStatus.java @@ -114,25 +114,24 @@ private static Iterable head(final Response origin, ); } - return new Concat( - Collections.singleton(String.format("HTTP/1.1 %d %s", status, reason)), - new Concat(origin.head(), - Collections.EMPTY_LIST, - new Concat.Condition() { - - private boolean first = true; + return new Concat(Collections.singleton(String.format( + "HTTP/1.1 %d %s", status, reason)), new Concat( + origin.head(), Collections.EMPTY_LIST, + new Concat.Condition() { - @Override - public boolean add(String element) { - if (first) { - first = false; - return false; - } else { - return true; - } - } - - })); + private boolean first = true; + + @Override + public boolean add(final String element) { + if (first) { + first = false; + return false; + } else { + return true; + } + } + + })); } /** diff --git a/src/main/java/org/takes/rs/RsWithoutHeader.java b/src/main/java/org/takes/rs/RsWithoutHeader.java index c4c50efc8..9b554055d 100644 --- a/src/main/java/org/takes/rs/RsWithoutHeader.java +++ b/src/main/java/org/takes/rs/RsWithoutHeader.java @@ -60,19 +60,16 @@ public Iterable head() throws IOException { "%s:", name.toLowerCase(Locale.ENGLISH) ); - return new Concat( - res.head(), - Collections.EMPTY_LIST, - new Concat.Condition() { - - @Override - public boolean add(String element) { - return !element.toLowerCase(Locale.ENGLISH) - .startsWith(prefix); - } - - }); - + return new Concat(res.head(), Collections.EMPTY_LIST, + new Concat.Condition() { + + @Override + public boolean add(final String element) { + return !element.toLowerCase(Locale.ENGLISH) + .startsWith(prefix); + } + + }); } @Override public InputStream body() throws IOException { diff --git a/src/test/java/org/takes/misc/ConcatTest.java b/src/test/java/org/takes/misc/ConcatTest.java index b49c4d820..1d5acefe3 100644 --- a/src/test/java/org/takes/misc/ConcatTest.java +++ b/src/test/java/org/takes/misc/ConcatTest.java @@ -34,56 +34,62 @@ /** * Test case for {@link Concat}. + * * @author Yegor Bugayenko (yegor@teamed.io) * @version $Id$ * @since 0.32.1 */ public class ConcatTest { - - @Test - public void concat() { - List a = new ArrayList(); - a.add("a1"); - a.add("a2"); - List b = new ArrayList(); - b.add("b1"); - b.add("b2"); - MatcherAssert.assertThat((Iterable)new Concat(a,b), hasItems("a1", "a2", "b1", "b2")); - } - - @Test - public void concatWithEmpty() { - List a = new ArrayList(); - a.add("a1"); - a.add("a2"); - List b = new ArrayList(); - - MatcherAssert.assertThat((Iterable)new Concat(a,b), hasItems("a1", "a2")); - MatcherAssert.assertThat((Iterable)new Concat(a,b), not(hasItems(""))); - //ensure concat empty lists will be empty - MatcherAssert.assertThat((Iterable)new Concat(b,b), emptyIterable()); - } - - @Test - public void concatWithCondition() { - List a = new ArrayList(); - a.add("a1"); - a.add("a2"); - List b = new ArrayList(); - b.add("b1"); - b.add("b2"); - - Iterable result = new Concat(a,b, new Concat.Condition() { - @Override - public boolean add(String element) { - return element.endsWith("1"); - } - - }); - - MatcherAssert.assertThat(result, hasItems("a1", "b1")); - MatcherAssert.assertThat(result, not(hasItems("a2", "b2"))); - } + @Test + public void concat() { + List a = new ArrayList(); + a.add("a1"); + a.add("a2"); + List b = new ArrayList(); + b.add("b1"); + b.add("b2"); + MatcherAssert.assertThat((Iterable) new Concat(a, b), + hasItems("a1", "a2", "b1", "b2")); + } + + @Test + public void concatWithEmpty() { + List a = new ArrayList(); + a.add("a1"); + a.add("a2"); + List b = new ArrayList(); + + MatcherAssert.assertThat((Iterable) new Concat(a, b), + hasItems("a1", "a2")); + MatcherAssert.assertThat((Iterable) new Concat(a, b), + not(hasItems(""))); + // ensure concat empty lists will be empty + MatcherAssert.assertThat((Iterable) new Concat(b, b), + emptyIterable()); + } + + @Test + public void concatWithCondition() { + List a = new ArrayList(); + a.add("a1"); + a.add("a2"); + List b = new ArrayList(); + b.add("b1"); + b.add("b2"); + + Iterable result = new Concat(a, b, + new Concat.Condition() { + + @Override + public boolean add(String element) { + return element.endsWith("1"); + } + + }); + + MatcherAssert.assertThat(result, hasItems("a1", "b1")); + MatcherAssert.assertThat(result, not(hasItems("a2", "b2"))); + } } diff --git a/src/test/java/org/takes/misc/IterableTransformTest.java b/src/test/java/org/takes/misc/IterableTransformTest.java index e88f628fd..a43cec175 100644 --- a/src/test/java/org/takes/misc/IterableTransformTest.java +++ b/src/test/java/org/takes/misc/IterableTransformTest.java @@ -32,28 +32,30 @@ /** * Test case for {@link IterableTransform}. + * * @author Yegor Bugayenko (yegor@teamed.io) * @version $Id$ * @since 0.32.1 */ public class IterableTransformTest { - - @Test - public void iterableTransform() { - List a = new ArrayList(); - a.add("a1"); - a.add("b1"); - a.add("c1"); - - MatcherAssert.assertThat(new IterableTransform(a, new IterableTransform.TransformAction() { - - @Override - public String transform(String element) { - return element.concat("t"); - } - - }), Matchers.hasItems("a1t", "b1t", "c1t")); - - } + + @Test + public void iterableTransform() { + List a = new ArrayList(); + a.add("a1"); + a.add("b1"); + a.add("c1"); + + MatcherAssert.assertThat(new IterableTransform(a, + new IterableTransform.TransformAction() { + + @Override + public String transform(String element) { + return element.concat("t"); + } + + }), Matchers.hasItems("a1t", "b1t", "c1t")); + + } } From 5956ed42d76d696919cc2bfaae85527c956a91af Mon Sep 17 00:00:00 2001 From: Jason Wong Date: Thu, 16 Apr 2015 12:20:33 +0800 Subject: [PATCH 05/49] Another fixes to styling and refactoring --- src/main/java/org/takes/misc/Concat.java | 41 +++++----- .../org/takes/misc/IterableTransform.java | 28 +++---- src/main/java/org/takes/misc/LcCondition.java | 57 +++++++++++++ src/main/java/org/takes/rq/RqWithHeader.java | 8 +- .../java/org/takes/rq/RqWithoutHeader.java | 21 ++--- src/main/java/org/takes/rs/RsWithHeader.java | 4 - src/main/java/org/takes/rs/RsWithHeaders.java | 15 ++-- src/main/java/org/takes/rs/RsWithStatus.java | 40 +++++----- .../java/org/takes/rs/RsWithoutHeader.java | 20 ++--- src/test/java/org/takes/misc/ConcatTest.java | 79 +++++++++++-------- .../org/takes/misc/IterableTransformTest.java | 35 ++++---- 11 files changed, 198 insertions(+), 150 deletions(-) create mode 100644 src/main/java/org/takes/misc/LcCondition.java diff --git a/src/main/java/org/takes/misc/Concat.java b/src/main/java/org/takes/misc/Concat.java index c4d225e53..26ff189b9 100644 --- a/src/main/java/org/takes/misc/Concat.java +++ b/src/main/java/org/takes/misc/Concat.java @@ -41,20 +41,10 @@ public final class Concat implements Iterable { */ private final transient List storage = new LinkedList(); - public interface Condition { - /** - * Determine if an element should be added. - * - * @param element The element in the iterables to examine. - * @return True to add the element, false to skip. - */ - boolean add(T element); - } - /** * To produce an iterable collection combining a and b, with order of the * elements in a first. - * + * * @param aitb First iterable to concat * @param bitb Second iterable to conat */ @@ -69,25 +59,24 @@ public Concat(final Iterable aitb, final Iterable bitb) { * * @param aitb First iterable to concat * @param bitb Second iterable to conat - * @param cond - * To determine which element in the iterables to add in the - * final iterable. + * @param cond To determine which element to add in the final iterable */ - public Concat(final Iterable aitb, final Iterable bitb, final Condition cond) { + public Concat(final Iterable aitb, final Iterable bitb, + final Condition cond) { this.concat(aitb, cond); this.concat(bitb, cond); } /** * Adding an iterable into storage with condition. - * + * * @param itb Iterable to add * @param cond Condition to determine the element should be added */ private void concat(final Iterable itb, final Condition cond) { - Iterator itr = itb.iterator(); + final Iterator itr = itb.iterator(); while (itr.hasNext()) { - T element = itr.next(); + final T element = itr.next(); if (cond.add(element)) { this.storage.add(element); } @@ -96,19 +85,29 @@ private void concat(final Iterable itb, final Condition cond) { /** * Adding an iterable into storage. - * + * * @param itb Iterable to add */ private void concat(final Iterable itb) { - Iterator itr = itb.iterator(); + final Iterator itr = itb.iterator(); while (itr.hasNext()) { this.storage.add(itr.next()); } } @Override - public Iterator iterator() { + public final Iterator iterator() { return this.storage.iterator(); } + + public interface Condition { + /** + * Determine if an element should be added. + * + * @param element The element in the iterables to examine. + * @return True to add the element, false to skip. + */ + boolean add(T element); + } } diff --git a/src/main/java/org/takes/misc/IterableTransform.java b/src/main/java/org/takes/misc/IterableTransform.java index b5bea7f45..eaff6e3f2 100644 --- a/src/main/java/org/takes/misc/IterableTransform.java +++ b/src/main/java/org/takes/misc/IterableTransform.java @@ -41,33 +41,33 @@ public class IterableTransform implements Iterable { */ private final transient List storage = new LinkedList(); - public static interface TransformAction { - /** - * The transform action of the element. - * - * @param element Element of the iterable - * @return Transformed element - */ - T transform(T element); - } - /** * Transform elements in the supplied iterable by the action supplied. - * + * * @param list Iterable to be transformed * @param action The actual transformation implementation */ public IterableTransform(final Iterable list, - final IterableTransform.TransformAction action) { - Iterator itr = list.iterator(); + final IterableTransform.Action action) { + final Iterator itr = list.iterator(); while (itr.hasNext()) { this.storage.add(action.transform(itr.next())); } } @Override - public Iterator iterator() { + public final Iterator iterator() { return this.storage.iterator(); } + + public interface Action { + /** + * The transform action of the element. + * + * @param element Element of the iterable + * @return Transformed element + */ + T transform(T element); + } } diff --git a/src/main/java/org/takes/misc/LcCondition.java b/src/main/java/org/takes/misc/LcCondition.java new file mode 100644 index 000000000..377cef554 --- /dev/null +++ b/src/main/java/org/takes/misc/LcCondition.java @@ -0,0 +1,57 @@ +/** + * The MIT License (MIT) + * + * Copyright (c) 2015 Yegor Bugayenko + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package org.takes.misc; + +import java.util.Locale; +import org.takes.misc.Concat.Condition; + +/** + * Concat lower case string condition. + * + * @author Yegor Bugayenko (yegor@teamed.io) + * @version $Id$ + * @since 0.32.1 + */ +public final class LcCondition implements Condition { + + /** + * Prefix. + */ + private String prefix; + + /** + * Ctor. + * @param prefix The prefix to check + */ + public LcCondition(final String prefix) { + this.prefix = prefix; + } + + @Override + public boolean add(final String element) { + return !element.toLowerCase(Locale.ENGLISH) + .startsWith(this.prefix); + } + +} diff --git a/src/main/java/org/takes/rq/RqWithHeader.java b/src/main/java/org/takes/rq/RqWithHeader.java index b06b965f2..6eefc14e8 100644 --- a/src/main/java/org/takes/rq/RqWithHeader.java +++ b/src/main/java/org/takes/rq/RqWithHeader.java @@ -26,9 +26,7 @@ import java.io.IOException; import java.io.InputStream; import java.util.Collections; - import lombok.EqualsAndHashCode; - import org.takes.Request; import org.takes.misc.Concat; @@ -65,8 +63,10 @@ public RqWithHeader(final Request req, final CharSequence header) { new Request() { @Override public Iterable head() throws IOException { - return new Concat(req.head(), - Collections.singleton(header.toString())); + return new Concat( + req.head(), + Collections.singleton(header.toString()) + ); } @Override public InputStream body() throws IOException { diff --git a/src/main/java/org/takes/rq/RqWithoutHeader.java b/src/main/java/org/takes/rq/RqWithoutHeader.java index ffb2e128b..76cdf4d8c 100644 --- a/src/main/java/org/takes/rq/RqWithoutHeader.java +++ b/src/main/java/org/takes/rq/RqWithoutHeader.java @@ -27,11 +27,10 @@ import java.io.InputStream; import java.util.Collections; import java.util.Locale; - import lombok.EqualsAndHashCode; - import org.takes.Request; import org.takes.misc.Concat; +import org.takes.misc.LcCondition; /** * Request without a header (even if it was absent). @@ -59,18 +58,11 @@ public Iterable head() throws IOException { final String prefix = String.format( "%s:", name.toString().toLowerCase(Locale.ENGLISH) ); - - return new Concat(req.head(), Collections.EMPTY_LIST, - new Concat.Condition() { - - @Override - public boolean add(final String element) { - return !element.toLowerCase(Locale.ENGLISH) - .startsWith(prefix); - } - - }); - + return new Concat( + req.head(), + Collections.EMPTY_LIST, + new LcCondition(prefix) + ); } @Override public InputStream body() throws IOException { @@ -79,5 +71,4 @@ public InputStream body() throws IOException { } ); } - } diff --git a/src/main/java/org/takes/rs/RsWithHeader.java b/src/main/java/org/takes/rs/RsWithHeader.java index 863acf05a..27d746471 100644 --- a/src/main/java/org/takes/rs/RsWithHeader.java +++ b/src/main/java/org/takes/rs/RsWithHeader.java @@ -26,12 +26,8 @@ import java.io.IOException; import java.io.InputStream; import java.util.Collections; -import java.util.LinkedList; -import java.util.List; import java.util.regex.Pattern; - import lombok.EqualsAndHashCode; - import org.takes.Response; import org.takes.misc.Concat; diff --git a/src/main/java/org/takes/rs/RsWithHeaders.java b/src/main/java/org/takes/rs/RsWithHeaders.java index 8d2a9c9fb..141f8b9d6 100644 --- a/src/main/java/org/takes/rs/RsWithHeaders.java +++ b/src/main/java/org/takes/rs/RsWithHeaders.java @@ -26,11 +26,7 @@ import java.io.IOException; import java.io.InputStream; import java.util.Arrays; -import java.util.LinkedList; -import java.util.List; - import lombok.EqualsAndHashCode; - import org.takes.Response; import org.takes.misc.Concat; import org.takes.misc.IterableTransform; @@ -65,20 +61,19 @@ public RsWithHeaders(final Response res, final Iterable headers) { super( new Response() { @Override - public Iterable head() throws IOException { - + public Iterable head() throws IOException { return new Concat( res.head(), new IterableTransform( headers, - new IterableTransform.TransformAction() { - + new IterableTransform.Action() { @Override public String transform(final String element) { return element.trim(); } - - })); + } + ) + ); } @Override public InputStream body() throws IOException { diff --git a/src/main/java/org/takes/rs/RsWithStatus.java b/src/main/java/org/takes/rs/RsWithStatus.java index 4946dbd40..b24366748 100644 --- a/src/main/java/org/takes/rs/RsWithStatus.java +++ b/src/main/java/org/takes/rs/RsWithStatus.java @@ -30,9 +30,7 @@ import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; - import lombok.EqualsAndHashCode; - import org.takes.Response; import org.takes.misc.Concat; @@ -113,25 +111,27 @@ private static Iterable head(final Response origin, ) ); } - - return new Concat(Collections.singleton(String.format( - "HTTP/1.1 %d %s", status, reason)), new Concat( - origin.head(), Collections.EMPTY_LIST, - new Concat.Condition() { - - private boolean first = true; - - @Override - public boolean add(final String element) { - if (first) { - first = false; - return false; - } else { - return true; + return new Concat( + Collections.singleton( + String.format("HTTP/1.1 %d %s", status, reason)), + new Concat( + origin.head(), + Collections.EMPTY_LIST, + new Concat.Condition() { + /** + * boolean to determine first. + */ + private boolean first = true; + + @Override + public boolean add(final String element) { + final boolean ret = this.first; + this.first = this.first ? false : this.first; + return !ret; + } } - } - - })); + ) + ); } /** diff --git a/src/main/java/org/takes/rs/RsWithoutHeader.java b/src/main/java/org/takes/rs/RsWithoutHeader.java index 9b554055d..5c1d3a03c 100644 --- a/src/main/java/org/takes/rs/RsWithoutHeader.java +++ b/src/main/java/org/takes/rs/RsWithoutHeader.java @@ -27,11 +27,10 @@ import java.io.InputStream; import java.util.Collections; import java.util.Locale; - import lombok.EqualsAndHashCode; - import org.takes.Response; import org.takes.misc.Concat; +import org.takes.misc.LcCondition; /** * Response decorator, without a header. @@ -59,17 +58,11 @@ public Iterable head() throws IOException { final String prefix = String.format( "%s:", name.toLowerCase(Locale.ENGLISH) ); - - return new Concat(res.head(), Collections.EMPTY_LIST, - new Concat.Condition() { - - @Override - public boolean add(final String element) { - return !element.toLowerCase(Locale.ENGLISH) - .startsWith(prefix); - } - - }); + return new Concat( + res.head(), + Collections.EMPTY_LIST, + new LcCondition(prefix) + ); } @Override public InputStream body() throws IOException { @@ -78,5 +71,4 @@ public InputStream body() throws IOException { } ); } - } diff --git a/src/test/java/org/takes/misc/ConcatTest.java b/src/test/java/org/takes/misc/ConcatTest.java index 1d5acefe3..c0851f35f 100644 --- a/src/test/java/org/takes/misc/ConcatTest.java +++ b/src/test/java/org/takes/misc/ConcatTest.java @@ -25,11 +25,8 @@ import java.util.ArrayList; import java.util.List; - import org.hamcrest.MatcherAssert; - -import static org.hamcrest.Matchers.*; - +import org.hamcrest.Matchers; import org.junit.Test; /** @@ -39,20 +36,28 @@ * @version $Id$ * @since 0.32.1 */ -public class ConcatTest { +public final class ConcatTest { + /** + * Basic concat unit test + */ @Test public void concat() { - List a = new ArrayList(); - a.add("a1"); - a.add("a2"); - List b = new ArrayList(); - b.add("b1"); - b.add("b2"); - MatcherAssert.assertThat((Iterable) new Concat(a, b), - hasItems("a1", "a2", "b1", "b2")); + List alist = new ArrayList(); + alist.add("a1"); + alist.add("a2"); + List blist = new ArrayList(); + blist.add("b1"); + blist.add("b2"); + MatcherAssert.assertThat( + (Iterable) new Concat(alist, blist), + Matchers.hasItems("a1", "a2", "b1", "b2") + ); } + /** + * Concat test with empty inputs + */ @Test public void concatWithEmpty() { List a = new ArrayList(); @@ -60,36 +65,46 @@ public void concatWithEmpty() { a.add("a2"); List b = new ArrayList(); - MatcherAssert.assertThat((Iterable) new Concat(a, b), - hasItems("a1", "a2")); - MatcherAssert.assertThat((Iterable) new Concat(a, b), - not(hasItems(""))); + MatcherAssert.assertThat( + (Iterable) new Concat(a, b), + Matchers.hasItems("a1", "a2") + ); + MatcherAssert.assertThat( + (Iterable) new Concat(a, b), + Matchers.not(Matchers.hasItems("")) + ); // ensure concat empty lists will be empty - MatcherAssert.assertThat((Iterable) new Concat(b, b), - emptyIterable()); + MatcherAssert.assertThat( + (Iterable) new Concat(b, b), + Matchers.emptyIterable() + ); } + /** + * Concat test with condition + */ @Test public void concatWithCondition() { - List a = new ArrayList(); - a.add("a1"); - a.add("a2"); - List b = new ArrayList(); - b.add("b1"); - b.add("b2"); + List alist = new ArrayList(); + alist.add("a1"); + alist.add("a2"); + List blist = new ArrayList(); + blist.add("b1"); + blist.add("b2"); - Iterable result = new Concat(a, b, + Iterable result = new Concat( + alist, + blist, new Concat.Condition() { - @Override - public boolean add(String element) { + public boolean add(final String element) { return element.endsWith("1"); } + } + ); - }); - - MatcherAssert.assertThat(result, hasItems("a1", "b1")); - MatcherAssert.assertThat(result, not(hasItems("a2", "b2"))); + MatcherAssert.assertThat(result, Matchers.hasItems("a1", "b1")); + MatcherAssert.assertThat(result, Matchers.not(Matchers.hasItems("a2", "b2"))); } } diff --git a/src/test/java/org/takes/misc/IterableTransformTest.java b/src/test/java/org/takes/misc/IterableTransformTest.java index a43cec175..3e87f2963 100644 --- a/src/test/java/org/takes/misc/IterableTransformTest.java +++ b/src/test/java/org/takes/misc/IterableTransformTest.java @@ -25,7 +25,6 @@ import java.util.ArrayList; import java.util.List; - import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; import org.junit.Test; @@ -37,25 +36,29 @@ * @version $Id$ * @since 0.32.1 */ -public class IterableTransformTest { +public final class IterableTransformTest { + /** + * Iterable transform test + */ @Test public void iterableTransform() { - List a = new ArrayList(); - a.add("a1"); - a.add("b1"); - a.add("c1"); - - MatcherAssert.assertThat(new IterableTransform(a, - new IterableTransform.TransformAction() { - - @Override - public String transform(String element) { - return element.concat("t"); - } - - }), Matchers.hasItems("a1t", "b1t", "c1t")); + List alist = new ArrayList(); + alist.add("a1"); + alist.add("b1"); + alist.add("c1"); + MatcherAssert.assertThat( + new IterableTransform(alist, + new IterableTransform.Action() { + @Override + public String transform(final String element) { + return element.concat("t"); + } + } + ), + Matchers.hasItems("a1t", "b1t", "c1t") + ); } } From 442eda3b08fcc054303371bb0a7c01b3b917131f Mon Sep 17 00:00:00 2001 From: super132 Date: Thu, 16 Apr 2015 12:49:16 +0800 Subject: [PATCH 06/49] Further styling fixes --- src/main/java/org/takes/misc/Concat.java | 14 +++--- .../org/takes/misc/IterableTransform.java | 6 +-- src/main/java/org/takes/misc/LcCondition.java | 12 ++--- src/main/java/org/takes/rq/RqWithHeader.java | 6 +-- .../java/org/takes/rq/RqWithoutHeader.java | 8 ++-- src/main/java/org/takes/rs/RsWithHeader.java | 3 +- src/main/java/org/takes/rs/RsWithHeaders.java | 27 ++++++----- src/main/java/org/takes/rs/RsWithStatus.java | 38 ++++++++------- .../java/org/takes/rs/RsWithoutHeader.java | 8 ++-- src/test/java/org/takes/misc/ConcatTest.java | 47 ++++++++++--------- .../org/takes/misc/IterableTransformTest.java | 26 +++++----- 11 files changed, 102 insertions(+), 93 deletions(-) diff --git a/src/main/java/org/takes/misc/Concat.java b/src/main/java/org/takes/misc/Concat.java index 26ff189b9..17238c34d 100644 --- a/src/main/java/org/takes/misc/Concat.java +++ b/src/main/java/org/takes/misc/Concat.java @@ -29,7 +29,7 @@ /** * Concat iterable. - * + * * @author Yegor Bugayenko (yegor@teamed.io) * @version $Id$ * @since 0.32.1 @@ -56,7 +56,7 @@ public Concat(final Iterable aitb, final Iterable bitb) { /** * To produce an iterable collection, determined by condition, combining a * and b, with order of the elements in a first. - * + * * @param aitb First iterable to concat * @param bitb Second iterable to conat * @param cond To determine which element to add in the final iterable @@ -66,6 +66,11 @@ public Concat(final Iterable aitb, final Iterable bitb, this.concat(aitb, cond); this.concat(bitb, cond); } + + @Override + public final Iterator iterator() { + return this.storage.iterator(); + } /** * Adding an iterable into storage with condition. @@ -94,11 +99,6 @@ private void concat(final Iterable itb) { this.storage.add(itr.next()); } } - - @Override - public final Iterator iterator() { - return this.storage.iterator(); - } public interface Condition { /** diff --git a/src/main/java/org/takes/misc/IterableTransform.java b/src/main/java/org/takes/misc/IterableTransform.java index eaff6e3f2..a78180cc3 100644 --- a/src/main/java/org/takes/misc/IterableTransform.java +++ b/src/main/java/org/takes/misc/IterableTransform.java @@ -29,7 +29,7 @@ /** * Transform elements in an iterable into others. - * + * * @author Yegor Bugayenko (yegor@teamed.io) * @version $Id$ * @since 0.32.1 @@ -44,7 +44,7 @@ public class IterableTransform implements Iterable { /** * Transform elements in the supplied iterable by the action supplied. * - * @param list Iterable to be transformed + * @param list Iterable to be transformed * @param action The actual transformation implementation */ public IterableTransform(final Iterable list, @@ -59,7 +59,7 @@ public IterableTransform(final Iterable list, public final Iterator iterator() { return this.storage.iterator(); } - + public interface Action { /** * The transform action of the element. diff --git a/src/main/java/org/takes/misc/LcCondition.java b/src/main/java/org/takes/misc/LcCondition.java index 377cef554..fc7502aae 100644 --- a/src/main/java/org/takes/misc/LcCondition.java +++ b/src/main/java/org/takes/misc/LcCondition.java @@ -28,7 +28,7 @@ /** * Concat lower case string condition. - * + * * @author Yegor Bugayenko (yegor@teamed.io) * @version $Id$ * @since 0.32.1 @@ -39,15 +39,15 @@ public final class LcCondition implements Condition { * Prefix. */ private String prefix; - + /** * Ctor. - * @param prefix The prefix to check + * @param str The prefix to check */ - public LcCondition(final String prefix) { - this.prefix = prefix; + public LcCondition(final String str) { + this.prefix = str; } - + @Override public boolean add(final String element) { return !element.toLowerCase(Locale.ENGLISH) diff --git a/src/main/java/org/takes/rq/RqWithHeader.java b/src/main/java/org/takes/rq/RqWithHeader.java index 6eefc14e8..15eea31a2 100644 --- a/src/main/java/org/takes/rq/RqWithHeader.java +++ b/src/main/java/org/takes/rq/RqWithHeader.java @@ -64,9 +64,9 @@ public RqWithHeader(final Request req, final CharSequence header) { @Override public Iterable head() throws IOException { return new Concat( - req.head(), - Collections.singleton(header.toString()) - ); + req.head(), + Collections.singleton(header.toString()) + ); } @Override public InputStream body() throws IOException { diff --git a/src/main/java/org/takes/rq/RqWithoutHeader.java b/src/main/java/org/takes/rq/RqWithoutHeader.java index 76cdf4d8c..471a620c7 100644 --- a/src/main/java/org/takes/rq/RqWithoutHeader.java +++ b/src/main/java/org/takes/rq/RqWithoutHeader.java @@ -59,10 +59,10 @@ public Iterable head() throws IOException { "%s:", name.toString().toLowerCase(Locale.ENGLISH) ); return new Concat( - req.head(), - Collections.EMPTY_LIST, - new LcCondition(prefix) - ); + req.head(), + Collections.EMPTY_LIST, + new LcCondition(prefix) + ); } @Override public InputStream body() throws IOException { diff --git a/src/main/java/org/takes/rs/RsWithHeader.java b/src/main/java/org/takes/rs/RsWithHeader.java index 27d746471..63a0ede11 100644 --- a/src/main/java/org/takes/rs/RsWithHeader.java +++ b/src/main/java/org/takes/rs/RsWithHeader.java @@ -131,8 +131,7 @@ private static Iterable extend(final Iterable head, header, HEADER ) ); - } - + } return new Concat(head, Collections.singleton(header)); } diff --git a/src/main/java/org/takes/rs/RsWithHeaders.java b/src/main/java/org/takes/rs/RsWithHeaders.java index 141f8b9d6..1002ae431 100644 --- a/src/main/java/org/takes/rs/RsWithHeaders.java +++ b/src/main/java/org/takes/rs/RsWithHeaders.java @@ -26,7 +26,9 @@ import java.io.IOException; import java.io.InputStream; import java.util.Arrays; + import lombok.EqualsAndHashCode; + import org.takes.Response; import org.takes.misc.Concat; import org.takes.misc.IterableTransform; @@ -63,17 +65,12 @@ public RsWithHeaders(final Response res, final Iterable headers) { @Override public Iterable head() throws IOException { return new Concat( - res.head(), - new IterableTransform( - headers, - new IterableTransform.Action() { - @Override - public String transform(final String element) { - return element.trim(); - } - } - ) - ); + res.head(), + new IterableTransform( + headers, + new Action() + ) + ); } @Override public InputStream body() throws IOException { @@ -83,4 +80,12 @@ public InputStream body() throws IOException { ); } + private static final class Action implements IterableTransform.Action { + + @Override + public String transform(final String element) { + return element.trim(); + } + + } } diff --git a/src/main/java/org/takes/rs/RsWithStatus.java b/src/main/java/org/takes/rs/RsWithStatus.java index b24366748..9189ae181 100644 --- a/src/main/java/org/takes/rs/RsWithStatus.java +++ b/src/main/java/org/takes/rs/RsWithStatus.java @@ -112,26 +112,28 @@ private static Iterable head(final Response origin, ); } return new Concat( - Collections.singleton( - String.format("HTTP/1.1 %d %s", status, reason)), - new Concat( - origin.head(), - Collections.EMPTY_LIST, - new Concat.Condition() { - /** - * boolean to determine first. - */ - private boolean first = true; - - @Override - public boolean add(final String element) { - final boolean ret = this.first; - this.first = this.first ? false : this.first; - return !ret; + Collections.singleton( + String.format("HTTP/1.1 %d %s", status, reason) + ), + new Concat( + origin.head(), + Collections.EMPTY_LIST, + new Concat.Condition() { + /** + * Boolean to determine first. + */ + private boolean first = true; + @Override + public boolean add(final String element) { + final boolean ret = this.first; + if (this.first) { + this.first = false; } + return !ret; } - ) - ); + } + ) + ); } /** diff --git a/src/main/java/org/takes/rs/RsWithoutHeader.java b/src/main/java/org/takes/rs/RsWithoutHeader.java index 5c1d3a03c..c54a0c604 100644 --- a/src/main/java/org/takes/rs/RsWithoutHeader.java +++ b/src/main/java/org/takes/rs/RsWithoutHeader.java @@ -59,10 +59,10 @@ public Iterable head() throws IOException { "%s:", name.toLowerCase(Locale.ENGLISH) ); return new Concat( - res.head(), - Collections.EMPTY_LIST, - new LcCondition(prefix) - ); + res.head(), + Collections.EMPTY_LIST, + new LcCondition(prefix) + ); } @Override public InputStream body() throws IOException { diff --git a/src/test/java/org/takes/misc/ConcatTest.java b/src/test/java/org/takes/misc/ConcatTest.java index c0851f35f..39598f87f 100644 --- a/src/test/java/org/takes/misc/ConcatTest.java +++ b/src/test/java/org/takes/misc/ConcatTest.java @@ -31,7 +31,7 @@ /** * Test case for {@link Concat}. - * + * * @author Yegor Bugayenko (yegor@teamed.io) * @version $Id$ * @since 0.32.1 @@ -39,14 +39,14 @@ public final class ConcatTest { /** - * Basic concat unit test + * Basic concat unit test. */ @Test public void concat() { - List alist = new ArrayList(); + final List alist = new ArrayList(2); alist.add("a1"); alist.add("a2"); - List blist = new ArrayList(); + final List blist = new ArrayList(2); blist.add("b1"); blist.add("b2"); MatcherAssert.assertThat( @@ -56,41 +56,41 @@ public void concat() { } /** - * Concat test with empty inputs + * Concat test with empty inputs. */ @Test public void concatWithEmpty() { - List a = new ArrayList(); - a.add("a1"); - a.add("a2"); - List b = new ArrayList(); + final List alist = new ArrayList(2); + alist.add("an1"); + alist.add("an2"); + final List blist = new ArrayList(0); MatcherAssert.assertThat( - (Iterable) new Concat(a, b), - Matchers.hasItems("a1", "a2") + (Iterable) new Concat(alist, blist), + Matchers.hasItems("an1", "an2") ); MatcherAssert.assertThat( - (Iterable) new Concat(a, b), + (Iterable) new Concat(alist, blist), Matchers.not(Matchers.hasItems("")) ); // ensure concat empty lists will be empty MatcherAssert.assertThat( - (Iterable) new Concat(b, b), + (Iterable) new Concat(blist, blist), Matchers.emptyIterable() ); } /** - * Concat test with condition + * Concat test with condition. */ @Test public void concatWithCondition() { - List alist = new ArrayList(); - alist.add("a1"); - alist.add("a2"); - List blist = new ArrayList(); - blist.add("b1"); - blist.add("b2"); + final List alist = new ArrayList(2); + alist.add("at1"); + alist.add("at2"); + final List blist = new ArrayList(2); + blist.add("bt1"); + blist.add("bt2"); Iterable result = new Concat( alist, @@ -103,8 +103,11 @@ public boolean add(final String element) { } ); - MatcherAssert.assertThat(result, Matchers.hasItems("a1", "b1")); - MatcherAssert.assertThat(result, Matchers.not(Matchers.hasItems("a2", "b2"))); + MatcherAssert.assertThat(result, Matchers.hasItems("at1", "bt1")); + MatcherAssert.assertThat( + result, + Matchers.not(Matchers.hasItems("at2", "bt2")) + ); } } diff --git a/src/test/java/org/takes/misc/IterableTransformTest.java b/src/test/java/org/takes/misc/IterableTransformTest.java index 3e87f2963..ef7c00292 100644 --- a/src/test/java/org/takes/misc/IterableTransformTest.java +++ b/src/test/java/org/takes/misc/IterableTransformTest.java @@ -31,7 +31,7 @@ /** * Test case for {@link IterableTransform}. - * + * * @author Yegor Bugayenko (yegor@teamed.io) * @version $Id$ * @since 0.32.1 @@ -39,25 +39,25 @@ public final class IterableTransformTest { /** - * Iterable transform test + * Iterable transform test. */ @Test public void iterableTransform() { - List alist = new ArrayList(); + final List alist = new ArrayList(); alist.add("a1"); alist.add("b1"); alist.add("c1"); - MatcherAssert.assertThat( - new IterableTransform(alist, - new IterableTransform.Action() { - @Override - public String transform(final String element) { - return element.concat("t"); - } - } - ), - Matchers.hasItems("a1t", "b1t", "c1t") + new IterableTransform( + alist, + new IterableTransform.Action() { + @Override + public String transform(final String element) { + return element.concat("t"); + } + } + ), + Matchers.hasItems("a1t", "b1t", "c1t") ); } From 71641d8f529d23102501beef191d364b217db893 Mon Sep 17 00:00:00 2001 From: Jason Wong Date: Thu, 16 Apr 2015 12:59:10 +0800 Subject: [PATCH 07/49] Another fixes to styling. --- src/main/java/org/takes/misc/Concat.java | 6 +-- src/main/java/org/takes/rs/RsWithHeader.java | 2 +- src/main/java/org/takes/rs/RsWithHeaders.java | 7 ++-- src/test/java/org/takes/misc/ConcatTest.java | 38 ++++++++++++------- .../org/takes/misc/IterableTransformTest.java | 2 +- 5 files changed, 32 insertions(+), 23 deletions(-) diff --git a/src/main/java/org/takes/misc/Concat.java b/src/main/java/org/takes/misc/Concat.java index 17238c34d..b53a89b95 100644 --- a/src/main/java/org/takes/misc/Concat.java +++ b/src/main/java/org/takes/misc/Concat.java @@ -66,9 +66,9 @@ public Concat(final Iterable aitb, final Iterable bitb, this.concat(aitb, cond); this.concat(bitb, cond); } - + @Override - public final Iterator iterator() { + public Iterator iterator() { return this.storage.iterator(); } @@ -99,7 +99,7 @@ private void concat(final Iterable itb) { this.storage.add(itr.next()); } } - + public interface Condition { /** * Determine if an element should be added. diff --git a/src/main/java/org/takes/rs/RsWithHeader.java b/src/main/java/org/takes/rs/RsWithHeader.java index 63a0ede11..20dbc2ce7 100644 --- a/src/main/java/org/takes/rs/RsWithHeader.java +++ b/src/main/java/org/takes/rs/RsWithHeader.java @@ -131,7 +131,7 @@ private static Iterable extend(final Iterable head, header, HEADER ) ); - } + } return new Concat(head, Collections.singleton(header)); } diff --git a/src/main/java/org/takes/rs/RsWithHeaders.java b/src/main/java/org/takes/rs/RsWithHeaders.java index 1002ae431..520bc994d 100644 --- a/src/main/java/org/takes/rs/RsWithHeaders.java +++ b/src/main/java/org/takes/rs/RsWithHeaders.java @@ -26,9 +26,7 @@ import java.io.IOException; import java.io.InputStream; import java.util.Arrays; - import lombok.EqualsAndHashCode; - import org.takes.Response; import org.takes.misc.Concat; import org.takes.misc.IterableTransform; @@ -80,12 +78,13 @@ public InputStream body() throws IOException { ); } - private static final class Action implements IterableTransform.Action { + private static final class Action implements + IterableTransform.Action { @Override public String transform(final String element) { return element.trim(); } - + } } diff --git a/src/test/java/org/takes/misc/ConcatTest.java b/src/test/java/org/takes/misc/ConcatTest.java index 39598f87f..7deea463c 100644 --- a/src/test/java/org/takes/misc/ConcatTest.java +++ b/src/test/java/org/takes/misc/ConcatTest.java @@ -44,14 +44,18 @@ public final class ConcatTest { @Test public void concat() { final List alist = new ArrayList(2); - alist.add("a1"); - alist.add("a2"); + String astr1 = "a1"; + String astr2 = "a2"; + alist.add(astr1); + alist.add(astr2); final List blist = new ArrayList(2); - blist.add("b1"); - blist.add("b2"); + String bstr1 = "b1"; + String bstr2 = "b2"; + blist.add(bstr1); + blist.add(bstr2); MatcherAssert.assertThat( (Iterable) new Concat(alist, blist), - Matchers.hasItems("a1", "a2", "b1", "b2") + Matchers.hasItems(astr1, astr2, bstr1, bstr2) ); } @@ -61,13 +65,15 @@ public void concat() { @Test public void concatWithEmpty() { final List alist = new ArrayList(2); - alist.add("an1"); - alist.add("an2"); + String astr1 = "an1"; + String astr2 = "an2"; + alist.add(astr1); + alist.add(astr2); final List blist = new ArrayList(0); MatcherAssert.assertThat( (Iterable) new Concat(alist, blist), - Matchers.hasItems("an1", "an2") + Matchers.hasItems(astr1, astr2) ); MatcherAssert.assertThat( (Iterable) new Concat(alist, blist), @@ -86,11 +92,15 @@ public void concatWithEmpty() { @Test public void concatWithCondition() { final List alist = new ArrayList(2); - alist.add("at1"); - alist.add("at2"); + String astr1 = "at1"; + String astr2 = "at2"; + alist.add(astr1); + alist.add(astr2); final List blist = new ArrayList(2); - blist.add("bt1"); - blist.add("bt2"); + String bstr1 = "bt1"; + String bstr2 = "bt2"; + blist.add(bstr1); + blist.add(bstr2); Iterable result = new Concat( alist, @@ -103,10 +113,10 @@ public boolean add(final String element) { } ); - MatcherAssert.assertThat(result, Matchers.hasItems("at1", "bt1")); + MatcherAssert.assertThat(result, Matchers.hasItems(astr1, bstr1)); MatcherAssert.assertThat( result, - Matchers.not(Matchers.hasItems("at2", "bt2")) + Matchers.not(Matchers.hasItems(astr2, bstr2)) ); } diff --git a/src/test/java/org/takes/misc/IterableTransformTest.java b/src/test/java/org/takes/misc/IterableTransformTest.java index ef7c00292..c514ec345 100644 --- a/src/test/java/org/takes/misc/IterableTransformTest.java +++ b/src/test/java/org/takes/misc/IterableTransformTest.java @@ -43,7 +43,7 @@ public final class IterableTransformTest { */ @Test public void iterableTransform() { - final List alist = new ArrayList(); + final List alist = new ArrayList(3); alist.add("a1"); alist.add("b1"); alist.add("c1"); From f04c129cc7986b137c6bbb50a7738e5f2c418a07 Mon Sep 17 00:00:00 2001 From: super132 Date: Thu, 16 Apr 2015 13:06:08 +0800 Subject: [PATCH 08/49] Minor style fixes. --- src/main/java/org/takes/rs/RsWithHeaders.java | 2 +- src/test/java/org/takes/misc/ConcatTest.java | 54 +++++++++---------- 2 files changed, 27 insertions(+), 29 deletions(-) diff --git a/src/main/java/org/takes/rs/RsWithHeaders.java b/src/main/java/org/takes/rs/RsWithHeaders.java index 520bc994d..6047a272f 100644 --- a/src/main/java/org/takes/rs/RsWithHeaders.java +++ b/src/main/java/org/takes/rs/RsWithHeaders.java @@ -61,7 +61,7 @@ public RsWithHeaders(final Response res, final Iterable headers) { super( new Response() { @Override - public Iterable head() throws IOException { + public Iterable head() throws IOException { return new Concat( res.head(), new IterableTransform( diff --git a/src/test/java/org/takes/misc/ConcatTest.java b/src/test/java/org/takes/misc/ConcatTest.java index 7deea463c..9496ccaa5 100644 --- a/src/test/java/org/takes/misc/ConcatTest.java +++ b/src/test/java/org/takes/misc/ConcatTest.java @@ -44,18 +44,18 @@ public final class ConcatTest { @Test public void concat() { final List alist = new ArrayList(2); - String astr1 = "a1"; - String astr2 = "a2"; - alist.add(astr1); - alist.add(astr2); + String aone = "a1"; + String atwo = "a2"; + alist.add(aone); + alist.add(atwo); final List blist = new ArrayList(2); - String bstr1 = "b1"; - String bstr2 = "b2"; - blist.add(bstr1); - blist.add(bstr2); + String bone = "b1"; + String btwo = "b2"; + blist.add(bone); + blist.add(btwo); MatcherAssert.assertThat( (Iterable) new Concat(alist, blist), - Matchers.hasItems(astr1, astr2, bstr1, bstr2) + Matchers.hasItems(aone, atwo, bone, btwo) ); } @@ -65,15 +65,15 @@ public void concat() { @Test public void concatWithEmpty() { final List alist = new ArrayList(2); - String astr1 = "an1"; - String astr2 = "an2"; - alist.add(astr1); - alist.add(astr2); + String aone = "an1"; + String atwo = "an2"; + alist.add(aone); + alist.add(atwo); final List blist = new ArrayList(0); MatcherAssert.assertThat( (Iterable) new Concat(alist, blist), - Matchers.hasItems(astr1, astr2) + Matchers.hasItems(aone, atwo) ); MatcherAssert.assertThat( (Iterable) new Concat(alist, blist), @@ -92,18 +92,17 @@ public void concatWithEmpty() { @Test public void concatWithCondition() { final List alist = new ArrayList(2); - String astr1 = "at1"; - String astr2 = "at2"; - alist.add(astr1); - alist.add(astr2); + String aone = "at1"; + String atwo = "at2"; + alist.add(aone); + alist.add(atwo); final List blist = new ArrayList(2); - String bstr1 = "bt1"; - String bstr2 = "bt2"; - blist.add(bstr1); - blist.add(bstr2); - + String bone = "bt1"; + String btwo = "bt2"; + blist.add(bone); + blist.add(btwo); Iterable result = new Concat( - alist, + alist, blist, new Concat.Condition() { @Override @@ -112,11 +111,10 @@ public boolean add(final String element) { } } ); - - MatcherAssert.assertThat(result, Matchers.hasItems(astr1, bstr1)); + MatcherAssert.assertThat(result, Matchers.hasItems(aone, bone)); MatcherAssert.assertThat( - result, - Matchers.not(Matchers.hasItems(astr2, bstr2)) + result, + Matchers.not(Matchers.hasItems(atwo, btwo)) ); } From bc62609af3b87ab5401380c93d66c66fbd49c8bf Mon Sep 17 00:00:00 2001 From: super132 Date: Thu, 16 Apr 2015 13:08:48 +0800 Subject: [PATCH 09/49] Minor code styles fixes in unit test --- src/test/java/org/takes/misc/ConcatTest.java | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/test/java/org/takes/misc/ConcatTest.java b/src/test/java/org/takes/misc/ConcatTest.java index 9496ccaa5..56e056e56 100644 --- a/src/test/java/org/takes/misc/ConcatTest.java +++ b/src/test/java/org/takes/misc/ConcatTest.java @@ -44,13 +44,13 @@ public final class ConcatTest { @Test public void concat() { final List alist = new ArrayList(2); - String aone = "a1"; - String atwo = "a2"; + final String aone = "a1"; + final String atwo = "a2"; alist.add(aone); alist.add(atwo); final List blist = new ArrayList(2); - String bone = "b1"; - String btwo = "b2"; + final String bone = "b1"; + final String btwo = "b2"; blist.add(bone); blist.add(btwo); MatcherAssert.assertThat( @@ -65,8 +65,8 @@ public void concat() { @Test public void concatWithEmpty() { final List alist = new ArrayList(2); - String aone = "an1"; - String atwo = "an2"; + final String aone = "an1"; + final String atwo = "an2"; alist.add(aone); alist.add(atwo); final List blist = new ArrayList(0); @@ -92,13 +92,13 @@ public void concatWithEmpty() { @Test public void concatWithCondition() { final List alist = new ArrayList(2); - String aone = "at1"; - String atwo = "at2"; + final String aone = "at1"; + final String atwo = "at2"; alist.add(aone); alist.add(atwo); final List blist = new ArrayList(2); - String bone = "bt1"; - String btwo = "bt2"; + final String bone = "bt1"; + final String btwo = "bt2"; blist.add(bone); blist.add(btwo); Iterable result = new Concat( From 575fb8664eb9caac5dcb0464dfd612ce3ebb5114 Mon Sep 17 00:00:00 2001 From: super132 Date: Thu, 16 Apr 2015 13:11:57 +0800 Subject: [PATCH 10/49] Another minor code style fixes to unit test --- src/test/java/org/takes/misc/ConcatTest.java | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/src/test/java/org/takes/misc/ConcatTest.java b/src/test/java/org/takes/misc/ConcatTest.java index 56e056e56..f5c96ab17 100644 --- a/src/test/java/org/takes/misc/ConcatTest.java +++ b/src/test/java/org/takes/misc/ConcatTest.java @@ -70,7 +70,6 @@ public void concatWithEmpty() { alist.add(aone); alist.add(atwo); final List blist = new ArrayList(0); - MatcherAssert.assertThat( (Iterable) new Concat(alist, blist), Matchers.hasItems(aone, atwo) @@ -79,7 +78,6 @@ public void concatWithEmpty() { (Iterable) new Concat(alist, blist), Matchers.not(Matchers.hasItems("")) ); - // ensure concat empty lists will be empty MatcherAssert.assertThat( (Iterable) new Concat(blist, blist), Matchers.emptyIterable() @@ -102,15 +100,15 @@ public void concatWithCondition() { blist.add(bone); blist.add(btwo); Iterable result = new Concat( - alist, - blist, - new Concat.Condition() { - @Override - public boolean add(final String element) { - return element.endsWith("1"); - } + alist, + blist, + new Concat.Condition() { + @Override + public boolean add(final String element) { + return element.endsWith("1"); } - ); + } + ); MatcherAssert.assertThat(result, Matchers.hasItems(aone, bone)); MatcherAssert.assertThat( result, From 5eef73b56a5269defd12d3dc91796eb0bf24f51c Mon Sep 17 00:00:00 2001 From: super132 Date: Thu, 16 Apr 2015 13:16:21 +0800 Subject: [PATCH 11/49] More minor updates to code stlyes --- src/main/java/org/takes/misc/LcCondition.java | 2 +- src/test/java/org/takes/misc/ConcatTest.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/takes/misc/LcCondition.java b/src/main/java/org/takes/misc/LcCondition.java index fc7502aae..6f15ae81d 100644 --- a/src/main/java/org/takes/misc/LcCondition.java +++ b/src/main/java/org/takes/misc/LcCondition.java @@ -38,7 +38,7 @@ public final class LcCondition implements Condition { /** * Prefix. */ - private String prefix; + private final transient String prefix; /** * Ctor. diff --git a/src/test/java/org/takes/misc/ConcatTest.java b/src/test/java/org/takes/misc/ConcatTest.java index f5c96ab17..850ad350e 100644 --- a/src/test/java/org/takes/misc/ConcatTest.java +++ b/src/test/java/org/takes/misc/ConcatTest.java @@ -99,7 +99,7 @@ public void concatWithCondition() { final String btwo = "bt2"; blist.add(bone); blist.add(btwo); - Iterable result = new Concat( + final Iterable result = new Concat( alist, blist, new Concat.Condition() { From 202b6c7cce94763548f9f0f3ae46c98bef57f977 Mon Sep 17 00:00:00 2001 From: super132 Date: Thu, 16 Apr 2015 17:21:22 +0800 Subject: [PATCH 12/49] Correct author name and since version. --- src/main/java/org/takes/misc/Concat.java | 4 ++-- src/main/java/org/takes/misc/IterableTransform.java | 4 ++-- src/main/java/org/takes/misc/LcCondition.java | 4 ++-- src/test/java/org/takes/misc/ConcatTest.java | 4 ++-- src/test/java/org/takes/misc/IterableTransformTest.java | 4 ++-- 5 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/main/java/org/takes/misc/Concat.java b/src/main/java/org/takes/misc/Concat.java index b53a89b95..4facca92e 100644 --- a/src/main/java/org/takes/misc/Concat.java +++ b/src/main/java/org/takes/misc/Concat.java @@ -30,9 +30,9 @@ /** * Concat iterable. * - * @author Yegor Bugayenko (yegor@teamed.io) + * @author Jason Wong (super132j@yahoo.com) * @version $Id$ - * @since 0.32.1 + * @since 0.13.8 */ public final class Concat implements Iterable { diff --git a/src/main/java/org/takes/misc/IterableTransform.java b/src/main/java/org/takes/misc/IterableTransform.java index a78180cc3..a38b09e7c 100644 --- a/src/main/java/org/takes/misc/IterableTransform.java +++ b/src/main/java/org/takes/misc/IterableTransform.java @@ -30,9 +30,9 @@ /** * Transform elements in an iterable into others. * - * @author Yegor Bugayenko (yegor@teamed.io) + * @author Jason Wong (super132j@yahoo.com) * @version $Id$ - * @since 0.32.1 + * @since 0.13.8 */ public class IterableTransform implements Iterable { diff --git a/src/main/java/org/takes/misc/LcCondition.java b/src/main/java/org/takes/misc/LcCondition.java index 6f15ae81d..782a3a023 100644 --- a/src/main/java/org/takes/misc/LcCondition.java +++ b/src/main/java/org/takes/misc/LcCondition.java @@ -29,9 +29,9 @@ /** * Concat lower case string condition. * - * @author Yegor Bugayenko (yegor@teamed.io) + * @author Jason Wong (yegor@teamed.io) * @version $Id$ - * @since 0.32.1 + * @since 0.13.8 */ public final class LcCondition implements Condition { diff --git a/src/test/java/org/takes/misc/ConcatTest.java b/src/test/java/org/takes/misc/ConcatTest.java index 850ad350e..6efbe8200 100644 --- a/src/test/java/org/takes/misc/ConcatTest.java +++ b/src/test/java/org/takes/misc/ConcatTest.java @@ -32,9 +32,9 @@ /** * Test case for {@link Concat}. * - * @author Yegor Bugayenko (yegor@teamed.io) + * @author Jason Wong (super132j@yahoo.com) * @version $Id$ - * @since 0.32.1 + * @since 0.13.8 */ public final class ConcatTest { diff --git a/src/test/java/org/takes/misc/IterableTransformTest.java b/src/test/java/org/takes/misc/IterableTransformTest.java index c514ec345..9b3ae0af1 100644 --- a/src/test/java/org/takes/misc/IterableTransformTest.java +++ b/src/test/java/org/takes/misc/IterableTransformTest.java @@ -32,9 +32,9 @@ /** * Test case for {@link IterableTransform}. * - * @author Yegor Bugayenko (yegor@teamed.io) + * @author Jason Wong (super132j@yahoo.com) * @version $Id$ - * @since 0.32.1 + * @since 0.13.8 */ public final class IterableTransformTest { From b6661158a4aff01161e1c147084146a7522c86a1 Mon Sep 17 00:00:00 2001 From: super132 Date: Sat, 18 Apr 2015 23:14:07 +0800 Subject: [PATCH 13/49] Code changes to address code review comments --- src/main/java/org/takes/misc/Concat.java | 10 ---- .../misc/{LcCondition.java => Condition.java} | 55 +++++++++++++------ ...terableTransform.java => Transformer.java} | 23 +++++++- .../java/org/takes/rq/RqWithoutHeader.java | 4 +- src/main/java/org/takes/rs/RsWithHeaders.java | 15 +---- src/main/java/org/takes/rs/RsWithStatus.java | 3 +- .../java/org/takes/rs/RsWithoutHeader.java | 4 +- src/test/java/org/takes/misc/ConcatTest.java | 2 +- .../org/takes/misc/IterableTransformTest.java | 6 +- 9 files changed, 71 insertions(+), 51 deletions(-) rename src/main/java/org/takes/misc/{LcCondition.java => Condition.java} (51%) rename src/main/java/org/takes/misc/{IterableTransform.java => Transformer.java} (80%) diff --git a/src/main/java/org/takes/misc/Concat.java b/src/main/java/org/takes/misc/Concat.java index 4facca92e..3524b41b5 100644 --- a/src/main/java/org/takes/misc/Concat.java +++ b/src/main/java/org/takes/misc/Concat.java @@ -100,14 +100,4 @@ private void concat(final Iterable itb) { } } - public interface Condition { - /** - * Determine if an element should be added. - * - * @param element The element in the iterables to examine. - * @return True to add the element, false to skip. - */ - boolean add(T element); - } - } diff --git a/src/main/java/org/takes/misc/LcCondition.java b/src/main/java/org/takes/misc/Condition.java similarity index 51% rename from src/main/java/org/takes/misc/LcCondition.java rename to src/main/java/org/takes/misc/Condition.java index 782a3a023..2360e418c 100644 --- a/src/main/java/org/takes/misc/LcCondition.java +++ b/src/main/java/org/takes/misc/Condition.java @@ -24,34 +24,55 @@ package org.takes.misc; import java.util.Locale; -import org.takes.misc.Concat.Condition; /** - * Concat lower case string condition. + * Condition to determine how {@link Concat} behave when joining two iterables. * - * @author Jason Wong (yegor@teamed.io) + * @author Jason Wong (super132j@yahoo.com) * @version $Id$ * @since 0.13.8 */ -public final class LcCondition implements Condition { - +public interface Condition { + /** - * Prefix. + * Determine if an element should be added. + * + * @param element The element in the iterables to examine. + * @return True to add the element, false to skip. */ - private final transient String prefix; - + boolean add(T element); + /** - * Ctor. - * @param str The prefix to check + * Concat lower case string condition. This condition changes all + * characters to lower case and determine if the iterable should be + * concatenated by determine if the element contain the 'prefix' + * supplied. + * + * @author Jason Wong (super132j@yahoo.com) + * @version $Id$ + * @since 0.13.8 + * */ - public LcCondition(final String str) { - this.prefix = str; - } + static final class LowerCase implements Condition { + + /** + * Prefix. + */ + private final transient String prefix; + + /** + * Ctor. + * @param str The prefix to check + */ + public LowerCase(final String str) { + this.prefix = str; + } - @Override - public boolean add(final String element) { - return !element.toLowerCase(Locale.ENGLISH) - .startsWith(this.prefix); + @Override + public boolean add(final String element) { + return !element.toLowerCase(Locale.ENGLISH) + .startsWith(this.prefix); + } } } diff --git a/src/main/java/org/takes/misc/IterableTransform.java b/src/main/java/org/takes/misc/Transformer.java similarity index 80% rename from src/main/java/org/takes/misc/IterableTransform.java rename to src/main/java/org/takes/misc/Transformer.java index a38b09e7c..8f4ad5472 100644 --- a/src/main/java/org/takes/misc/IterableTransform.java +++ b/src/main/java/org/takes/misc/Transformer.java @@ -34,7 +34,7 @@ * @version $Id$ * @since 0.13.8 */ -public class IterableTransform implements Iterable { +public class Transformer implements Iterable { /** * Internal storage. @@ -47,8 +47,8 @@ public class IterableTransform implements Iterable { * @param list Iterable to be transformed * @param action The actual transformation implementation */ - public IterableTransform(final Iterable list, - final IterableTransform.Action action) { + public Transformer(final Iterable list, + final Transformer.Action action) { final Iterator itr = list.iterator(); while (itr.hasNext()) { this.storage.add(action.transform(itr.next())); @@ -70,4 +70,21 @@ public interface Action { T transform(T element); } + /** + * Trimming action used with {@link Transformer} + * + * @author Jason Wong (super132j@yahoo.com) + * @version $Id$ + * @since 0.13.8 + * + */ + public static final class Trim implements + Transformer.Action { + + @Override + public String transform(final String element) { + return element.trim(); + } + +} } diff --git a/src/main/java/org/takes/rq/RqWithoutHeader.java b/src/main/java/org/takes/rq/RqWithoutHeader.java index 471a620c7..a3129dca1 100644 --- a/src/main/java/org/takes/rq/RqWithoutHeader.java +++ b/src/main/java/org/takes/rq/RqWithoutHeader.java @@ -30,7 +30,7 @@ import lombok.EqualsAndHashCode; import org.takes.Request; import org.takes.misc.Concat; -import org.takes.misc.LcCondition; +import org.takes.misc.Condition; /** * Request without a header (even if it was absent). @@ -61,7 +61,7 @@ public Iterable head() throws IOException { return new Concat( req.head(), Collections.EMPTY_LIST, - new LcCondition(prefix) + new Condition.LowerCase(prefix) ); } @Override diff --git a/src/main/java/org/takes/rs/RsWithHeaders.java b/src/main/java/org/takes/rs/RsWithHeaders.java index 6047a272f..372cf6959 100644 --- a/src/main/java/org/takes/rs/RsWithHeaders.java +++ b/src/main/java/org/takes/rs/RsWithHeaders.java @@ -29,7 +29,7 @@ import lombok.EqualsAndHashCode; import org.takes.Response; import org.takes.misc.Concat; -import org.takes.misc.IterableTransform; +import org.takes.misc.Transformer; /** * Response decorator, with an additional headers. @@ -64,9 +64,9 @@ public RsWithHeaders(final Response res, final Iterable headers) { public Iterable head() throws IOException { return new Concat( res.head(), - new IterableTransform( + new Transformer( headers, - new Action() + new Transformer.Trim() ) ); } @@ -78,13 +78,4 @@ public InputStream body() throws IOException { ); } - private static final class Action implements - IterableTransform.Action { - - @Override - public String transform(final String element) { - return element.trim(); - } - - } } diff --git a/src/main/java/org/takes/rs/RsWithStatus.java b/src/main/java/org/takes/rs/RsWithStatus.java index 9189ae181..f006cb41b 100644 --- a/src/main/java/org/takes/rs/RsWithStatus.java +++ b/src/main/java/org/takes/rs/RsWithStatus.java @@ -33,6 +33,7 @@ import lombok.EqualsAndHashCode; import org.takes.Response; import org.takes.misc.Concat; +import org.takes.misc.Condition; /** * Response decorator, with status code. @@ -118,7 +119,7 @@ private static Iterable head(final Response origin, new Concat( origin.head(), Collections.EMPTY_LIST, - new Concat.Condition() { + new Condition() { /** * Boolean to determine first. */ diff --git a/src/main/java/org/takes/rs/RsWithoutHeader.java b/src/main/java/org/takes/rs/RsWithoutHeader.java index c54a0c604..029908731 100644 --- a/src/main/java/org/takes/rs/RsWithoutHeader.java +++ b/src/main/java/org/takes/rs/RsWithoutHeader.java @@ -30,7 +30,7 @@ import lombok.EqualsAndHashCode; import org.takes.Response; import org.takes.misc.Concat; -import org.takes.misc.LcCondition; +import org.takes.misc.Condition; /** * Response decorator, without a header. @@ -61,7 +61,7 @@ public Iterable head() throws IOException { return new Concat( res.head(), Collections.EMPTY_LIST, - new LcCondition(prefix) + new Condition.LowerCase(prefix) ); } @Override diff --git a/src/test/java/org/takes/misc/ConcatTest.java b/src/test/java/org/takes/misc/ConcatTest.java index 6efbe8200..119b566af 100644 --- a/src/test/java/org/takes/misc/ConcatTest.java +++ b/src/test/java/org/takes/misc/ConcatTest.java @@ -102,7 +102,7 @@ public void concatWithCondition() { final Iterable result = new Concat( alist, blist, - new Concat.Condition() { + new Condition() { @Override public boolean add(final String element) { return element.endsWith("1"); diff --git a/src/test/java/org/takes/misc/IterableTransformTest.java b/src/test/java/org/takes/misc/IterableTransformTest.java index 9b3ae0af1..ed734e679 100644 --- a/src/test/java/org/takes/misc/IterableTransformTest.java +++ b/src/test/java/org/takes/misc/IterableTransformTest.java @@ -30,7 +30,7 @@ import org.junit.Test; /** - * Test case for {@link IterableTransform}. + * Test case for {@link Transformer}. * * @author Jason Wong (super132j@yahoo.com) * @version $Id$ @@ -48,9 +48,9 @@ public void iterableTransform() { alist.add("b1"); alist.add("c1"); MatcherAssert.assertThat( - new IterableTransform( + new Transformer( alist, - new IterableTransform.Action() { + new Transformer.Action() { @Override public String transform(final String element) { return element.concat("t"); From 8ec1dd2185cd219eca3b0ceb886ff3eb7d40e08e Mon Sep 17 00:00:00 2001 From: super132 Date: Sat, 18 Apr 2015 23:49:56 +0800 Subject: [PATCH 14/49] Fixing code style issues --- src/main/java/org/takes/misc/Condition.java | 10 +++++----- src/main/java/org/takes/misc/Transformer.java | 14 ++++++-------- src/main/java/org/takes/rs/RsWithHeaders.java | 7 ++----- 3 files changed, 13 insertions(+), 18 deletions(-) diff --git a/src/main/java/org/takes/misc/Condition.java b/src/main/java/org/takes/misc/Condition.java index 2360e418c..309c9fe8a 100644 --- a/src/main/java/org/takes/misc/Condition.java +++ b/src/main/java/org/takes/misc/Condition.java @@ -33,7 +33,7 @@ * @since 0.13.8 */ public interface Condition { - + /** * Determine if an element should be added. * @@ -41,12 +41,12 @@ public interface Condition { * @return True to add the element, false to skip. */ boolean add(T element); - + /** - * Concat lower case string condition. This condition changes all + * Concat lower case string condition. This condition changes all * characters to lower case and determine if the iterable should be * concatenated by determine if the element contain the 'prefix' - * supplied. + * supplied. * * @author Jason Wong (super132j@yahoo.com) * @version $Id$ @@ -54,7 +54,7 @@ public interface Condition { * */ static final class LowerCase implements Condition { - + /** * Prefix. */ diff --git a/src/main/java/org/takes/misc/Transformer.java b/src/main/java/org/takes/misc/Transformer.java index a24922ecb..e293082b6 100644 --- a/src/main/java/org/takes/misc/Transformer.java +++ b/src/main/java/org/takes/misc/Transformer.java @@ -71,37 +71,35 @@ public interface Action { } /** - * Trimming action used with {@link Transformer} + * Trimming action used with {@link Transformer}. * * @author Jason Wong (super132j@yahoo.com) * @version $Id$ * @since 0.13.8 - * */ public static final class Trim implements Transformer.Action { - + @Override public String transform(final String element) { return element.trim(); } } - + /** - * Convert CharSequence into String + * Convert CharSequence into String. * * @author Jason Wong (super132j@yahoo.com) * @version $Id$ * @since 0.13.8 - * */ public static final class ToString implements Transformer.Action { @Override - public String transform(CharSequence element) { + public String transform(final CharSequence element) { return element.toString(); } - + } } diff --git a/src/main/java/org/takes/rs/RsWithHeaders.java b/src/main/java/org/takes/rs/RsWithHeaders.java index 784e62f81..1b821d973 100644 --- a/src/main/java/org/takes/rs/RsWithHeaders.java +++ b/src/main/java/org/takes/rs/RsWithHeaders.java @@ -26,13 +26,10 @@ import java.io.IOException; import java.io.InputStream; import java.util.Arrays; - import lombok.EqualsAndHashCode; - import org.takes.Response; import org.takes.misc.Concat; import org.takes.misc.Transformer; -import org.takes.misc.Transformer.Trim; /** * Response decorator, with an additional headers. @@ -71,11 +68,11 @@ public Iterable head() throws IOException { res.head(), new Transformer( new Transformer( - (Iterable)headers, + (Iterable) headers, new Transformer.ToString() ) , new Transformer.Trim() - ) + ) ); } @Override From ad0dc65b69ef451418974531b244f5077ffa2736 Mon Sep 17 00:00:00 2001 From: super132 Date: Sun, 19 Apr 2015 00:05:46 +0800 Subject: [PATCH 15/49] Minor fixes to code style --- src/main/java/org/takes/misc/Transformer.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/takes/misc/Transformer.java b/src/main/java/org/takes/misc/Transformer.java index e293082b6..aad67fdb4 100644 --- a/src/main/java/org/takes/misc/Transformer.java +++ b/src/main/java/org/takes/misc/Transformer.java @@ -88,7 +88,7 @@ public String transform(final String element) { /** * Convert CharSequence into String. - * + * * @author Jason Wong (super132j@yahoo.com) * @version $Id$ * @since 0.13.8 From b416e3a9ba9dd32c019142a83a4bd22cbdbe3b17 Mon Sep 17 00:00:00 2001 From: super132 Date: Sun, 19 Apr 2015 00:24:34 +0800 Subject: [PATCH 16/49] Code static analysis fix --- src/main/java/org/takes/misc/Condition.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/takes/misc/Condition.java b/src/main/java/org/takes/misc/Condition.java index 309c9fe8a..696315a64 100644 --- a/src/main/java/org/takes/misc/Condition.java +++ b/src/main/java/org/takes/misc/Condition.java @@ -53,7 +53,7 @@ public interface Condition { * @since 0.13.8 * */ - static final class LowerCase implements Condition { + static class LowerCase implements Condition { /** * Prefix. From 3d1abbc0c771f5f9f7e87967e50a1ae2d428816a Mon Sep 17 00:00:00 2001 From: super132 Date: Sun, 19 Apr 2015 00:27:43 +0800 Subject: [PATCH 17/49] Minor fix. --- src/main/java/org/takes/misc/Condition.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/takes/misc/Condition.java b/src/main/java/org/takes/misc/Condition.java index 696315a64..d2805a245 100644 --- a/src/main/java/org/takes/misc/Condition.java +++ b/src/main/java/org/takes/misc/Condition.java @@ -53,7 +53,7 @@ public interface Condition { * @since 0.13.8 * */ - static class LowerCase implements Condition { + class LowerCase implements Condition { /** * Prefix. From 97e42d2ea2f8e564085e7628fd497e988040dc7d Mon Sep 17 00:00:00 2001 From: super132 Date: Sun, 19 Apr 2015 17:49:49 +0800 Subject: [PATCH 18/49] Addressing code review comments --- src/main/java/org/takes/misc/Condition.java | 5 -- .../java/org/takes/misc/TransformAction.java | 66 +++++++++++++++++++ src/main/java/org/takes/misc/Transformer.java | 44 +------------ src/main/java/org/takes/rs/RsWithHeaders.java | 5 +- .../org/takes/misc/IterableTransformTest.java | 2 +- 5 files changed, 71 insertions(+), 51 deletions(-) create mode 100644 src/main/java/org/takes/misc/TransformAction.java diff --git a/src/main/java/org/takes/misc/Condition.java b/src/main/java/org/takes/misc/Condition.java index d2805a245..1dff7f51b 100644 --- a/src/main/java/org/takes/misc/Condition.java +++ b/src/main/java/org/takes/misc/Condition.java @@ -47,11 +47,6 @@ public interface Condition { * characters to lower case and determine if the iterable should be * concatenated by determine if the element contain the 'prefix' * supplied. - * - * @author Jason Wong (super132j@yahoo.com) - * @version $Id$ - * @since 0.13.8 - * */ class LowerCase implements Condition { diff --git a/src/main/java/org/takes/misc/TransformAction.java b/src/main/java/org/takes/misc/TransformAction.java new file mode 100644 index 000000000..398086cb9 --- /dev/null +++ b/src/main/java/org/takes/misc/TransformAction.java @@ -0,0 +1,66 @@ +/** + * The MIT License (MIT) + * + * Copyright (c) 2015 Yegor Bugayenko + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package org.takes.misc; + +/** + * Action for {@link Transformer} to perform actual transformation. + * + * @author Jason Wong (super132j@yahoo.com) + * @version $Id$ + * @since 0.13.8 + */ +public interface TransformAction { + /** + * The transform action of the element of type T to K. + * + * @param element Element of the iterable + * @return Transformed element + */ + K transform(T element); + + /** + * Trimming action used with {@link Transformer}. + */ + class Trim implements + TransformAction { + + @Override + public String transform(final String element) { + return element.trim(); + } + } + + /** + * Convert CharSequence into String. + */ + class ToString implements + TransformAction { + + @Override + public String transform(final CharSequence element) { + return element.toString(); + } + + } +} diff --git a/src/main/java/org/takes/misc/Transformer.java b/src/main/java/org/takes/misc/Transformer.java index aad67fdb4..52a8425b0 100644 --- a/src/main/java/org/takes/misc/Transformer.java +++ b/src/main/java/org/takes/misc/Transformer.java @@ -48,7 +48,7 @@ public class Transformer implements Iterable { * @param action The actual transformation implementation */ public Transformer(final Iterable list, - final Transformer.Action action) { + final TransformAction action) { final Iterator itr = list.iterator(); while (itr.hasNext()) { this.storage.add(action.transform(itr.next())); @@ -60,46 +60,4 @@ public final Iterator iterator() { return this.storage.iterator(); } - public interface Action { - /** - * The transform action of the element of type T to K. - * - * @param element Element of the iterable - * @return Transformed element - */ - K transform(T element); - } - - /** - * Trimming action used with {@link Transformer}. - * - * @author Jason Wong (super132j@yahoo.com) - * @version $Id$ - * @since 0.13.8 - */ - public static final class Trim implements - Transformer.Action { - - @Override - public String transform(final String element) { - return element.trim(); - } - } - - /** - * Convert CharSequence into String. - * - * @author Jason Wong (super132j@yahoo.com) - * @version $Id$ - * @since 0.13.8 - */ - public static final class ToString implements - Transformer.Action { - - @Override - public String transform(final CharSequence element) { - return element.toString(); - } - - } } diff --git a/src/main/java/org/takes/rs/RsWithHeaders.java b/src/main/java/org/takes/rs/RsWithHeaders.java index 1b821d973..77f71e1bf 100644 --- a/src/main/java/org/takes/rs/RsWithHeaders.java +++ b/src/main/java/org/takes/rs/RsWithHeaders.java @@ -29,6 +29,7 @@ import lombok.EqualsAndHashCode; import org.takes.Response; import org.takes.misc.Concat; +import org.takes.misc.TransformAction; import org.takes.misc.Transformer; /** @@ -69,9 +70,9 @@ public Iterable head() throws IOException { new Transformer( new Transformer( (Iterable) headers, - new Transformer.ToString() + new TransformAction.ToString() ) - , new Transformer.Trim() + , new TransformAction.Trim() ) ); } diff --git a/src/test/java/org/takes/misc/IterableTransformTest.java b/src/test/java/org/takes/misc/IterableTransformTest.java index 7ee0e8806..9fa3cfe85 100644 --- a/src/test/java/org/takes/misc/IterableTransformTest.java +++ b/src/test/java/org/takes/misc/IterableTransformTest.java @@ -50,7 +50,7 @@ public void iterableTransform() { MatcherAssert.assertThat( new Transformer( alist, - new Transformer.Action() { + new TransformAction() { @Override public String transform(final String element) { return element.concat("t"); From cf1b7c18e6b302dcc8cb3cd184415d69ae87ea0d Mon Sep 17 00:00:00 2001 From: super132 Date: Sun, 19 Apr 2015 23:27:43 +0800 Subject: [PATCH 19/49] Code styling changes as per advised in comments --- src/main/java/org/takes/misc/Concat.java | 4 ---- src/main/java/org/takes/misc/Condition.java | 1 - src/main/java/org/takes/misc/TransformAction.java | 7 ++----- src/main/java/org/takes/misc/Transformer.java | 1 - src/main/java/org/takes/rs/RsWithHeaders.java | 4 ++-- 5 files changed, 4 insertions(+), 13 deletions(-) diff --git a/src/main/java/org/takes/misc/Concat.java b/src/main/java/org/takes/misc/Concat.java index 3524b41b5..433898c77 100644 --- a/src/main/java/org/takes/misc/Concat.java +++ b/src/main/java/org/takes/misc/Concat.java @@ -44,7 +44,6 @@ public final class Concat implements Iterable { /** * To produce an iterable collection combining a and b, with order of the * elements in a first. - * * @param aitb First iterable to concat * @param bitb Second iterable to conat */ @@ -56,7 +55,6 @@ public Concat(final Iterable aitb, final Iterable bitb) { /** * To produce an iterable collection, determined by condition, combining a * and b, with order of the elements in a first. - * * @param aitb First iterable to concat * @param bitb Second iterable to conat * @param cond To determine which element to add in the final iterable @@ -74,7 +72,6 @@ public Iterator iterator() { /** * Adding an iterable into storage with condition. - * * @param itb Iterable to add * @param cond Condition to determine the element should be added */ @@ -90,7 +87,6 @@ private void concat(final Iterable itb, final Condition cond) { /** * Adding an iterable into storage. - * * @param itb Iterable to add */ private void concat(final Iterable itb) { diff --git a/src/main/java/org/takes/misc/Condition.java b/src/main/java/org/takes/misc/Condition.java index 1dff7f51b..afef95f59 100644 --- a/src/main/java/org/takes/misc/Condition.java +++ b/src/main/java/org/takes/misc/Condition.java @@ -36,7 +36,6 @@ public interface Condition { /** * Determine if an element should be added. - * * @param element The element in the iterables to examine. * @return True to add the element, false to skip. */ diff --git a/src/main/java/org/takes/misc/TransformAction.java b/src/main/java/org/takes/misc/TransformAction.java index 398086cb9..7421a7725 100644 --- a/src/main/java/org/takes/misc/TransformAction.java +++ b/src/main/java/org/takes/misc/TransformAction.java @@ -33,7 +33,6 @@ public interface TransformAction { /** * The transform action of the element of type T to K. - * * @param element Element of the iterable * @return Transformed element */ @@ -42,8 +41,7 @@ public interface TransformAction { /** * Trimming action used with {@link Transformer}. */ - class Trim implements - TransformAction { + class Trim implements TransformAction { @Override public String transform(final String element) { @@ -54,8 +52,7 @@ public String transform(final String element) { /** * Convert CharSequence into String. */ - class ToString implements - TransformAction { + class ToString implements TransformAction { @Override public String transform(final CharSequence element) { diff --git a/src/main/java/org/takes/misc/Transformer.java b/src/main/java/org/takes/misc/Transformer.java index 52a8425b0..e3ff29f44 100644 --- a/src/main/java/org/takes/misc/Transformer.java +++ b/src/main/java/org/takes/misc/Transformer.java @@ -43,7 +43,6 @@ public class Transformer implements Iterable { /** * Transform elements in the supplied iterable by the action supplied. - * * @param list Iterable to be transformed * @param action The actual transformation implementation */ diff --git a/src/main/java/org/takes/rs/RsWithHeaders.java b/src/main/java/org/takes/rs/RsWithHeaders.java index 77f71e1bf..92a3c12e1 100644 --- a/src/main/java/org/takes/rs/RsWithHeaders.java +++ b/src/main/java/org/takes/rs/RsWithHeaders.java @@ -71,8 +71,8 @@ public Iterable head() throws IOException { new Transformer( (Iterable) headers, new TransformAction.ToString() - ) - , new TransformAction.Trim() + ), + new TransformAction.Trim() ) ); } From 4bca084c14c933f42627b9ca4ba611551d348cae Mon Sep 17 00:00:00 2001 From: super132 Date: Mon, 20 Apr 2015 08:20:26 +0800 Subject: [PATCH 20/49] Using Collection.emptyList() to avoid unchecked casting. --- src/main/java/org/takes/rq/RqWithoutHeader.java | 3 +-- src/main/java/org/takes/rs/RsWithStatus.java | 3 +-- src/main/java/org/takes/rs/RsWithoutHeader.java | 3 +-- 3 files changed, 3 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/takes/rq/RqWithoutHeader.java b/src/main/java/org/takes/rq/RqWithoutHeader.java index a3129dca1..dd77ed457 100644 --- a/src/main/java/org/takes/rq/RqWithoutHeader.java +++ b/src/main/java/org/takes/rq/RqWithoutHeader.java @@ -53,14 +53,13 @@ public RqWithoutHeader(final Request req, final CharSequence name) { super( new Request() { @Override - @SuppressWarnings("unchecked") public Iterable head() throws IOException { final String prefix = String.format( "%s:", name.toString().toLowerCase(Locale.ENGLISH) ); return new Concat( req.head(), - Collections.EMPTY_LIST, + Collections.emptyList(), new Condition.LowerCase(prefix) ); } diff --git a/src/main/java/org/takes/rs/RsWithStatus.java b/src/main/java/org/takes/rs/RsWithStatus.java index 5bdd77d2e..3a858c7aa 100644 --- a/src/main/java/org/takes/rs/RsWithStatus.java +++ b/src/main/java/org/takes/rs/RsWithStatus.java @@ -100,7 +100,6 @@ public InputStream body() throws IOException { * @return Head * @throws IOException If fails */ - @SuppressWarnings("unchecked") private static Iterable head(final Response origin, final int status, final CharSequence reason) throws IOException { // @checkstyle MagicNumber (1 line) @@ -119,7 +118,7 @@ private static Iterable head(final Response origin, ), new Concat( origin.head(), - Collections.EMPTY_LIST, + Collections.emptyList(), new Condition() { /** * Boolean to determine first. diff --git a/src/main/java/org/takes/rs/RsWithoutHeader.java b/src/main/java/org/takes/rs/RsWithoutHeader.java index e162f0ad8..fafc0d1df 100644 --- a/src/main/java/org/takes/rs/RsWithoutHeader.java +++ b/src/main/java/org/takes/rs/RsWithoutHeader.java @@ -53,14 +53,13 @@ public RsWithoutHeader(final Response res, final CharSequence name) { super( new Response() { @Override - @SuppressWarnings("unchecked") public Iterable head() throws IOException { final String prefix = String.format( "%s:", name.toString().toLowerCase(Locale.ENGLISH) ); return new Concat( res.head(), - Collections.EMPTY_LIST, + Collections.emptyList(), new Condition.LowerCase(prefix) ); } From a2b21e66e43359c4f6a003f4ddff4184f2d97477 Mon Sep 17 00:00:00 2001 From: Jason Wong Date: Mon, 20 Apr 2015 14:32:25 +0800 Subject: [PATCH 21/49] Select class is created for separating filtering and concatenation logic from Concat. --- src/main/java/org/takes/misc/Concat.java | 28 ------- src/main/java/org/takes/misc/Select.java | 75 +++++++++++++++++++ .../misc/{Transformer.java => Transform.java} | 4 +- .../java/org/takes/misc/TransformAction.java | 4 +- .../java/org/takes/rq/RqWithoutHeader.java | 8 +- src/main/java/org/takes/rs/RsWithHeaders.java | 6 +- src/main/java/org/takes/rs/RsWithStatus.java | 30 ++++---- .../java/org/takes/rs/RsWithoutHeader.java | 6 +- src/test/java/org/takes/misc/ConcatTest.java | 32 -------- .../org/takes/misc/IterableTransformTest.java | 4 +- src/test/java/org/takes/misc/SelectTest.java | 67 +++++++++++++++++ 11 files changed, 171 insertions(+), 93 deletions(-) create mode 100644 src/main/java/org/takes/misc/Select.java rename src/main/java/org/takes/misc/{Transformer.java => Transform.java} (95%) create mode 100644 src/test/java/org/takes/misc/SelectTest.java diff --git a/src/main/java/org/takes/misc/Concat.java b/src/main/java/org/takes/misc/Concat.java index 433898c77..ce5ab847b 100644 --- a/src/main/java/org/takes/misc/Concat.java +++ b/src/main/java/org/takes/misc/Concat.java @@ -52,39 +52,11 @@ public Concat(final Iterable aitb, final Iterable bitb) { this.concat(bitb); } - /** - * To produce an iterable collection, determined by condition, combining a - * and b, with order of the elements in a first. - * @param aitb First iterable to concat - * @param bitb Second iterable to conat - * @param cond To determine which element to add in the final iterable - */ - public Concat(final Iterable aitb, final Iterable bitb, - final Condition cond) { - this.concat(aitb, cond); - this.concat(bitb, cond); - } - @Override public Iterator iterator() { return this.storage.iterator(); } - /** - * Adding an iterable into storage with condition. - * @param itb Iterable to add - * @param cond Condition to determine the element should be added - */ - private void concat(final Iterable itb, final Condition cond) { - final Iterator itr = itb.iterator(); - while (itr.hasNext()) { - final T element = itr.next(); - if (cond.add(element)) { - this.storage.add(element); - } - } - } - /** * Adding an iterable into storage. * @param itb Iterable to add diff --git a/src/main/java/org/takes/misc/Select.java b/src/main/java/org/takes/misc/Select.java new file mode 100644 index 000000000..74ca2816a --- /dev/null +++ b/src/main/java/org/takes/misc/Select.java @@ -0,0 +1,75 @@ +/** + * The MIT License (MIT) + * + * Copyright (c) 2015 Yegor Bugayenko + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package org.takes.misc; + +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; + +/** + * Select elements into a new iterable with given condition. + * + * @author Jason Wong (super132j@yahoo.com) + * @version $Id$ + * @since 0.13.8 + */ +public class Select implements Iterable { + + /** + * Internal storage to hold the elements from iterables. + */ + private final transient List storage = new LinkedList(); + + /** + * To produce an iterable collection, determined by condition, combining a + * and b, with order of the elements in a first. + * @param aitb First iterable to concat + * @param bitb Second iterable to conat + * @param cond To determine which element to add in the final iterable + */ + public Select(final Iterable itb, final Condition cond) { + this.select(itb, cond); + } + + @Override + public Iterator iterator() { + return this.storage.iterator(); + } + + /** + * Adding an iterable into storage with condition. + * @param itb Iterable to add + * @param cond Condition to determine the element should be added + */ + private void select(final Iterable itb, final Condition cond) { + final Iterator itr = itb.iterator(); + while (itr.hasNext()) { + final T element = itr.next(); + if (cond.add(element)) { + this.storage.add(element); + } + } + } + +} diff --git a/src/main/java/org/takes/misc/Transformer.java b/src/main/java/org/takes/misc/Transform.java similarity index 95% rename from src/main/java/org/takes/misc/Transformer.java rename to src/main/java/org/takes/misc/Transform.java index e3ff29f44..54545ec51 100644 --- a/src/main/java/org/takes/misc/Transformer.java +++ b/src/main/java/org/takes/misc/Transform.java @@ -34,7 +34,7 @@ * @version $Id$ * @since 0.13.8 */ -public class Transformer implements Iterable { +public class Transform implements Iterable { /** * Internal storage. @@ -46,7 +46,7 @@ public class Transformer implements Iterable { * @param list Iterable to be transformed * @param action The actual transformation implementation */ - public Transformer(final Iterable list, + public Transform(final Iterable list, final TransformAction action) { final Iterator itr = list.iterator(); while (itr.hasNext()) { diff --git a/src/main/java/org/takes/misc/TransformAction.java b/src/main/java/org/takes/misc/TransformAction.java index 7421a7725..1755762b5 100644 --- a/src/main/java/org/takes/misc/TransformAction.java +++ b/src/main/java/org/takes/misc/TransformAction.java @@ -24,7 +24,7 @@ package org.takes.misc; /** - * Action for {@link Transformer} to perform actual transformation. + * Action for {@link Transform} to perform actual transformation. * * @author Jason Wong (super132j@yahoo.com) * @version $Id$ @@ -39,7 +39,7 @@ public interface TransformAction { K transform(T element); /** - * Trimming action used with {@link Transformer}. + * Trimming action used with {@link Transform}. */ class Trim implements TransformAction { diff --git a/src/main/java/org/takes/rq/RqWithoutHeader.java b/src/main/java/org/takes/rq/RqWithoutHeader.java index dd77ed457..0bd7ea786 100644 --- a/src/main/java/org/takes/rq/RqWithoutHeader.java +++ b/src/main/java/org/takes/rq/RqWithoutHeader.java @@ -25,12 +25,11 @@ import java.io.IOException; import java.io.InputStream; -import java.util.Collections; import java.util.Locale; import lombok.EqualsAndHashCode; import org.takes.Request; -import org.takes.misc.Concat; import org.takes.misc.Condition; +import org.takes.misc.Select; /** * Request without a header (even if it was absent). @@ -57,9 +56,8 @@ public Iterable head() throws IOException { final String prefix = String.format( "%s:", name.toString().toLowerCase(Locale.ENGLISH) ); - return new Concat( - req.head(), - Collections.emptyList(), + return new Select( + req.head(), new Condition.LowerCase(prefix) ); } diff --git a/src/main/java/org/takes/rs/RsWithHeaders.java b/src/main/java/org/takes/rs/RsWithHeaders.java index 92a3c12e1..7b08d01d7 100644 --- a/src/main/java/org/takes/rs/RsWithHeaders.java +++ b/src/main/java/org/takes/rs/RsWithHeaders.java @@ -30,7 +30,7 @@ import org.takes.Response; import org.takes.misc.Concat; import org.takes.misc.TransformAction; -import org.takes.misc.Transformer; +import org.takes.misc.Transform; /** * Response decorator, with an additional headers. @@ -67,8 +67,8 @@ public RsWithHeaders(final Response res, public Iterable head() throws IOException { return new Concat( res.head(), - new Transformer( - new Transformer( + new Transform( + new Transform( (Iterable) headers, new TransformAction.ToString() ), diff --git a/src/main/java/org/takes/rs/RsWithStatus.java b/src/main/java/org/takes/rs/RsWithStatus.java index 3a858c7aa..8ef2ff588 100644 --- a/src/main/java/org/takes/rs/RsWithStatus.java +++ b/src/main/java/org/takes/rs/RsWithStatus.java @@ -34,6 +34,7 @@ import org.takes.Response; import org.takes.misc.Concat; import org.takes.misc.Condition; +import org.takes.misc.Select; /** * Response decorator, with status code. @@ -116,23 +117,22 @@ private static Iterable head(final Response origin, Collections.singleton( String.format("HTTP/1.1 %d %s", status, reason) ), - new Concat( - origin.head(), - Collections.emptyList(), - new Condition() { - /** - * Boolean to determine first. - */ - private boolean first = true; - @Override - public boolean add(final String element) { - final boolean ret = this.first; - if (this.first) { - this.first = false; - } - return !ret; + new Select( + origin.head(), + new Condition() { + /** + * Boolean to determine first. + */ + private boolean first = true; + @Override + public boolean add(final String element) { + final boolean ret = this.first; + if (this.first) { + this.first = false; } + return !ret; } + } ) ); } diff --git a/src/main/java/org/takes/rs/RsWithoutHeader.java b/src/main/java/org/takes/rs/RsWithoutHeader.java index fafc0d1df..da0bbaf23 100644 --- a/src/main/java/org/takes/rs/RsWithoutHeader.java +++ b/src/main/java/org/takes/rs/RsWithoutHeader.java @@ -25,11 +25,10 @@ import java.io.IOException; import java.io.InputStream; -import java.util.Collections; import java.util.Locale; import lombok.EqualsAndHashCode; import org.takes.Response; -import org.takes.misc.Concat; +import org.takes.misc.Select; import org.takes.misc.Condition; /** @@ -57,9 +56,8 @@ public Iterable head() throws IOException { final String prefix = String.format( "%s:", name.toString().toLowerCase(Locale.ENGLISH) ); - return new Concat( + return new Select( res.head(), - Collections.emptyList(), new Condition.LowerCase(prefix) ); } diff --git a/src/test/java/org/takes/misc/ConcatTest.java b/src/test/java/org/takes/misc/ConcatTest.java index 119b566af..a3893f770 100644 --- a/src/test/java/org/takes/misc/ConcatTest.java +++ b/src/test/java/org/takes/misc/ConcatTest.java @@ -84,36 +84,4 @@ public void concatWithEmpty() { ); } - /** - * Concat test with condition. - */ - @Test - public void concatWithCondition() { - final List alist = new ArrayList(2); - final String aone = "at1"; - final String atwo = "at2"; - alist.add(aone); - alist.add(atwo); - final List blist = new ArrayList(2); - final String bone = "bt1"; - final String btwo = "bt2"; - blist.add(bone); - blist.add(btwo); - final Iterable result = new Concat( - alist, - blist, - new Condition() { - @Override - public boolean add(final String element) { - return element.endsWith("1"); - } - } - ); - MatcherAssert.assertThat(result, Matchers.hasItems(aone, bone)); - MatcherAssert.assertThat( - result, - Matchers.not(Matchers.hasItems(atwo, btwo)) - ); - } - } diff --git a/src/test/java/org/takes/misc/IterableTransformTest.java b/src/test/java/org/takes/misc/IterableTransformTest.java index 9fa3cfe85..7be49c9ed 100644 --- a/src/test/java/org/takes/misc/IterableTransformTest.java +++ b/src/test/java/org/takes/misc/IterableTransformTest.java @@ -30,7 +30,7 @@ import org.junit.Test; /** - * Test case for {@link Transformer}. + * Test case for {@link Transform}. * * @author Jason Wong (super132j@yahoo.com) * @version $Id$ @@ -48,7 +48,7 @@ public void iterableTransform() { alist.add("b1"); alist.add("c1"); MatcherAssert.assertThat( - new Transformer( + new Transform( alist, new TransformAction() { @Override diff --git a/src/test/java/org/takes/misc/SelectTest.java b/src/test/java/org/takes/misc/SelectTest.java new file mode 100644 index 000000000..b5141037c --- /dev/null +++ b/src/test/java/org/takes/misc/SelectTest.java @@ -0,0 +1,67 @@ +/** + * The MIT License (MIT) + * + * Copyright (c) 2015 Yegor Bugayenko + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package org.takes.misc; + +import java.util.ArrayList; +import java.util.List; +import org.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; +import org.junit.Test; + +/** + * Test case for {@link Concat}. + * + * @author Jason Wong (super132j@yahoo.com) + * @version $Id$ + * @since 0.13.8 + */ +public class SelectTest { + + /** + * Select test with condition. + */ + @Test + public void select() { + final List alist = new ArrayList(2); + final String aone = "at1"; + final String atwo = "at2"; + alist.add(aone); + alist.add(atwo); + final Iterable result = new Select( + alist, + new Condition() { + @Override + public boolean add(final String element) { + return element.endsWith("1"); + } + } + ); + MatcherAssert.assertThat(result, Matchers.hasItems(aone)); + MatcherAssert.assertThat( + result, + Matchers.not(Matchers.hasItems(atwo)) + ); + } + +} From a2a9dcf5e7589d50e79cc15d8f3261f8ef37143e Mon Sep 17 00:00:00 2001 From: Jason Wong Date: Mon, 20 Apr 2015 14:37:32 +0800 Subject: [PATCH 22/49] Fixing code static analysis issues. --- src/main/java/org/takes/misc/Select.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/takes/misc/Select.java b/src/main/java/org/takes/misc/Select.java index 74ca2816a..1b248e4ec 100644 --- a/src/main/java/org/takes/misc/Select.java +++ b/src/main/java/org/takes/misc/Select.java @@ -34,7 +34,7 @@ * @version $Id$ * @since 0.13.8 */ -public class Select implements Iterable { +public final class Select implements Iterable { /** * Internal storage to hold the elements from iterables. @@ -44,8 +44,7 @@ public class Select implements Iterable { /** * To produce an iterable collection, determined by condition, combining a * and b, with order of the elements in a first. - * @param aitb First iterable to concat - * @param bitb Second iterable to conat + * @param itb iterable to select * @param cond To determine which element to add in the final iterable */ public Select(final Iterable itb, final Condition cond) { From a06b4885077c3cc137b77fd898e651f24120aab5 Mon Sep 17 00:00:00 2001 From: Jason Wong Date: Mon, 20 Apr 2015 18:18:24 +0800 Subject: [PATCH 23/49] Refactor according to code review comments --- src/main/java/org/takes/misc/Concat.java | 74 ++++++++++--- src/main/java/org/takes/misc/Condition.java | 4 +- src/main/java/org/takes/misc/Select.java | 100 +++++++++++++++--- src/main/java/org/takes/misc/Transform.java | 65 ++++++++++-- .../java/org/takes/rq/RqWithoutHeader.java | 2 +- src/main/java/org/takes/rs/RsWithHeaders.java | 2 +- src/main/java/org/takes/rs/RsWithStatus.java | 2 +- .../java/org/takes/rs/RsWithoutHeader.java | 2 +- src/test/java/org/takes/misc/SelectTest.java | 4 +- 9 files changed, 209 insertions(+), 46 deletions(-) diff --git a/src/main/java/org/takes/misc/Concat.java b/src/main/java/org/takes/misc/Concat.java index ce5ab847b..86e4cfff5 100644 --- a/src/main/java/org/takes/misc/Concat.java +++ b/src/main/java/org/takes/misc/Concat.java @@ -24,8 +24,7 @@ package org.takes.misc; import java.util.Iterator; -import java.util.LinkedList; -import java.util.List; +import java.util.NoSuchElementException; /** * Concat iterable. @@ -37,9 +36,14 @@ public final class Concat implements Iterable { /** - * Internal storage to hold the elements from iterables. + * Internal reference to hold the first elements from constructor. */ - private final transient List storage = new LinkedList(); + private final transient Iterable left; + + /** + * Internal reference to hold the second elements from constructor. + */ + private final transient Iterable right; /** * To produce an iterable collection combining a and b, with order of the @@ -48,24 +52,66 @@ public final class Concat implements Iterable { * @param bitb Second iterable to conat */ public Concat(final Iterable aitb, final Iterable bitb) { - this.concat(aitb); - this.concat(bitb); + this.left = aitb; + this.right = bitb; } @Override public Iterator iterator() { - return this.storage.iterator(); + return new ConcatIterator( + this.left.iterator(), + this.right.iterator() + ); } /** - * Adding an iterable into storage. - * @param itb Iterable to add + * The concat iterator to traverse the input iterables as if they are + * from one list. */ - private void concat(final Iterable itb) { - final Iterator itr = itb.iterator(); - while (itr.hasNext()) { - this.storage.add(itr.next()); + private static class ConcatIterator implements Iterator { + + /** + * Internal reference for holding the first iterator form constructor. + */ + private final transient Iterator left; + + /** + * Internal reference for holding the second iterator form constructor. + */ + private final transient Iterator right; + + /** + * Ctor. ConcatIterator traverses the element + * @param aitr + * @param bitr + */ + public ConcatIterator(final Iterator aitr, final Iterator bitr) { + this.left = aitr; + this.right = bitr; + } + + @Override + public boolean hasNext() { + final boolean left = this.left.hasNext(); + final boolean right = this.right.hasNext(); + if (left) { + return true; + } else if (right) { + return true; + } else { + return false; + } } - } + @Override + public E next() { + if (this.left.hasNext()) { + return this.left.next(); + } else if (this.right.hasNext()) { + return this.right.next(); + } else { + throw new NoSuchElementException(); + } + } + } } diff --git a/src/main/java/org/takes/misc/Condition.java b/src/main/java/org/takes/misc/Condition.java index afef95f59..947792d26 100644 --- a/src/main/java/org/takes/misc/Condition.java +++ b/src/main/java/org/takes/misc/Condition.java @@ -39,7 +39,7 @@ public interface Condition { * @param element The element in the iterables to examine. * @return True to add the element, false to skip. */ - boolean add(T element); + boolean fits(T element); /** * Concat lower case string condition. This condition changes all @@ -63,7 +63,7 @@ public LowerCase(final String str) { } @Override - public boolean add(final String element) { + public boolean fits(final String element) { return !element.toLowerCase(Locale.ENGLISH) .startsWith(this.prefix); } diff --git a/src/main/java/org/takes/misc/Select.java b/src/main/java/org/takes/misc/Select.java index 1b248e4ec..3b2a8a363 100644 --- a/src/main/java/org/takes/misc/Select.java +++ b/src/main/java/org/takes/misc/Select.java @@ -23,9 +23,10 @@ */ package org.takes.misc; +import java.util.ArrayList; import java.util.Iterator; -import java.util.LinkedList; import java.util.List; +import java.util.NoSuchElementException; /** * Select elements into a new iterable with given condition. @@ -39,34 +40,105 @@ public final class Select implements Iterable { /** * Internal storage to hold the elements from iterables. */ - private final transient List storage = new LinkedList(); + private final transient Iterable list; + + /** + * The condition to filter the element in the iterator + */ + private final transient Condition condition; /** * To produce an iterable collection, determined by condition, combining a * and b, with order of the elements in a first. - * @param itb iterable to select + * @param itb Iterable to select * @param cond To determine which element to add in the final iterable */ public Select(final Iterable itb, final Condition cond) { - this.select(itb, cond); + this.list = itb; + this.condition = cond; } @Override public Iterator iterator() { - return this.storage.iterator(); + return new SelectIterator( + this.list.iterator(), + this.condition + ); } /** - * Adding an iterable into storage with condition. - * @param itb Iterable to add - * @param cond Condition to determine the element should be added + * The select iterator to traverse the input iterables and return the + * elements from the list with given condition. */ - private void select(final Iterable itb, final Condition cond) { - final Iterator itr = itb.iterator(); - while (itr.hasNext()) { - final T element = itr.next(); - if (cond.add(element)) { - this.storage.add(element); + private static class SelectIterator implements Iterator { + + /** + * The iterator to reflect the traverse state. + */ + private final transient Iterator iterator; + + /** + * The condition to filter the elements in the iterator. + */ + private final transient Condition condition; + + /** + * The list storing the current object of the iterator. + */ + private final transient List current = new ArrayList(1); + + /** + * The index pointing to the current element list. + */ + private static final transient int HEAD = 0; + + /** + * Ctor. ConcatIterator traverses the element. + * @param itr Iterator of the original iterable + * @param cond Condition to filter out elements + */ + public SelectIterator(final Iterator itr, final Condition cond) { + this.condition = cond; + this.iterator = itr; + if (this.iterator.hasNext()) { + this.current.add(this.iterator.next()); + } + } + + @Override + public boolean hasNext() { + if (!this.current.isEmpty()) { + if (this.condition.fits(this.current.get(HEAD))) { + return true; + } else { + this.current.remove(HEAD); + while (this.iterator.hasNext()) { + final E element = this.iterator.next(); + if (this.condition.fits(element)) { + this.current.add(element); + break; + } + } + return !this.current.isEmpty(); + } + } else { + return false; + } + } + + @Override + public E next() { + if (this.hasNext()) { + final E result = this.current.get(HEAD); + this.current.remove(0); + if (this.iterator.hasNext()) { + this.current.add(this.iterator.next()); + } + return result; + } else { + throw new NoSuchElementException( + "No more element with fits the select condition." + ); } } } diff --git a/src/main/java/org/takes/misc/Transform.java b/src/main/java/org/takes/misc/Transform.java index 54545ec51..36a4404dd 100644 --- a/src/main/java/org/takes/misc/Transform.java +++ b/src/main/java/org/takes/misc/Transform.java @@ -24,8 +24,7 @@ package org.takes.misc; import java.util.Iterator; -import java.util.LinkedList; -import java.util.List; +import java.util.NoSuchElementException; /** * Transform elements in an iterable (in type T) into others (in type K). @@ -39,24 +38,70 @@ public class Transform implements Iterable { /** * Internal storage. */ - private final transient List storage = new LinkedList(); + private final transient Iterable list; + + /** + * The action to transform the elements in the iterator. + */ + private final transient TransformAction action; /** * Transform elements in the supplied iterable by the action supplied. * @param list Iterable to be transformed * @param action The actual transformation implementation */ - public Transform(final Iterable list, - final TransformAction action) { - final Iterator itr = list.iterator(); - while (itr.hasNext()) { - this.storage.add(action.transform(itr.next())); - } + public Transform(final Iterable itb, + final TransformAction act) { + this.list = itb; + this.action = act; } @Override public final Iterator iterator() { - return this.storage.iterator(); + return new TransformIterator(this.list.iterator(), this.action); } + /** + * The iterator to iterator through the original type B and return the + * transformed element in type A + */ + private static class TransformIterator implements Iterator { + + /** + * The iterator to reflect the traverse state. + */ + private final transient Iterator iterator; + + /** + * The action to transform the elements in the iterator. + */ + private final transient TransformAction action; + + /** + * Ctor. ConcatIterator traverses the element. + * @param itr Iterator of the original iterable + * @param act Action to transform elements + */ + public TransformIterator(final Iterator itr, + final TransformAction act) { + this.action = act; + this.iterator = itr; + } + + @Override + public boolean hasNext() { + return this.iterator.hasNext(); + } + + @Override + public A next() { + if (this.hasNext()) { + return this.action.transform(this.iterator.next()); + } else { + throw new NoSuchElementException( + "No more element with fits the select condition." + ); + } + } + } } diff --git a/src/main/java/org/takes/rq/RqWithoutHeader.java b/src/main/java/org/takes/rq/RqWithoutHeader.java index 0bd7ea786..32b6a18f2 100644 --- a/src/main/java/org/takes/rq/RqWithoutHeader.java +++ b/src/main/java/org/takes/rq/RqWithoutHeader.java @@ -57,7 +57,7 @@ public Iterable head() throws IOException { "%s:", name.toString().toLowerCase(Locale.ENGLISH) ); return new Select( - req.head(), + req.head(), new Condition.LowerCase(prefix) ); } diff --git a/src/main/java/org/takes/rs/RsWithHeaders.java b/src/main/java/org/takes/rs/RsWithHeaders.java index 7b08d01d7..a1a95a019 100644 --- a/src/main/java/org/takes/rs/RsWithHeaders.java +++ b/src/main/java/org/takes/rs/RsWithHeaders.java @@ -29,8 +29,8 @@ import lombok.EqualsAndHashCode; import org.takes.Response; import org.takes.misc.Concat; -import org.takes.misc.TransformAction; import org.takes.misc.Transform; +import org.takes.misc.TransformAction; /** * Response decorator, with an additional headers. diff --git a/src/main/java/org/takes/rs/RsWithStatus.java b/src/main/java/org/takes/rs/RsWithStatus.java index 8ef2ff588..533053583 100644 --- a/src/main/java/org/takes/rs/RsWithStatus.java +++ b/src/main/java/org/takes/rs/RsWithStatus.java @@ -125,7 +125,7 @@ private static Iterable head(final Response origin, */ private boolean first = true; @Override - public boolean add(final String element) { + public boolean fits(final String element) { final boolean ret = this.first; if (this.first) { this.first = false; diff --git a/src/main/java/org/takes/rs/RsWithoutHeader.java b/src/main/java/org/takes/rs/RsWithoutHeader.java index da0bbaf23..b47289ce6 100644 --- a/src/main/java/org/takes/rs/RsWithoutHeader.java +++ b/src/main/java/org/takes/rs/RsWithoutHeader.java @@ -28,8 +28,8 @@ import java.util.Locale; import lombok.EqualsAndHashCode; import org.takes.Response; -import org.takes.misc.Select; import org.takes.misc.Condition; +import org.takes.misc.Select; /** * Response decorator, without a header. diff --git a/src/test/java/org/takes/misc/SelectTest.java b/src/test/java/org/takes/misc/SelectTest.java index b5141037c..fa32623b8 100644 --- a/src/test/java/org/takes/misc/SelectTest.java +++ b/src/test/java/org/takes/misc/SelectTest.java @@ -36,7 +36,7 @@ * @version $Id$ * @since 0.13.8 */ -public class SelectTest { +public final class SelectTest { /** * Select test with condition. @@ -52,7 +52,7 @@ public void select() { alist, new Condition() { @Override - public boolean add(final String element) { + public boolean fits(final String element) { return element.endsWith("1"); } } From 321ab09a022c34eb1075d538437b03d2de662e9b Mon Sep 17 00:00:00 2001 From: super132 Date: Mon, 20 Apr 2015 18:22:46 +0800 Subject: [PATCH 24/49] Fixing compilation error in CI. --- src/main/java/org/takes/misc/Concat.java | 5 +++++ src/main/java/org/takes/misc/Select.java | 5 +++++ src/main/java/org/takes/misc/Transform.java | 5 +++++ 3 files changed, 15 insertions(+) diff --git a/src/main/java/org/takes/misc/Concat.java b/src/main/java/org/takes/misc/Concat.java index 86e4cfff5..eeb6de539 100644 --- a/src/main/java/org/takes/misc/Concat.java +++ b/src/main/java/org/takes/misc/Concat.java @@ -113,5 +113,10 @@ public E next() { throw new NoSuchElementException(); } } + + @Override + public void remove() { + throw new UnsupportedOperationException(); + } } } diff --git a/src/main/java/org/takes/misc/Select.java b/src/main/java/org/takes/misc/Select.java index 3b2a8a363..9e5e67cc7 100644 --- a/src/main/java/org/takes/misc/Select.java +++ b/src/main/java/org/takes/misc/Select.java @@ -141,6 +141,11 @@ public E next() { ); } } + + @Override + public void remove() { + throw new UnsupportedOperationException(); + } } } diff --git a/src/main/java/org/takes/misc/Transform.java b/src/main/java/org/takes/misc/Transform.java index 36a4404dd..3262f5fd0 100644 --- a/src/main/java/org/takes/misc/Transform.java +++ b/src/main/java/org/takes/misc/Transform.java @@ -103,5 +103,10 @@ public A next() { ); } } + + @Override + public void remove() { + throw new UnsupportedOperationException(); + } } } From a1e2dd709181ccc564c2f3a5e4f09d5a7c5d50d3 Mon Sep 17 00:00:00 2001 From: super132 Date: Mon, 20 Apr 2015 18:35:21 +0800 Subject: [PATCH 25/49] Fixing static code analysis issues. --- src/main/java/org/takes/misc/Concat.java | 20 ++++------ src/main/java/org/takes/misc/Select.java | 43 +++++++++++---------- src/main/java/org/takes/misc/Transform.java | 6 +-- 3 files changed, 32 insertions(+), 37 deletions(-) diff --git a/src/main/java/org/takes/misc/Concat.java b/src/main/java/org/takes/misc/Concat.java index eeb6de539..ffdb46d9a 100644 --- a/src/main/java/org/takes/misc/Concat.java +++ b/src/main/java/org/takes/misc/Concat.java @@ -65,7 +65,7 @@ public Iterator iterator() { } /** - * The concat iterator to traverse the input iterables as if they are + * The concat iterator to traverse the input iterables as if they are * from one list. */ private static class ConcatIterator implements Iterator { @@ -81,9 +81,9 @@ private static class ConcatIterator implements Iterator { private final transient Iterator right; /** - * Ctor. ConcatIterator traverses the element - * @param aitr - * @param bitr + * Ctor. ConcatIterator traverses the element. + * @param aitr The first iterable to traverse + * @param bitr The second iterable to traverse */ public ConcatIterator(final Iterator aitr, final Iterator bitr) { this.left = aitr; @@ -92,15 +92,9 @@ public ConcatIterator(final Iterator aitr, final Iterator bitr) { @Override public boolean hasNext() { - final boolean left = this.left.hasNext(); - final boolean right = this.right.hasNext(); - if (left) { - return true; - } else if (right) { - return true; - } else { - return false; - } + final boolean first = this.left.hasNext(); + final boolean second = this.right.hasNext(); + return first || second; } @Override diff --git a/src/main/java/org/takes/misc/Select.java b/src/main/java/org/takes/misc/Select.java index 9e5e67cc7..9f13373d0 100644 --- a/src/main/java/org/takes/misc/Select.java +++ b/src/main/java/org/takes/misc/Select.java @@ -43,7 +43,7 @@ public final class Select implements Iterable { private final transient Iterable list; /** - * The condition to filter the element in the iterator + * The condition to filter the element in the iterator. */ private final transient Condition condition; @@ -72,6 +72,11 @@ public Iterator iterator() { */ private static class SelectIterator implements Iterator { + /** + * The index pointing to the current element list. + */ + private static final transient int HEAD = 0; + /** * The iterator to reflect the traverse state. */ @@ -87,11 +92,6 @@ private static class SelectIterator implements Iterator { */ private final transient List current = new ArrayList(1); - /** - * The index pointing to the current element list. - */ - private static final transient int HEAD = 0; - /** * Ctor. ConcatIterator traverses the element. * @param itr Iterator of the original iterable @@ -107,23 +107,24 @@ public SelectIterator(final Iterator itr, final Condition cond) { @Override public boolean hasNext() { - if (!this.current.isEmpty()) { - if (this.condition.fits(this.current.get(HEAD))) { - return true; - } else { - this.current.remove(HEAD); - while (this.iterator.hasNext()) { - final E element = this.iterator.next(); - if (this.condition.fits(element)) { - this.current.add(element); - break; - } + if (this.current.isEmpty()) { + return false; + } + final boolean result; + if (this.condition.fits(this.current.get(HEAD))) { + result = true; + } else { + this.current.remove(HEAD); + while (this.iterator.hasNext()) { + final E element = this.iterator.next(); + if (this.condition.fits(element)) { + this.current.add(element); + break; } - return !this.current.isEmpty(); } - } else { - return false; + result = !this.current.isEmpty(); } + return result; } @Override @@ -141,7 +142,7 @@ public E next() { ); } } - + @Override public void remove() { throw new UnsupportedOperationException(); diff --git a/src/main/java/org/takes/misc/Transform.java b/src/main/java/org/takes/misc/Transform.java index 3262f5fd0..f27713c97 100644 --- a/src/main/java/org/takes/misc/Transform.java +++ b/src/main/java/org/takes/misc/Transform.java @@ -47,8 +47,8 @@ public class Transform implements Iterable { /** * Transform elements in the supplied iterable by the action supplied. - * @param list Iterable to be transformed - * @param action The actual transformation implementation + * @param itb Iterable to be transformed + * @param act The actual transformation implementation */ public Transform(final Iterable itb, final TransformAction act) { @@ -63,7 +63,7 @@ public final Iterator iterator() { /** * The iterator to iterator through the original type B and return the - * transformed element in type A + * transformed element in type A. */ private static class TransformIterator implements Iterator { From a2d9672eb93860e783b7ef5be0e0d6dee21336fb Mon Sep 17 00:00:00 2001 From: super132 Date: Mon, 20 Apr 2015 18:47:21 +0800 Subject: [PATCH 26/49] Code refactor for PMD issues --- src/main/java/org/takes/misc/Concat.java | 6 +++-- src/main/java/org/takes/misc/Select.java | 31 +++++++++++++++--------- 2 files changed, 23 insertions(+), 14 deletions(-) diff --git a/src/main/java/org/takes/misc/Concat.java b/src/main/java/org/takes/misc/Concat.java index ffdb46d9a..5f0640280 100644 --- a/src/main/java/org/takes/misc/Concat.java +++ b/src/main/java/org/takes/misc/Concat.java @@ -99,13 +99,15 @@ public boolean hasNext() { @Override public E next() { + final E object; if (this.left.hasNext()) { - return this.left.next(); + object = this.left.next(); } else if (this.right.hasNext()) { - return this.right.next(); + object = this.right.next(); } else { throw new NoSuchElementException(); } + return object; } @Override diff --git a/src/main/java/org/takes/misc/Select.java b/src/main/java/org/takes/misc/Select.java index 9f13373d0..76045d436 100644 --- a/src/main/java/org/takes/misc/Select.java +++ b/src/main/java/org/takes/misc/Select.java @@ -107,22 +107,13 @@ public SelectIterator(final Iterator itr, final Condition cond) { @Override public boolean hasNext() { + final boolean result; if (this.current.isEmpty()) { return false; - } - final boolean result; - if (this.condition.fits(this.current.get(HEAD))) { + } else if (this.condition.fits(this.current.get(HEAD))) { result = true; } else { - this.current.remove(HEAD); - while (this.iterator.hasNext()) { - final E element = this.iterator.next(); - if (this.condition.fits(element)) { - this.current.add(element); - break; - } - } - result = !this.current.isEmpty(); + result = lookForNext(); } return result; } @@ -147,6 +138,22 @@ public E next() { public void remove() { throw new UnsupportedOperationException(); } + + /** + * + * @return + */ + private boolean lookForNext() { + this.current.remove(HEAD); + while (this.iterator.hasNext()) { + final E element = this.iterator.next(); + if (this.condition.fits(element)) { + this.current.add(element); + break; + } + } + return !this.current.isEmpty(); + } } } From 6a9353982b63e437c52b9f3370bc608fa5f1770d Mon Sep 17 00:00:00 2001 From: super132 Date: Mon, 20 Apr 2015 18:50:12 +0800 Subject: [PATCH 27/49] minor Code style fixes --- src/main/java/org/takes/misc/Select.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/takes/misc/Select.java b/src/main/java/org/takes/misc/Select.java index 76045d436..87efc6959 100644 --- a/src/main/java/org/takes/misc/Select.java +++ b/src/main/java/org/takes/misc/Select.java @@ -113,7 +113,7 @@ public boolean hasNext() { } else if (this.condition.fits(this.current.get(HEAD))) { result = true; } else { - result = lookForNext(); + result = this.lookForNext(); } return result; } @@ -140,8 +140,8 @@ public void remove() { } /** - * - * @return + * Look for the first next element which matches condition. + * @return True if the element found, false otherwise */ private boolean lookForNext() { this.current.remove(HEAD); From 36fcee6726c4db25cc465f4dfe58c3f00cfdd7bf Mon Sep 17 00:00:00 2001 From: super132 Date: Mon, 20 Apr 2015 22:31:14 +0800 Subject: [PATCH 28/49] Code static analysis fix. --- src/main/java/org/takes/misc/Select.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/takes/misc/Select.java b/src/main/java/org/takes/misc/Select.java index 87efc6959..a4157d664 100644 --- a/src/main/java/org/takes/misc/Select.java +++ b/src/main/java/org/takes/misc/Select.java @@ -109,7 +109,7 @@ public SelectIterator(final Iterator itr, final Condition cond) { public boolean hasNext() { final boolean result; if (this.current.isEmpty()) { - return false; + result = false; } else if (this.condition.fits(this.current.get(HEAD))) { result = true; } else { From 520fbe55f984a164560e499c7a96ff2baa4c94d3 Mon Sep 17 00:00:00 2001 From: super132 Date: Tue, 21 Apr 2015 12:56:49 +0800 Subject: [PATCH 29/49] Code refactoring for addressing code review comments --- src/main/java/org/takes/misc/Concat.java | 8 ++--- src/main/java/org/takes/misc/Condition.java | 31 ----------------- src/main/java/org/takes/misc/Select.java | 33 ++++++++----------- src/main/java/org/takes/misc/Transform.java | 13 +++----- .../java/org/takes/rq/RqWithoutHeader.java | 8 ++++- .../java/org/takes/rs/RsWithoutHeader.java | 8 ++++- src/test/java/org/takes/misc/ConcatTest.java | 6 ++-- src/test/java/org/takes/misc/SelectTest.java | 8 +++-- ...eTransformTest.java => TransformTest.java} | 6 ++-- 9 files changed, 46 insertions(+), 75 deletions(-) rename src/test/java/org/takes/misc/{IterableTransformTest.java => TransformTest.java} (94%) diff --git a/src/main/java/org/takes/misc/Concat.java b/src/main/java/org/takes/misc/Concat.java index 5f0640280..2d8d1648d 100644 --- a/src/main/java/org/takes/misc/Concat.java +++ b/src/main/java/org/takes/misc/Concat.java @@ -92,9 +92,7 @@ public ConcatIterator(final Iterator aitr, final Iterator bitr) { @Override public boolean hasNext() { - final boolean first = this.left.hasNext(); - final boolean second = this.right.hasNext(); - return first || second; + return this.left.hasNext() || this.right.hasNext(); } @Override @@ -112,7 +110,9 @@ public E next() { @Override public void remove() { - throw new UnsupportedOperationException(); + throw new UnsupportedOperationException( + "This iterable is immutable and cannot remove anything" + ); } } } diff --git a/src/main/java/org/takes/misc/Condition.java b/src/main/java/org/takes/misc/Condition.java index 947792d26..bfd9a88e9 100644 --- a/src/main/java/org/takes/misc/Condition.java +++ b/src/main/java/org/takes/misc/Condition.java @@ -23,8 +23,6 @@ */ package org.takes.misc; -import java.util.Locale; - /** * Condition to determine how {@link Concat} behave when joining two iterables. * @@ -40,33 +38,4 @@ public interface Condition { * @return True to add the element, false to skip. */ boolean fits(T element); - - /** - * Concat lower case string condition. This condition changes all - * characters to lower case and determine if the iterable should be - * concatenated by determine if the element contain the 'prefix' - * supplied. - */ - class LowerCase implements Condition { - - /** - * Prefix. - */ - private final transient String prefix; - - /** - * Ctor. - * @param str The prefix to check - */ - public LowerCase(final String str) { - this.prefix = str; - } - - @Override - public boolean fits(final String element) { - return !element.toLowerCase(Locale.ENGLISH) - .startsWith(this.prefix); - } - } - } diff --git a/src/main/java/org/takes/misc/Select.java b/src/main/java/org/takes/misc/Select.java index 87efc6959..a36d27fcc 100644 --- a/src/main/java/org/takes/misc/Select.java +++ b/src/main/java/org/takes/misc/Select.java @@ -100,38 +100,29 @@ private static class SelectIterator implements Iterator { public SelectIterator(final Iterator itr, final Condition cond) { this.condition = cond; this.iterator = itr; - if (this.iterator.hasNext()) { - this.current.add(this.iterator.next()); - } } @Override public boolean hasNext() { - final boolean result; if (this.current.isEmpty()) { - return false; - } else if (this.condition.fits(this.current.get(HEAD))) { - result = true; - } else { - result = this.lookForNext(); + this.lookForNext(); } - return result; + return !this.current.isEmpty(); } @Override public E next() { - if (this.hasNext()) { + if (this.current.isEmpty()) { + this.lookForNext(); + } + if (!this.current.isEmpty()) { final E result = this.current.get(HEAD); - this.current.remove(0); - if (this.iterator.hasNext()) { - this.current.add(this.iterator.next()); - } + this.current.remove(HEAD); return result; - } else { - throw new NoSuchElementException( - "No more element with fits the select condition." - ); } + throw new NoSuchElementException( + "No more element with fits the select condition." + ); } @Override @@ -144,7 +135,9 @@ public void remove() { * @return True if the element found, false otherwise */ private boolean lookForNext() { - this.current.remove(HEAD); + if (!this.current.isEmpty()) { + this.current.remove(HEAD); + } while (this.iterator.hasNext()) { final E element = this.iterator.next(); if (this.condition.fits(element)) { diff --git a/src/main/java/org/takes/misc/Transform.java b/src/main/java/org/takes/misc/Transform.java index f27713c97..9e78d600d 100644 --- a/src/main/java/org/takes/misc/Transform.java +++ b/src/main/java/org/takes/misc/Transform.java @@ -24,7 +24,6 @@ package org.takes.misc; import java.util.Iterator; -import java.util.NoSuchElementException; /** * Transform elements in an iterable (in type T) into others (in type K). @@ -95,18 +94,14 @@ public boolean hasNext() { @Override public A next() { - if (this.hasNext()) { - return this.action.transform(this.iterator.next()); - } else { - throw new NoSuchElementException( - "No more element with fits the select condition." - ); - } + return this.action.transform(this.iterator.next()); } @Override public void remove() { - throw new UnsupportedOperationException(); + throw new UnsupportedOperationException( + "This iterable is immutable and cannot remove anything" + ); } } } diff --git a/src/main/java/org/takes/rq/RqWithoutHeader.java b/src/main/java/org/takes/rq/RqWithoutHeader.java index 32b6a18f2..1b7737529 100644 --- a/src/main/java/org/takes/rq/RqWithoutHeader.java +++ b/src/main/java/org/takes/rq/RqWithoutHeader.java @@ -58,7 +58,13 @@ public Iterable head() throws IOException { ); return new Select( req.head(), - new Condition.LowerCase(prefix) + new Condition() { + @Override + public boolean fits(final String element) { + return !element.toLowerCase(Locale.ENGLISH) + .startsWith(prefix); + } + } ); } @Override diff --git a/src/main/java/org/takes/rs/RsWithoutHeader.java b/src/main/java/org/takes/rs/RsWithoutHeader.java index b47289ce6..0be8f277b 100644 --- a/src/main/java/org/takes/rs/RsWithoutHeader.java +++ b/src/main/java/org/takes/rs/RsWithoutHeader.java @@ -58,7 +58,13 @@ public Iterable head() throws IOException { ); return new Select( res.head(), - new Condition.LowerCase(prefix) + new Condition() { + @Override + public boolean fits(final String element) { + return !element.toLowerCase(Locale.ENGLISH) + .startsWith(prefix); + } + } ); } @Override diff --git a/src/test/java/org/takes/misc/ConcatTest.java b/src/test/java/org/takes/misc/ConcatTest.java index a3893f770..286b245dd 100644 --- a/src/test/java/org/takes/misc/ConcatTest.java +++ b/src/test/java/org/takes/misc/ConcatTest.java @@ -39,7 +39,7 @@ public final class ConcatTest { /** - * Basic concat unit test. + * Concat can concat. */ @Test public void concat() { @@ -60,10 +60,10 @@ public void concat() { } /** - * Concat test with empty inputs. + * Concat can concat with empty list. */ @Test - public void concatWithEmpty() { + public void concatWithEmptyList() { final List alist = new ArrayList(2); final String aone = "an1"; final String atwo = "an2"; diff --git a/src/test/java/org/takes/misc/SelectTest.java b/src/test/java/org/takes/misc/SelectTest.java index fa32623b8..7ba9ee60d 100644 --- a/src/test/java/org/takes/misc/SelectTest.java +++ b/src/test/java/org/takes/misc/SelectTest.java @@ -39,15 +39,17 @@ public final class SelectTest { /** - * Select test with condition. + * Select can select with condition. */ @Test - public void select() { - final List alist = new ArrayList(2); + public void selectWithCondition() { + final List alist = new ArrayList(3); final String aone = "at1"; final String atwo = "at2"; + final String athree = "at31"; alist.add(aone); alist.add(atwo); + alist.add(athree); final Iterable result = new Select( alist, new Condition() { diff --git a/src/test/java/org/takes/misc/IterableTransformTest.java b/src/test/java/org/takes/misc/TransformTest.java similarity index 94% rename from src/test/java/org/takes/misc/IterableTransformTest.java rename to src/test/java/org/takes/misc/TransformTest.java index 7be49c9ed..b7a3930bd 100644 --- a/src/test/java/org/takes/misc/IterableTransformTest.java +++ b/src/test/java/org/takes/misc/TransformTest.java @@ -36,13 +36,13 @@ * @version $Id$ * @since 0.13.8 */ -public final class IterableTransformTest { +public final class TransformTest { /** - * Iterable transform test. + * Transform can transform list. */ @Test - public void iterableTransform() { + public void transformList() { final List alist = new ArrayList(3); alist.add("a1"); alist.add("b1"); From 6b016ef9b7565e8d783df409111ad3133355d955 Mon Sep 17 00:00:00 2001 From: super132 Date: Tue, 21 Apr 2015 13:19:33 +0800 Subject: [PATCH 30/49] Fixing code styling issues --- src/main/java/org/takes/misc/Condition.java | 31 +++++++++++++++++++ .../java/org/takes/rq/RqWithoutHeader.java | 8 +---- .../java/org/takes/rs/RsWithoutHeader.java | 8 +---- 3 files changed, 33 insertions(+), 14 deletions(-) diff --git a/src/main/java/org/takes/misc/Condition.java b/src/main/java/org/takes/misc/Condition.java index bfd9a88e9..80e301c87 100644 --- a/src/main/java/org/takes/misc/Condition.java +++ b/src/main/java/org/takes/misc/Condition.java @@ -23,6 +23,8 @@ */ package org.takes.misc; +import java.util.Locale; + /** * Condition to determine how {@link Concat} behave when joining two iterables. * @@ -38,4 +40,33 @@ public interface Condition { * @return True to add the element, false to skip. */ boolean fits(T element); + + /** + * Concat lower case string condition. This condition changes all + * characters to lower case and determine if the iterable should be + * concatenated by determine if the element contain the 'prefix' + * supplied. + */ + class LowerStartsWith implements Condition { + + /** + * Prefix. + */ + private final transient String prefix; + + /** + * Ctor. + * @param str The prefix to check + */ + public LowerStartsWith(final String str) { + this.prefix = str; + } + + @Override + public boolean fits(final String element) { + return !element.toLowerCase(Locale.ENGLISH) + .startsWith(this.prefix); + } + } + } diff --git a/src/main/java/org/takes/rq/RqWithoutHeader.java b/src/main/java/org/takes/rq/RqWithoutHeader.java index 1b7737529..de5da4fd1 100644 --- a/src/main/java/org/takes/rq/RqWithoutHeader.java +++ b/src/main/java/org/takes/rq/RqWithoutHeader.java @@ -58,13 +58,7 @@ public Iterable head() throws IOException { ); return new Select( req.head(), - new Condition() { - @Override - public boolean fits(final String element) { - return !element.toLowerCase(Locale.ENGLISH) - .startsWith(prefix); - } - } + new Condition.LowerStartsWith(prefix) ); } @Override diff --git a/src/main/java/org/takes/rs/RsWithoutHeader.java b/src/main/java/org/takes/rs/RsWithoutHeader.java index 0be8f277b..c5be7c6c3 100644 --- a/src/main/java/org/takes/rs/RsWithoutHeader.java +++ b/src/main/java/org/takes/rs/RsWithoutHeader.java @@ -58,13 +58,7 @@ public Iterable head() throws IOException { ); return new Select( res.head(), - new Condition() { - @Override - public boolean fits(final String element) { - return !element.toLowerCase(Locale.ENGLISH) - .startsWith(prefix); - } - } + new Condition.LowerStartsWith(prefix) ); } @Override From 02f86129b7a34387c9784be16aade9c9e0afea30 Mon Sep 17 00:00:00 2001 From: super132 Date: Tue, 21 Apr 2015 13:24:04 +0800 Subject: [PATCH 31/49] Minor stlying fix --- src/main/java/org/takes/misc/Transform.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/takes/misc/Transform.java b/src/main/java/org/takes/misc/Transform.java index 9e78d600d..37557d518 100644 --- a/src/main/java/org/takes/misc/Transform.java +++ b/src/main/java/org/takes/misc/Transform.java @@ -82,7 +82,7 @@ private static class TransformIterator implements Iterator { * @param act Action to transform elements */ public TransformIterator(final Iterator itr, - final TransformAction act) { + final TransformAction act) { this.action = act; this.iterator = itr; } From a823694542c94bb6831ee861930b6dbae7fb6374 Mon Sep 17 00:00:00 2001 From: super132 Date: Tue, 21 Apr 2015 13:25:14 +0800 Subject: [PATCH 32/49] Other minor code style fixes --- src/main/java/org/takes/misc/Condition.java | 2 +- src/main/java/org/takes/misc/Transform.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/takes/misc/Condition.java b/src/main/java/org/takes/misc/Condition.java index 80e301c87..49c2afb1e 100644 --- a/src/main/java/org/takes/misc/Condition.java +++ b/src/main/java/org/takes/misc/Condition.java @@ -65,7 +65,7 @@ public LowerStartsWith(final String str) { @Override public boolean fits(final String element) { return !element.toLowerCase(Locale.ENGLISH) - .startsWith(this.prefix); + .startsWith(this.prefix); } } diff --git a/src/main/java/org/takes/misc/Transform.java b/src/main/java/org/takes/misc/Transform.java index 37557d518..416794db1 100644 --- a/src/main/java/org/takes/misc/Transform.java +++ b/src/main/java/org/takes/misc/Transform.java @@ -50,7 +50,7 @@ public class Transform implements Iterable { * @param act The actual transformation implementation */ public Transform(final Iterable itb, - final TransformAction act) { + final TransformAction act) { this.list = itb; this.action = act; } From 3f1d3ff42f71112a34ac61338786695544264e68 Mon Sep 17 00:00:00 2001 From: super132 Date: Tue, 21 Apr 2015 18:05:52 +0800 Subject: [PATCH 33/49] Further code refinements. --- src/main/java/org/takes/misc/Concat.java | 5 +---- src/main/java/org/takes/misc/Select.java | 4 +++- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/takes/misc/Concat.java b/src/main/java/org/takes/misc/Concat.java index 2d8d1648d..695fb2b09 100644 --- a/src/main/java/org/takes/misc/Concat.java +++ b/src/main/java/org/takes/misc/Concat.java @@ -24,7 +24,6 @@ package org.takes.misc; import java.util.Iterator; -import java.util.NoSuchElementException; /** * Concat iterable. @@ -100,10 +99,8 @@ public E next() { final E object; if (this.left.hasNext()) { object = this.left.next(); - } else if (this.right.hasNext()) { - object = this.right.next(); } else { - throw new NoSuchElementException(); + object = this.right.next(); } return object; } diff --git a/src/main/java/org/takes/misc/Select.java b/src/main/java/org/takes/misc/Select.java index a36d27fcc..050e56bcd 100644 --- a/src/main/java/org/takes/misc/Select.java +++ b/src/main/java/org/takes/misc/Select.java @@ -127,7 +127,9 @@ public E next() { @Override public void remove() { - throw new UnsupportedOperationException(); + throw new UnsupportedOperationException( + "This iterable is immutable and cannot remove anything" + ); } /** From c371d8ad7988fd6bbf8d8ce5c1d02013c2dc6685 Mon Sep 17 00:00:00 2001 From: super132 Date: Tue, 21 Apr 2015 18:38:21 +0800 Subject: [PATCH 34/49] Minor code refactoring. --- src/main/java/org/takes/misc/Transform.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/takes/misc/Transform.java b/src/main/java/org/takes/misc/Transform.java index 416794db1..257684e4b 100644 --- a/src/main/java/org/takes/misc/Transform.java +++ b/src/main/java/org/takes/misc/Transform.java @@ -32,7 +32,7 @@ * @version $Id$ * @since 0.13.8 */ -public class Transform implements Iterable { +public final class Transform implements Iterable { /** * Internal storage. From 3537d5e17818247e7a61d1ac14a2b4d5fbb039d8 Mon Sep 17 00:00:00 2001 From: Jason Wong Date: Tue, 21 Apr 2015 18:43:01 +0800 Subject: [PATCH 35/49] Revert "Minor code refactoring." This reverts commit c371d8ad7988fd6bbf8d8ce5c1d02013c2dc6685. --- src/main/java/org/takes/misc/Transform.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/takes/misc/Transform.java b/src/main/java/org/takes/misc/Transform.java index 257684e4b..416794db1 100644 --- a/src/main/java/org/takes/misc/Transform.java +++ b/src/main/java/org/takes/misc/Transform.java @@ -32,7 +32,7 @@ * @version $Id$ * @since 0.13.8 */ -public final class Transform implements Iterable { +public class Transform implements Iterable { /** * Internal storage. From 99eaf3d0020e2e25c2f5bcc82b18dc63bb6cc298 Mon Sep 17 00:00:00 2001 From: super132 Date: Tue, 21 Apr 2015 22:33:27 +0800 Subject: [PATCH 36/49] Minor javadoc update --- src/test/java/org/takes/misc/SelectTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/org/takes/misc/SelectTest.java b/src/test/java/org/takes/misc/SelectTest.java index 7ba9ee60d..6bd505181 100644 --- a/src/test/java/org/takes/misc/SelectTest.java +++ b/src/test/java/org/takes/misc/SelectTest.java @@ -30,7 +30,7 @@ import org.junit.Test; /** - * Test case for {@link Concat}. + * Test case for {@link Select}. * * @author Jason Wong (super132j@yahoo.com) * @version $Id$ From d02c65a3af6408d744bbdec163b0bb725fff7a43 Mon Sep 17 00:00:00 2001 From: super132 Date: Wed, 22 Apr 2015 09:39:07 +0800 Subject: [PATCH 37/49] Extracting out negation condition from LowerStartsWith --- src/main/java/org/takes/misc/Condition.java | 26 ++++++++++++++++++- .../java/org/takes/rq/RqWithoutHeader.java | 4 ++- .../java/org/takes/rs/RsWithoutHeader.java | 4 ++- 3 files changed, 31 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/takes/misc/Condition.java b/src/main/java/org/takes/misc/Condition.java index 49c2afb1e..5ad7642d2 100644 --- a/src/main/java/org/takes/misc/Condition.java +++ b/src/main/java/org/takes/misc/Condition.java @@ -64,9 +64,33 @@ public LowerStartsWith(final String str) { @Override public boolean fits(final String element) { - return !element.toLowerCase(Locale.ENGLISH) + return element.toLowerCase(Locale.ENGLISH) .startsWith(this.prefix); } } + /** + * Negating condition of any condition + */ + class Not implements Condition { + + /** + * Prefix. + */ + private final transient Condition condition; + + /** + * Ctor. + * @param str The prefix to check + */ + public Not(final Condition cond) { + this.condition = cond; + } + + @Override + public boolean fits(final T element) { + return !this.condition.fits(element); + } + } + } diff --git a/src/main/java/org/takes/rq/RqWithoutHeader.java b/src/main/java/org/takes/rq/RqWithoutHeader.java index de5da4fd1..0668b428b 100644 --- a/src/main/java/org/takes/rq/RqWithoutHeader.java +++ b/src/main/java/org/takes/rq/RqWithoutHeader.java @@ -58,7 +58,9 @@ public Iterable head() throws IOException { ); return new Select( req.head(), - new Condition.LowerStartsWith(prefix) + new Condition.Not( + new Condition.LowerStartsWith(prefix) + ) ); } @Override diff --git a/src/main/java/org/takes/rs/RsWithoutHeader.java b/src/main/java/org/takes/rs/RsWithoutHeader.java index c5be7c6c3..f4532b016 100644 --- a/src/main/java/org/takes/rs/RsWithoutHeader.java +++ b/src/main/java/org/takes/rs/RsWithoutHeader.java @@ -58,7 +58,9 @@ public Iterable head() throws IOException { ); return new Select( res.head(), - new Condition.LowerStartsWith(prefix) + new Condition.Not( + new Condition.LowerStartsWith(prefix) + ) ); } @Override From 546168dce634193b252a3b37a43a08cba6076906 Mon Sep 17 00:00:00 2001 From: super132 Date: Wed, 22 Apr 2015 09:52:01 +0800 Subject: [PATCH 38/49] Fixing code style issues --- src/main/java/org/takes/misc/Condition.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/takes/misc/Condition.java b/src/main/java/org/takes/misc/Condition.java index 5ad7642d2..dc2b4b7d4 100644 --- a/src/main/java/org/takes/misc/Condition.java +++ b/src/main/java/org/takes/misc/Condition.java @@ -70,18 +70,18 @@ public boolean fits(final String element) { } /** - * Negating condition of any condition + * Negating condition of any condition. */ class Not implements Condition { /** * Prefix. */ - private final transient Condition condition; + private final transient Condition condition; /** * Ctor. - * @param str The prefix to check + * @param cond The condition to negate */ public Not(final Condition cond) { this.condition = cond; From 299627adb3bf6e5f106d45b141c55441d36a7691 Mon Sep 17 00:00:00 2001 From: super132 Date: Wed, 22 Apr 2015 09:55:25 +0800 Subject: [PATCH 39/49] Minor update to javadoc --- src/main/java/org/takes/misc/Condition.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/takes/misc/Condition.java b/src/main/java/org/takes/misc/Condition.java index dc2b4b7d4..286be8379 100644 --- a/src/main/java/org/takes/misc/Condition.java +++ b/src/main/java/org/takes/misc/Condition.java @@ -26,7 +26,7 @@ import java.util.Locale; /** - * Condition to determine how {@link Concat} behave when joining two iterables. + * Condition to determine how {@link Select} behave when filtering an iterable. * * @author Jason Wong (super132j@yahoo.com) * @version $Id$ From 91d760b981a527b420b34979368abd3c534d5bf3 Mon Sep 17 00:00:00 2001 From: super132 Date: Wed, 22 Apr 2015 10:25:11 +0800 Subject: [PATCH 40/49] Minor spacing update --- src/main/java/org/takes/rs/RsWithStatus.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/takes/rs/RsWithStatus.java b/src/main/java/org/takes/rs/RsWithStatus.java index 533053583..09cce6457 100644 --- a/src/main/java/org/takes/rs/RsWithStatus.java +++ b/src/main/java/org/takes/rs/RsWithStatus.java @@ -115,7 +115,7 @@ private static Iterable head(final Response origin, } return new Concat( Collections.singleton( - String.format("HTTP/1.1 %d %s", status, reason) + String.format("HTTP/1.1 %d %s", status, reason) ), new Select( origin.head(), From 69ff9344606bb03a85c6941d087d09f979a67205 Mon Sep 17 00:00:00 2001 From: super132 Date: Wed, 22 Apr 2015 12:43:47 +0800 Subject: [PATCH 41/49] Refactored SelectIterator to use Guava AbstractIterator --- src/main/java/org/takes/misc/Select.java | 66 +++++------------------- 1 file changed, 12 insertions(+), 54 deletions(-) diff --git a/src/main/java/org/takes/misc/Select.java b/src/main/java/org/takes/misc/Select.java index 050e56bcd..9cd767390 100644 --- a/src/main/java/org/takes/misc/Select.java +++ b/src/main/java/org/takes/misc/Select.java @@ -23,10 +23,10 @@ */ package org.takes.misc; +import com.google.common.collect.AbstractIterator; import java.util.ArrayList; import java.util.Iterator; import java.util.List; -import java.util.NoSuchElementException; /** * Select elements into a new iterable with given condition. @@ -70,12 +70,7 @@ public Iterator iterator() { * The select iterator to traverse the input iterables and return the * elements from the list with given condition. */ - private static class SelectIterator implements Iterator { - - /** - * The index pointing to the current element list. - */ - private static final transient int HEAD = 0; + private static class SelectIterator extends AbstractIterator { /** * The iterator to reflect the traverse state. @@ -87,11 +82,6 @@ private static class SelectIterator implements Iterator { */ private final transient Condition condition; - /** - * The list storing the current object of the iterator. - */ - private final transient List current = new ArrayList(1); - /** * Ctor. ConcatIterator traverses the element. * @param itr Iterator of the original iterable @@ -101,53 +91,21 @@ public SelectIterator(final Iterator itr, final Condition cond) { this.condition = cond; this.iterator = itr; } - - @Override - public boolean hasNext() { - if (this.current.isEmpty()) { - this.lookForNext(); - } - return !this.current.isEmpty(); - } - - @Override - public E next() { - if (this.current.isEmpty()) { - this.lookForNext(); - } - if (!this.current.isEmpty()) { - final E result = this.current.get(HEAD); - this.current.remove(HEAD); - return result; - } - throw new NoSuchElementException( - "No more element with fits the select condition." - ); - } - + @Override - public void remove() { - throw new UnsupportedOperationException( - "This iterable is immutable and cannot remove anything" - ); - } - - /** - * Look for the first next element which matches condition. - * @return True if the element found, false otherwise - */ - private boolean lookForNext() { - if (!this.current.isEmpty()) { - this.current.remove(HEAD); - } + protected E computeNext() { + final List element = new ArrayList(1); while (this.iterator.hasNext()) { - final E element = this.iterator.next(); - if (this.condition.fits(element)) { - this.current.add(element); + final E object = this.iterator.next(); + if (this.condition.fits(object)) { + element.add(object); break; } } - return !this.current.isEmpty(); + if (element.isEmpty()) { + element.add(this.endOfData()); + } + return element.get(0); } } From 45b207966a733718eadd6a8ed5a9fdce8486bd3e Mon Sep 17 00:00:00 2001 From: super132 Date: Wed, 22 Apr 2015 12:50:11 +0800 Subject: [PATCH 42/49] Updating POM to enable use of Guava --- pom.xml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/pom.xml b/pom.xml index 88af4d59f..733079bbb 100644 --- a/pom.xml +++ b/pom.xml @@ -135,6 +135,11 @@ compile true + + com.google.guava + guava + provided + org.projectlombok lombok From 1385aeed0d244c4b3791c4148792e0f83ebb71aa Mon Sep 17 00:00:00 2001 From: super132 Date: Wed, 22 Apr 2015 12:55:25 +0800 Subject: [PATCH 43/49] Fixing POM.xml --- pom.xml | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/pom.xml b/pom.xml index 733079bbb..d2d0e3e08 100644 --- a/pom.xml +++ b/pom.xml @@ -138,7 +138,7 @@ com.google.guava guava - provided + compile org.projectlombok @@ -154,11 +154,6 @@ commons-io test - - com.google.guava - guava - test - com.jcabi jcabi-matchers From 76012c20e4e2c10e9265c937ee58a9211aad694a Mon Sep 17 00:00:00 2001 From: super132 Date: Wed, 22 Apr 2015 13:01:08 +0800 Subject: [PATCH 44/49] Fixing minor space problem. --- src/main/java/org/takes/misc/Select.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/takes/misc/Select.java b/src/main/java/org/takes/misc/Select.java index 9cd767390..821312d66 100644 --- a/src/main/java/org/takes/misc/Select.java +++ b/src/main/java/org/takes/misc/Select.java @@ -91,7 +91,7 @@ public SelectIterator(final Iterator itr, final Condition cond) { this.condition = cond; this.iterator = itr; } - + @Override protected E computeNext() { final List element = new ArrayList(1); From 9750dc852e63542fba48f6bcd998103f1411de49 Mon Sep 17 00:00:00 2001 From: super132 Date: Wed, 22 Apr 2015 13:06:56 +0800 Subject: [PATCH 45/49] Adding super() call in constructor --- src/main/java/org/takes/misc/Select.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/org/takes/misc/Select.java b/src/main/java/org/takes/misc/Select.java index 821312d66..59dfcfa0c 100644 --- a/src/main/java/org/takes/misc/Select.java +++ b/src/main/java/org/takes/misc/Select.java @@ -88,6 +88,7 @@ private static class SelectIterator extends AbstractIterator { * @param cond Condition to filter out elements */ public SelectIterator(final Iterator itr, final Condition cond) { + super(); this.condition = cond; this.iterator = itr; } From a3693bb3bda64b6df9044f44f4017e29910bb84e Mon Sep 17 00:00:00 2001 From: super132 Date: Wed, 22 Apr 2015 13:52:23 +0800 Subject: [PATCH 46/49] Refactoring LowerStartsWith condition into two to fulfill design needs --- src/main/java/org/takes/misc/Condition.java | 39 +++++++++++++++---- .../java/org/takes/rq/RqWithoutHeader.java | 4 +- .../java/org/takes/rs/RsWithoutHeader.java | 4 +- 3 files changed, 37 insertions(+), 10 deletions(-) diff --git a/src/main/java/org/takes/misc/Condition.java b/src/main/java/org/takes/misc/Condition.java index 286be8379..974cb670a 100644 --- a/src/main/java/org/takes/misc/Condition.java +++ b/src/main/java/org/takes/misc/Condition.java @@ -42,12 +42,11 @@ public interface Condition { boolean fits(T element); /** - * Concat lower case string condition. This condition changes all - * characters to lower case and determine if the iterable should be - * concatenated by determine if the element contain the 'prefix' + * Starts with string condition. This condition determines if the iterable + * should be concatenated by determine if the element contain the 'prefix' * supplied. */ - class LowerStartsWith implements Condition { + class StartsWith implements Condition { /** * Prefix. @@ -58,14 +57,13 @@ class LowerStartsWith implements Condition { * Ctor. * @param str The prefix to check */ - public LowerStartsWith(final String str) { + public StartsWith(final String str) { this.prefix = str; } @Override public boolean fits(final String element) { - return element.toLowerCase(Locale.ENGLISH) - .startsWith(this.prefix); + return element.startsWith(this.prefix); } } @@ -75,7 +73,7 @@ public boolean fits(final String element) { class Not implements Condition { /** - * Prefix. + * Condition. */ private final transient Condition condition; @@ -93,4 +91,29 @@ public boolean fits(final T element) { } } + /** + * Translate the string element into lower case for condition checking. + * It does not alter the element in an iterable. + */ + class LowerCase implements Condition { + + /** + * Condition. + */ + private final transient Condition condition; + + /** + * Ctor. + * @param cond The condition for checking + */ + public LowerCase(final Condition cond) { + this.condition = cond; + } + + @Override + public boolean fits(final String element) { + return this.condition.fits(element.toLowerCase(Locale.ENGLISH)); + } + } + } diff --git a/src/main/java/org/takes/rq/RqWithoutHeader.java b/src/main/java/org/takes/rq/RqWithoutHeader.java index 0668b428b..c394697a0 100644 --- a/src/main/java/org/takes/rq/RqWithoutHeader.java +++ b/src/main/java/org/takes/rq/RqWithoutHeader.java @@ -59,7 +59,9 @@ public Iterable head() throws IOException { return new Select( req.head(), new Condition.Not( - new Condition.LowerStartsWith(prefix) + new Condition.LowerCase( + new Condition.StartsWith(prefix) + ) ) ); } diff --git a/src/main/java/org/takes/rs/RsWithoutHeader.java b/src/main/java/org/takes/rs/RsWithoutHeader.java index f4532b016..a6670daa0 100644 --- a/src/main/java/org/takes/rs/RsWithoutHeader.java +++ b/src/main/java/org/takes/rs/RsWithoutHeader.java @@ -59,7 +59,9 @@ public Iterable head() throws IOException { return new Select( res.head(), new Condition.Not( - new Condition.LowerStartsWith(prefix) + new Condition.LowerCase( + new Condition.StartsWith(prefix) + ) ) ); } From 6e25d5b5659361a20b61aad2cb67630ca57916e0 Mon Sep 17 00:00:00 2001 From: super132 Date: Wed, 22 Apr 2015 13:55:57 +0800 Subject: [PATCH 47/49] Fixing minor spacing issues --- src/main/java/org/takes/misc/Condition.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/takes/misc/Condition.java b/src/main/java/org/takes/misc/Condition.java index 974cb670a..f46399867 100644 --- a/src/main/java/org/takes/misc/Condition.java +++ b/src/main/java/org/takes/misc/Condition.java @@ -96,12 +96,12 @@ public boolean fits(final T element) { * It does not alter the element in an iterable. */ class LowerCase implements Condition { - + /** * Condition. */ private final transient Condition condition; - + /** * Ctor. * @param cond The condition for checking From 1af35836ab1ce37368a6c770adad081c22104ccf Mon Sep 17 00:00:00 2001 From: super132 Date: Thu, 23 Apr 2015 10:49:25 +0800 Subject: [PATCH 48/49] Removing Guava dependency --- pom.xml | 10 ++-- src/main/java/org/takes/misc/Select.java | 65 +++++++++++++++++++----- 2 files changed, 58 insertions(+), 17 deletions(-) diff --git a/pom.xml b/pom.xml index d2d0e3e08..88af4d59f 100644 --- a/pom.xml +++ b/pom.xml @@ -135,11 +135,6 @@ compile true - - com.google.guava - guava - compile - org.projectlombok lombok @@ -154,6 +149,11 @@ commons-io test + + com.google.guava + guava + test + com.jcabi jcabi-matchers diff --git a/src/main/java/org/takes/misc/Select.java b/src/main/java/org/takes/misc/Select.java index 59dfcfa0c..050e56bcd 100644 --- a/src/main/java/org/takes/misc/Select.java +++ b/src/main/java/org/takes/misc/Select.java @@ -23,10 +23,10 @@ */ package org.takes.misc; -import com.google.common.collect.AbstractIterator; import java.util.ArrayList; import java.util.Iterator; import java.util.List; +import java.util.NoSuchElementException; /** * Select elements into a new iterable with given condition. @@ -70,7 +70,12 @@ public Iterator iterator() { * The select iterator to traverse the input iterables and return the * elements from the list with given condition. */ - private static class SelectIterator extends AbstractIterator { + private static class SelectIterator implements Iterator { + + /** + * The index pointing to the current element list. + */ + private static final transient int HEAD = 0; /** * The iterator to reflect the traverse state. @@ -82,31 +87,67 @@ private static class SelectIterator extends AbstractIterator { */ private final transient Condition condition; + /** + * The list storing the current object of the iterator. + */ + private final transient List current = new ArrayList(1); + /** * Ctor. ConcatIterator traverses the element. * @param itr Iterator of the original iterable * @param cond Condition to filter out elements */ public SelectIterator(final Iterator itr, final Condition cond) { - super(); this.condition = cond; this.iterator = itr; } @Override - protected E computeNext() { - final List element = new ArrayList(1); + public boolean hasNext() { + if (this.current.isEmpty()) { + this.lookForNext(); + } + return !this.current.isEmpty(); + } + + @Override + public E next() { + if (this.current.isEmpty()) { + this.lookForNext(); + } + if (!this.current.isEmpty()) { + final E result = this.current.get(HEAD); + this.current.remove(HEAD); + return result; + } + throw new NoSuchElementException( + "No more element with fits the select condition." + ); + } + + @Override + public void remove() { + throw new UnsupportedOperationException( + "This iterable is immutable and cannot remove anything" + ); + } + + /** + * Look for the first next element which matches condition. + * @return True if the element found, false otherwise + */ + private boolean lookForNext() { + if (!this.current.isEmpty()) { + this.current.remove(HEAD); + } while (this.iterator.hasNext()) { - final E object = this.iterator.next(); - if (this.condition.fits(object)) { - element.add(object); + final E element = this.iterator.next(); + if (this.condition.fits(element)) { + this.current.add(element); break; } } - if (element.isEmpty()) { - element.add(this.endOfData()); - } - return element.get(0); + return !this.current.isEmpty(); } } From 08737a00365fbac4ed068b156300aa2e6e9f9292 Mon Sep 17 00:00:00 2001 From: super132 Date: Sat, 25 Apr 2015 09:31:00 +0800 Subject: [PATCH 49/49] Refactoring case insensitive comparison to inline condition. --- src/main/java/org/takes/misc/Condition.java | 53 ------------------- .../java/org/takes/rq/RqWithoutHeader.java | 13 +++-- .../java/org/takes/rs/RsWithoutHeader.java | 13 +++-- 3 files changed, 16 insertions(+), 63 deletions(-) diff --git a/src/main/java/org/takes/misc/Condition.java b/src/main/java/org/takes/misc/Condition.java index f46399867..9a109897f 100644 --- a/src/main/java/org/takes/misc/Condition.java +++ b/src/main/java/org/takes/misc/Condition.java @@ -23,8 +23,6 @@ */ package org.takes.misc; -import java.util.Locale; - /** * Condition to determine how {@link Select} behave when filtering an iterable. * @@ -41,32 +39,6 @@ public interface Condition { */ boolean fits(T element); - /** - * Starts with string condition. This condition determines if the iterable - * should be concatenated by determine if the element contain the 'prefix' - * supplied. - */ - class StartsWith implements Condition { - - /** - * Prefix. - */ - private final transient String prefix; - - /** - * Ctor. - * @param str The prefix to check - */ - public StartsWith(final String str) { - this.prefix = str; - } - - @Override - public boolean fits(final String element) { - return element.startsWith(this.prefix); - } - } - /** * Negating condition of any condition. */ @@ -91,29 +63,4 @@ public boolean fits(final T element) { } } - /** - * Translate the string element into lower case for condition checking. - * It does not alter the element in an iterable. - */ - class LowerCase implements Condition { - - /** - * Condition. - */ - private final transient Condition condition; - - /** - * Ctor. - * @param cond The condition for checking - */ - public LowerCase(final Condition cond) { - this.condition = cond; - } - - @Override - public boolean fits(final String element) { - return this.condition.fits(element.toLowerCase(Locale.ENGLISH)); - } - } - } diff --git a/src/main/java/org/takes/rq/RqWithoutHeader.java b/src/main/java/org/takes/rq/RqWithoutHeader.java index c394697a0..b998918a4 100644 --- a/src/main/java/org/takes/rq/RqWithoutHeader.java +++ b/src/main/java/org/takes/rq/RqWithoutHeader.java @@ -50,6 +50,7 @@ public final class RqWithoutHeader extends RqWrap { */ public RqWithoutHeader(final Request req, final CharSequence name) { super( + // @checkstyle AnonInnerLengthCheck (50 lines) new Request() { @Override public Iterable head() throws IOException { @@ -58,11 +59,13 @@ public Iterable head() throws IOException { ); return new Select( req.head(), - new Condition.Not( - new Condition.LowerCase( - new Condition.StartsWith(prefix) - ) - ) + new Condition() { + @Override + public boolean fits(final String header) { + return !header.toLowerCase(Locale.ENGLISH) + .startsWith(prefix); + } + } ); } @Override diff --git a/src/main/java/org/takes/rs/RsWithoutHeader.java b/src/main/java/org/takes/rs/RsWithoutHeader.java index a6670daa0..daef1245a 100644 --- a/src/main/java/org/takes/rs/RsWithoutHeader.java +++ b/src/main/java/org/takes/rs/RsWithoutHeader.java @@ -50,6 +50,7 @@ public final class RsWithoutHeader extends RsWrap { */ public RsWithoutHeader(final Response res, final CharSequence name) { super( + // @checkstyle AnonInnerLengthCheck (50 lines) new Response() { @Override public Iterable head() throws IOException { @@ -58,11 +59,13 @@ public Iterable head() throws IOException { ); return new Select( res.head(), - new Condition.Not( - new Condition.LowerCase( - new Condition.StartsWith(prefix) - ) - ) + new Condition() { + @Override + public boolean fits(final String header) { + return !header.toLowerCase(Locale.ENGLISH) + .startsWith(prefix); + } + } ); } @Override