Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#160 let's get rid of List.add() - Part 3 #234

Merged
merged 10 commits into from
May 8, 2015
109 changes: 109 additions & 0 deletions src/main/java/org/takes/misc/Transform.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/**
* 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 ([email protected])
* @version $Id$
* @since 0.13.8
*/
public class Transform<T, K> implements Iterable<K> {

/**
* Internal storage.
*/
private final transient Iterable<T> list;

/**
* The action to transform the elements in the iterator.
*/
private final transient TransformAction<T, K> 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<T> itb,
final TransformAction<T, K> act) {
this.list = itb;
this.action = act;
}

@Override
public final Iterator<K> iterator() {
return new TransformIterator<T, K>(this.list.iterator(), this.action);
}

/**
* The iterator to iterator through the original type B and return the
* transformed element in type A.
*
* <p>This class is NOT thread-safe.
*/
private static class TransformIterator<B, A> implements Iterator<A> {

/**
* The iterator to reflect the traverse state.
*/
private final transient Iterator<B> iterator;

/**
* The action to transform the elements in the iterator.
*/
private final transient TransformAction<B, A> action;

/**
* Ctor. ConcatIterator traverses the element.
* @param itr Iterator of the original iterable
* @param act Action to transform elements
*/
public TransformIterator(final Iterator<B> itr,
final TransformAction<B, A> 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"
);
}
}
}
63 changes: 63 additions & 0 deletions src/main/java/org/takes/misc/TransformAction.java
Original file line number Diff line number Diff line change
@@ -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 ([email protected])
* @version $Id$
* @since 0.13.8
*/
public interface TransformAction<T, K> {
/**
* 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<String, String> {

@Override
public String transform(final String element) {
return element.trim();
}
}

/**
* Convert CharSequence into String.
*/
class ToString implements TransformAction<CharSequence, String> {

@Override
public String transform(final CharSequence element) {
return element.toString();
}

}
}
4 changes: 4 additions & 0 deletions src/main/java/org/takes/rs/RsWithHeaders.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@super132 let's move this into the javadoc below

// 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
Expand Down
64 changes: 64 additions & 0 deletions src/test/java/org/takes/misc/TransformTest.java
Original file line number Diff line number Diff line change
@@ -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 ([email protected])
* @version $Id$
* @since 0.13.8
*/
public final class TransformTest {

/**
* Transform can transform list.
*/
@Test
public void transformList() {
final List<String> alist = new ArrayList<String>(3);
alist.add("a1");
alist.add("b1");
alist.add("c1");
MatcherAssert.assertThat(
new Transform<String, String>(
alist,
new TransformAction<String, String>() {
@Override
public String transform(final String element) {
return element.concat("t");
}
}
),
Matchers.hasItems("a1t", "b1t", "c1t")
);
}

}