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.15.2
*/
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;

/**
* Ctor.
* @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.15.2
*/
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();
}

}
}
6 changes: 6 additions & 0 deletions src/main/java/org/takes/rs/RsWithHeaders.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ public RsWithHeaders(final Response res, final CharSequence... headers) {

/**
* Ctor.
* @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,
* 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
*/
Expand Down
62 changes: 62 additions & 0 deletions src/test/java/org/takes/misc/TransformTest.java
Original file line number Diff line number Diff line change
@@ -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 com.google.common.base.Joiner;
import java.util.Arrays;
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.15.2
*/
public final class TransformTest {

/**
* Transform can transform list.
*/
@Test
public void transformsList() {
MatcherAssert.assertThat(
Joiner.on(" ").join(
new Transform<String, String>(
Arrays.asList("one", "two", "three"),
new TransformAction<String, String>() {
@Override
public String transform(final String element) {
return element.concat("t");
}
}
)
),
Matchers.equalTo("onet twot threet")
);
}

}