-
Notifications
You must be signed in to change notification settings - Fork 203
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
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
1afddf9
Introducing Transform for changing iterables in RsWithHeaders
super132 84f3a37
Updating javadoc
super132 fe6a251
Updating todo comments to fix check styles error
super132 04919af
Putting the todo comments into javadoc.
super132 1073038
Merge remote-tracking branch 'origin/master' into iterable-transform
super132 2a4b1d6
Fixing a javadoc.
super132 8fdedf0
Fixing minor javadoc issues
super132 73ca774
Updated unit test cases
super132 fa432a7
Updated puzzle comments to have more comprehensive description.
super132 8153eed
Updated puzzle comment by adding time budget.
super132 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
); | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@super132 please add an estimation here.