From 1afddf9102d4abfd122daafd58546073bd7b3503 Mon Sep 17 00:00:00 2001 From: super132 Date: Fri, 1 May 2015 10:56:29 +0800 Subject: [PATCH 1/9] Introducing Transform for changing iterables in RsWithHeaders --- src/main/java/org/takes/misc/Transform.java | 107 ++++++++++++++++++ .../java/org/takes/misc/TransformAction.java | 63 +++++++++++ src/main/java/org/takes/rs/RsWithHeaders.java | 3 + .../java/org/takes/misc/TransformTest.java | 64 +++++++++++ 4 files changed, 237 insertions(+) create mode 100644 src/main/java/org/takes/misc/Transform.java create mode 100644 src/main/java/org/takes/misc/TransformAction.java create mode 100644 src/test/java/org/takes/misc/TransformTest.java diff --git a/src/main/java/org/takes/misc/Transform.java b/src/main/java/org/takes/misc/Transform.java new file mode 100644 index 000000000..416794db1 --- /dev/null +++ b/src/main/java/org/takes/misc/Transform.java @@ -0,0 +1,107 @@ +/** + * 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; + +/** + * Transform elements in an iterable (in type T) into others (in type K). + * + * @author Jason Wong (super132j@yahoo.com) + * @version $Id$ + * @since 0.13.8 + */ +public class Transform implements Iterable { + + /** + * Internal storage. + */ + 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 itb Iterable to be transformed + * @param act The actual transformation implementation + */ + public Transform(final Iterable itb, + final TransformAction act) { + this.list = itb; + this.action = act; + } + + @Override + public final Iterator 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() { + return this.action.transform(this.iterator.next()); + } + + @Override + public void remove() { + throw new UnsupportedOperationException( + "This iterable is immutable and cannot remove anything" + ); + } + } +} 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..1755762b5 --- /dev/null +++ b/src/main/java/org/takes/misc/TransformAction.java @@ -0,0 +1,63 @@ +/** + * 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 Transform} 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 Transform}. + */ + 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/rs/RsWithHeaders.java b/src/main/java/org/takes/rs/RsWithHeaders.java index e6cf2678f..84a3741f4 100644 --- a/src/main/java/org/takes/rs/RsWithHeaders.java +++ b/src/main/java/org/takes/rs/RsWithHeaders.java @@ -63,6 +63,9 @@ public RsWithHeaders(final Response res, new Response() { @Override public List head() throws IOException { + //@todo #160:DEV To implement the concatenation and + // transformation with conjunction of Concat class + // and Transform class to get rid of the use of List.add() final List head = new LinkedList(); for (final String hdr : res.head()) { head.add(hdr); diff --git a/src/test/java/org/takes/misc/TransformTest.java b/src/test/java/org/takes/misc/TransformTest.java new file mode 100644 index 000000000..b7a3930bd --- /dev/null +++ b/src/test/java/org/takes/misc/TransformTest.java @@ -0,0 +1,64 @@ +/** + * 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 Transform}. + * + * @author Jason Wong (super132j@yahoo.com) + * @version $Id$ + * @since 0.13.8 + */ +public final class TransformTest { + + /** + * Transform can transform list. + */ + @Test + public void transformList() { + final List alist = new ArrayList(3); + alist.add("a1"); + alist.add("b1"); + alist.add("c1"); + MatcherAssert.assertThat( + new Transform( + alist, + new TransformAction() { + @Override + public String transform(final String element) { + return element.concat("t"); + } + } + ), + Matchers.hasItems("a1t", "b1t", "c1t") + ); + } + +} From 84f3a37e0df84dfa6a82aa2a87960ee1a38a01b4 Mon Sep 17 00:00:00 2001 From: super132 Date: Fri, 1 May 2015 11:44:02 +0800 Subject: [PATCH 2/9] Updating javadoc --- src/main/java/org/takes/misc/Transform.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/org/takes/misc/Transform.java b/src/main/java/org/takes/misc/Transform.java index 416794db1..61876552d 100644 --- a/src/main/java/org/takes/misc/Transform.java +++ b/src/main/java/org/takes/misc/Transform.java @@ -63,6 +63,8 @@ public final Iterator iterator() { /** * The iterator to iterator through the original type B and return the * transformed element in type A. + * + *

This class is NOT thread-safe. */ private static class TransformIterator implements Iterator { From fe6a251db06b34ff61d1f5c33bb50c27dea1b329 Mon Sep 17 00:00:00 2001 From: super132 Date: Fri, 1 May 2015 12:32:34 +0800 Subject: [PATCH 3/9] Updating todo comments to fix check styles error --- src/main/java/org/takes/rs/RsWithHeaders.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/takes/rs/RsWithHeaders.java b/src/main/java/org/takes/rs/RsWithHeaders.java index 84a3741f4..049957db1 100644 --- a/src/main/java/org/takes/rs/RsWithHeaders.java +++ b/src/main/java/org/takes/rs/RsWithHeaders.java @@ -52,6 +52,10 @@ public RsWithHeaders(final Response res, final CharSequence... headers) { this(res, Arrays.asList(headers)); } + //@todo #160:DEV To implement the concatenation and + // transformation with conjunction of Concat class + // and Transform class to get rid of the use of List.add() + // in the anonymous Response class head() nethod. /** * Ctor. * @param res Original response @@ -63,9 +67,6 @@ public RsWithHeaders(final Response res, new Response() { @Override public List head() throws IOException { - //@todo #160:DEV To implement the concatenation and - // transformation with conjunction of Concat class - // and Transform class to get rid of the use of List.add() final List head = new LinkedList(); for (final String hdr : res.head()) { head.add(hdr); From 04919afb627b13c766ba759de8ae9e89ed9f7ae9 Mon Sep 17 00:00:00 2001 From: super132 Date: Fri, 1 May 2015 20:53:40 +0800 Subject: [PATCH 4/9] Putting the todo comments into javadoc. --- src/main/java/org/takes/rs/RsWithHeaders.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/takes/rs/RsWithHeaders.java b/src/main/java/org/takes/rs/RsWithHeaders.java index 049957db1..60eefa41d 100644 --- a/src/main/java/org/takes/rs/RsWithHeaders.java +++ b/src/main/java/org/takes/rs/RsWithHeaders.java @@ -52,12 +52,12 @@ public RsWithHeaders(final Response res, final CharSequence... headers) { this(res, Arrays.asList(headers)); } - //@todo #160:DEV To implement the concatenation and - // transformation with conjunction of Concat class - // and Transform class to get rid of the use of List.add() - // in the anonymous Response class head() nethod. /** * Ctor. + * @todo #160:DEV To implement the concatenation and + * transformation with conjunction of Concat class + * and Transform class to get rid of the use of List.add() + * in the anonymous Response class head() method. * @param res Original response * @param headers Headers */ From 2a4b1d642ef7249efcf8300faaeae766c97547f3 Mon Sep 17 00:00:00 2001 From: super132 Date: Sat, 2 May 2015 11:21:36 +0800 Subject: [PATCH 5/9] Fixing a javadoc. --- 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 61876552d..0eba74712 100644 --- a/src/main/java/org/takes/misc/Transform.java +++ b/src/main/java/org/takes/misc/Transform.java @@ -45,7 +45,7 @@ public class Transform implements Iterable { private final transient TransformAction action; /** - * Transform elements in the supplied iterable by the action supplied. + * Ctor. * @param itb Iterable to be transformed * @param act The actual transformation implementation */ From 8fdedf0156b091e9b50aa3975852e23c3c6c834a Mon Sep 17 00:00:00 2001 From: super132 Date: Sat, 2 May 2015 11:37:50 +0800 Subject: [PATCH 6/9] Fixing minor javadoc issues --- src/main/java/org/takes/misc/Transform.java | 2 +- src/main/java/org/takes/misc/TransformAction.java | 2 +- src/test/java/org/takes/misc/TransformTest.java | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/takes/misc/Transform.java b/src/main/java/org/takes/misc/Transform.java index 0eba74712..cac798597 100644 --- a/src/main/java/org/takes/misc/Transform.java +++ b/src/main/java/org/takes/misc/Transform.java @@ -30,7 +30,7 @@ * * @author Jason Wong (super132j@yahoo.com) * @version $Id$ - * @since 0.13.8 + * @since 0.15.2 */ public class Transform implements Iterable { diff --git a/src/main/java/org/takes/misc/TransformAction.java b/src/main/java/org/takes/misc/TransformAction.java index 1755762b5..bdc198cb6 100644 --- a/src/main/java/org/takes/misc/TransformAction.java +++ b/src/main/java/org/takes/misc/TransformAction.java @@ -28,7 +28,7 @@ * * @author Jason Wong (super132j@yahoo.com) * @version $Id$ - * @since 0.13.8 + * @since 0.15.2 */ public interface TransformAction { /** diff --git a/src/test/java/org/takes/misc/TransformTest.java b/src/test/java/org/takes/misc/TransformTest.java index b7a3930bd..ae079babf 100644 --- a/src/test/java/org/takes/misc/TransformTest.java +++ b/src/test/java/org/takes/misc/TransformTest.java @@ -34,7 +34,7 @@ * * @author Jason Wong (super132j@yahoo.com) * @version $Id$ - * @since 0.13.8 + * @since 0.15.2 */ public final class TransformTest { @@ -42,7 +42,7 @@ public final class TransformTest { * Transform can transform list. */ @Test - public void transformList() { + public void transformsList() { final List alist = new ArrayList(3); alist.add("a1"); alist.add("b1"); From 73ca774ea0de2c7c72cb9978b3372b110a7f7241 Mon Sep 17 00:00:00 2001 From: super132 Date: Sat, 2 May 2015 15:26:10 +0800 Subject: [PATCH 7/9] Updated unit test cases --- .../java/org/takes/misc/TransformTest.java | 26 +++++++++---------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/src/test/java/org/takes/misc/TransformTest.java b/src/test/java/org/takes/misc/TransformTest.java index ae079babf..bc9c8fd06 100644 --- a/src/test/java/org/takes/misc/TransformTest.java +++ b/src/test/java/org/takes/misc/TransformTest.java @@ -23,8 +23,8 @@ */ package org.takes.misc; -import java.util.ArrayList; -import java.util.List; +import com.google.common.base.Joiner; +import java.util.Arrays; import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; import org.junit.Test; @@ -43,21 +43,19 @@ public final class TransformTest { */ @Test public void transformsList() { - final List alist = new ArrayList(3); - alist.add("a1"); - alist.add("b1"); - alist.add("c1"); MatcherAssert.assertThat( - new Transform( - alist, - new TransformAction() { - @Override - public String transform(final String element) { - return element.concat("t"); + Joiner.on(" ").join( + new Transform( + Arrays.asList("one", "two", "three"), + new TransformAction() { + @Override + public String transform(final String element) { + return element.concat("t"); + } } - } + ) ), - Matchers.hasItems("a1t", "b1t", "c1t") + Matchers.equalTo("onet twot threet") ); } From fa432a7fd60de7cb8ef5d5ab2196c915315c2ed0 Mon Sep 17 00:00:00 2001 From: super132 Date: Mon, 4 May 2015 11:15:43 +0800 Subject: [PATCH 8/9] Updated puzzle comments to have more comprehensive description. --- src/main/java/org/takes/rs/RsWithHeaders.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/takes/rs/RsWithHeaders.java b/src/main/java/org/takes/rs/RsWithHeaders.java index 60eefa41d..e49e9cdde 100644 --- a/src/main/java/org/takes/rs/RsWithHeaders.java +++ b/src/main/java/org/takes/rs/RsWithHeaders.java @@ -57,7 +57,9 @@ public RsWithHeaders(final Response res, final CharSequence... headers) { * @todo #160:DEV To implement the concatenation and * transformation with conjunction of Concat class * and Transform class to get rid of the use of List.add() - * in the anonymous Response class head() method. + * in the anonymous Response class head() method. That is, + * we are going to do something like this: + * return new Concat(res.head(), new Transform(headers, new Trim())) * @param res Original response * @param headers Headers */ From 8153eeda76e306e8bb9b4391fad16d4bef23b5f9 Mon Sep 17 00:00:00 2001 From: super132 Date: Fri, 8 May 2015 09:50:28 +0800 Subject: [PATCH 9/9] Updated puzzle comment by adding time budget. --- src/main/java/org/takes/rs/RsWithHeaders.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/takes/rs/RsWithHeaders.java b/src/main/java/org/takes/rs/RsWithHeaders.java index e49e9cdde..538982d4e 100644 --- a/src/main/java/org/takes/rs/RsWithHeaders.java +++ b/src/main/java/org/takes/rs/RsWithHeaders.java @@ -54,7 +54,7 @@ public RsWithHeaders(final Response res, final CharSequence... headers) { /** * Ctor. - * @todo #160:DEV To implement the concatenation and + * @todo #160:30min/DEV To implement the concatenation and * transformation with conjunction of Concat class * and Transform class to get rid of the use of List.add() * in the anonymous Response class head() method. That is,