Skip to content

Commit

Permalink
Merge branch '__rultor'
Browse files Browse the repository at this point in the history
  • Loading branch information
rultor committed Sep 25, 2019
2 parents f43537b + c0a680f commit 903dd2f
Show file tree
Hide file tree
Showing 9 changed files with 506 additions and 0 deletions.
73 changes: 73 additions & 0 deletions src/main/java/org/cactoos/collection/Sliced.java
@@ -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)
));
}
}
65 changes: 65 additions & 0 deletions src/main/java/org/cactoos/iterable/Sliced.java
@@ -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()
)
)
);
}
}
3 changes: 3 additions & 0 deletions src/main/java/org/cactoos/iterator/HeadOf.java
Expand Up @@ -33,6 +33,9 @@
*
* @param <T> Element type
* @since 0.8
* @todo #1188:30min Change the implementation of this class with the help of
* <tt>org.cactoos.iterator.Sliced</tt> decorator, as it contains specific
* constructor to build heading iterator.
*/
public final class HeadOf<T> implements Iterator<T> {

Expand Down
3 changes: 3 additions & 0 deletions src/main/java/org/cactoos/iterator/Partitioned.java
Expand Up @@ -34,6 +34,9 @@
*
* @param <T> Partitions value type
* @since 0.29
* @todo #1188:30min Change the implementation of this class with the help of
* <tt>org.cactoos.iterator.Sliced</tt> decorator, to slice the original
* iterator into sliced portions.
*/
public final class Partitioned<T> implements Iterator<List<T>> {

Expand Down
3 changes: 3 additions & 0 deletions src/main/java/org/cactoos/iterator/Skipped.java
Expand Up @@ -33,6 +33,9 @@
*
* @param <T> Element type
* @since 0.34
* @todo #1188:30min Change the implementation of this class with the help of
* <tt>org.cactoos.iterator.Sliced</tt> decorator, as it contains specific
* constructor to build skipped iterator.
*/
public final class Skipped<T> implements Iterator<T> {

Expand Down
127 changes: 127 additions & 0 deletions src/main/java/org/cactoos/iterator/Sliced.java
@@ -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;
}
}
}
55 changes: 55 additions & 0 deletions src/test/java/org/cactoos/collection/SlicedTest.java
@@ -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();
}
}

4 comments on commit 903dd2f

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on 903dd2f Sep 25, 2019

Choose a reason for hiding this comment

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

Puzzle 1188-96768eb2 discovered in src/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.

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on 903dd2f Sep 25, 2019

Choose a reason for hiding this comment

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

Puzzle 1188-555ede32 discovered in src/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.

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on 903dd2f Sep 25, 2019

Choose a reason for hiding this comment

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

Puzzle 1188-ace6cee0 discovered in src/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.

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on 903dd2f Sep 25, 2019

Choose a reason for hiding this comment

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

Puzzle 1188-895eb6a4 discovered in src/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.

Please sign in to comment.