Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
506 additions
and
0 deletions.
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,73 @@ | ||
/* | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) 2017-2019 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.cactoos.collection; | ||
|
||
import java.util.Collection; | ||
import org.cactoos.iterable.IterableOf; | ||
|
||
/** | ||
* Sliced portion of the collection. | ||
* | ||
* <p>There is no thread-safety guarantee. | ||
* | ||
* @param <T> Element type | ||
* @since 1.0.0 | ||
*/ | ||
public final class Sliced<T> extends CollectionEnvelope<T> { | ||
|
||
/** | ||
* Ctor. | ||
* @param start Starting index | ||
* @param count Maximum number of elements for resulted iterator | ||
* @param items Items | ||
*/ | ||
@SafeVarargs | ||
public Sliced(final int start, final int count, final T... items) { | ||
this(start, count, new IterableOf<>(items)); | ||
} | ||
|
||
/** | ||
* Ctor. | ||
* @param start Starting index | ||
* @param count Maximum number of elements for resulted iterator | ||
* @param iterable Source iterable | ||
*/ | ||
public Sliced(final int start, final int count, | ||
final Iterable<T> iterable) { | ||
this(start, count, new CollectionOf<T>(iterable)); | ||
} | ||
|
||
/** | ||
* Ctor. | ||
* @param start Starting index | ||
* @param count Maximum number of elements for resulted iterator | ||
* @param collection Source collection | ||
*/ | ||
public Sliced(final int start, final int count, | ||
final Collection<T> collection) { | ||
super(() -> new CollectionOf<T>( | ||
new org.cactoos.iterable.Sliced<T>(start, count, collection) | ||
)); | ||
} | ||
} |
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,65 @@ | ||
/* | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) 2017-2019 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.cactoos.iterable; | ||
|
||
/** | ||
* Sliced portion of the iterable. | ||
* | ||
* <p>There is no thread-safety guarantee.</p> | ||
* | ||
* @param <T> Element type | ||
* @since 1.0.0 | ||
*/ | ||
public final class Sliced<T> extends IterableEnvelope<T> { | ||
|
||
/** | ||
* Ctor. | ||
* @param start Starting index | ||
* @param count Maximum number of elements for resulted iterator | ||
* @param items Varargs items | ||
*/ | ||
@SafeVarargs | ||
public Sliced(final int start, final int count, final T... items) { | ||
this(start, count, new IterableOf<>(items)); | ||
} | ||
|
||
/** | ||
* Ctor. | ||
* @param start Starting index | ||
* @param count Maximum number of elements for resulted iterator | ||
* @param iterable Decorated iterable | ||
*/ | ||
public Sliced(final int start, final int count, | ||
final Iterable<T> iterable) { | ||
super( | ||
new IterableOf<>( | ||
() -> new org.cactoos.iterator.Sliced<>( | ||
start, | ||
count, | ||
iterable.iterator() | ||
) | ||
) | ||
); | ||
} | ||
} |
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
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,127 @@ | ||
/* | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) 2017-2019 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.cactoos.iterator; | ||
|
||
import java.util.Iterator; | ||
import java.util.NoSuchElementException; | ||
import java.util.function.IntPredicate; | ||
|
||
/** | ||
* Creates an iterator returning an interval(slice) of the original iterator | ||
* by means of providing starting index, number of elements to retrieve from | ||
* the starting index and a decorated original iterator. | ||
* Can be used to retrieve the head, tail or a portion in the middle of the | ||
* original iterator. | ||
* | ||
* <p>There is no thread-safety guarantee.</p> | ||
* @param <T> The type of the iterator. | ||
* @since 1.0.0 | ||
* @todo #1188:30min This class to be refactored by extending IteratorEnvelope, | ||
* which will provide extra functionality to subclasses and is planned to be | ||
* added to Cactoos shortly. | ||
*/ | ||
public final class Sliced<T> implements Iterator<T> { | ||
|
||
/** | ||
* Decorated iterator. | ||
*/ | ||
private final Iterator<T> iterator; | ||
|
||
/** | ||
* First index of the resulted iterator. | ||
*/ | ||
private final int start; | ||
|
||
/** | ||
* Current index on the original iterator. | ||
*/ | ||
private int current; | ||
|
||
/** | ||
* Predicate which tests whether the end has been reached. | ||
*/ | ||
private IntPredicate end; | ||
|
||
/** | ||
* Constructor. | ||
* @param start Starting index | ||
* @param count Maximum number of elements for resulted iterator | ||
* @param iterator Decorated iterator | ||
*/ | ||
public Sliced(final int start, final int count, | ||
final Iterator<T> iterator) { | ||
this(start, index -> index > start + count - 1, iterator); | ||
} | ||
|
||
/** | ||
* Constructor. | ||
* Constructs an iterator of start position and up to the end | ||
* @param start Starting index | ||
* @param iterator Decorated iterator | ||
*/ | ||
public Sliced(final int start, final Iterator<T> iterator) { | ||
this(start, any -> !iterator.hasNext(), iterator); | ||
} | ||
|
||
/** | ||
* Constructor. | ||
* @param start Starting index | ||
* @param end Predicate that test whether iterating should stop | ||
* @param iterator Decorated iterator | ||
*/ | ||
private Sliced(final int start, final IntPredicate end, | ||
final Iterator<T> iterator) { | ||
this.start = start; | ||
this.end = end; | ||
this.iterator = iterator; | ||
this.current = 0; | ||
} | ||
|
||
@Override | ||
public boolean hasNext() { | ||
this.skip(); | ||
return !this.end.test(this.current) && this.iterator.hasNext(); | ||
} | ||
|
||
@Override | ||
public T next() { | ||
if (!this.hasNext()) { | ||
throw new NoSuchElementException( | ||
"The iterator doesn't have items any more" | ||
); | ||
} | ||
++this.current; | ||
return this.iterator.next(); | ||
} | ||
|
||
/** | ||
* Skips head elements up to start index. | ||
*/ | ||
private void skip() { | ||
while (this.current < this.start && this.iterator.hasNext()) { | ||
this.iterator.next(); | ||
++this.current; | ||
} | ||
} | ||
} |
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,55 @@ | ||
/* | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) 2017-2019 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.cactoos.collection; | ||
|
||
import org.hamcrest.core.IsEqual; | ||
import org.junit.Test; | ||
import org.llorllale.cactoos.matchers.Assertion; | ||
|
||
/** | ||
* Test case for {@link Sliced}. | ||
* @since 1.0.0 | ||
* @checkstyle JavadocMethodCheck (500 lines) | ||
* @checkstyle MagicNumberCheck (500 lines) | ||
*/ | ||
public final class SlicedTest { | ||
|
||
@Test | ||
@SuppressWarnings("PMD.AvoidDuplicateLiterals") | ||
public void sliceCollection() { | ||
new Assertion<>( | ||
"Must get sliced collection", | ||
new Sliced<>( | ||
2, | ||
2, | ||
"one", "two", "three", "four" | ||
), | ||
new IsEqual<>( | ||
new CollectionOf<>( | ||
"three", "four" | ||
) | ||
) | ||
).affirm(); | ||
} | ||
} |
Oops, something went wrong.
903dd2f
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.
Puzzle
1188-96768eb2
discovered insrc/main/java/org/cactoos/iterator/Partitioned.java
and submitted as #1191. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.903dd2f
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.
Puzzle
1188-555ede32
discovered insrc/main/java/org/cactoos/iterator/Skipped.java
and submitted as #1192. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.903dd2f
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.
Puzzle
1188-ace6cee0
discovered insrc/main/java/org/cactoos/iterator/Sliced.java
and submitted as #1193. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.903dd2f
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.
Puzzle
1188-895eb6a4
discovered insrc/main/java/org/cactoos/iterator/HeadOf.java
and submitted as #1194. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.