Skip to content

Commit

Permalink
Merge branch '__rultor'
Browse files Browse the repository at this point in the history
  • Loading branch information
rultor committed Jun 20, 2017
2 parents 696aa04 + 9b4868a commit 972a69e
Show file tree
Hide file tree
Showing 4 changed files with 272 additions and 0 deletions.
58 changes: 58 additions & 0 deletions src/main/java/org/cactoos/list/CycledIterable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2017 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.list;

import java.util.Iterator;

/**
* Cycled Iterable.
*
* <p>There is no thread-safety guarantee.
*
* @author Ilia Rogozhin (ilia.rogozhin@gmail.com)
* @version $Id$
* @param <T> Type of item
* @since 0.8
*/
public final class CycledIterable<T> implements Iterable<T> {

/**
* Iterable.
*/
private final Iterable<T> iterable;

/**
* Ctor.
* @param iterable Iterable
*/
public CycledIterable(final Iterable<T> iterable) {
this.iterable = iterable;
}

@Override
public Iterator<T> iterator() {
return new CycledIterator<>(this.iterable);
}
}
75 changes: 75 additions & 0 deletions src/main/java/org/cactoos/list/CycledIterator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2017 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.list;

import java.util.Iterator;
import java.util.NoSuchElementException;

/**
* Cycled Iterator.
*
* <p>There is no thread-safety guarantee.
*
* @author Ilia Rogozhin (ilia.rogozhin@gmail.com)
* @version $Id$
* @param <T> Type of item
* @since 0.8
*/
public final class CycledIterator<T> implements Iterator<T> {

/**
* Iterable.
*/
private final Iterable<T> iterable;

/**
* Iterator.
*/
private Iterator<T> iterator;

/**
* Ctor.
* @param iterable Iterable.
*/
public CycledIterator(final Iterable<T> iterable) {
this.iterable = iterable;
}

@Override
public boolean hasNext() {
if (this.iterator == null || !this.iterator.hasNext()) {
this.iterator = this.iterable.iterator();
}
return this.iterator.hasNext();
}

@Override
public T next() {
if (!this.hasNext()) {
throw new NoSuchElementException();
}
return this.iterator.next();
}
}
72 changes: 72 additions & 0 deletions src/test/java/org/cactoos/list/CycledIterableTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2017 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.list;

import java.util.Collections;
import org.cactoos.ScalarHasValue;
import org.hamcrest.MatcherAssert;
import org.junit.Test;

/**
* Test Case for {@link CycledIterable}.
* @author Ilia Rogozhin (ilia.rogozhin@gmail.com)
* @version $Id$
* @since 0.8
* @checkstyle JavadocMethodCheck (500 lines)
*/
public final class CycledIterableTest {

@Test
public void repeatIterableTest() throws Exception {
final String expected = "two";
MatcherAssert.assertThat(
"Can't repeat iterable",
new ItemOfIterable<>(
new CycledIterable<>(
new ArrayAsIterable<>(
"one", expected, "three"
)
),
// @checkstyle MagicNumberCheck (1 line)<
7
),
new ScalarHasValue<>(
expected
)
);
}

@Test()
public void notCycledEmptyTest() throws Exception {
MatcherAssert.assertThat(
"Can't generate an empty iterable",
new LengthOfIterable(
new CycledIterable<>(
Collections::emptyIterator
)
),
new ScalarHasValue<>(0)
);
}
}
67 changes: 67 additions & 0 deletions src/test/java/org/cactoos/list/CycledIteratorTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2017 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.list;

import java.util.Collections;
import java.util.NoSuchElementException;
import org.cactoos.ScalarHasValue;
import org.hamcrest.MatcherAssert;
import org.junit.Test;

/**
* Test Case for {@link CycledIterator}.
* @author Ilia Rogozhin (ilia.rogozhin@gmail.com)
* @version $Id$
* @since 0.8
* @checkstyle JavadocMethodCheck (500 lines)
*/
public final class CycledIteratorTest {

@Test
public void repeatIteratorTest() throws Exception {
final String expected = "two";
MatcherAssert.assertThat(
"Can't repeat iterator",
new ItemOfIterator<>(
new CycledIterator<>(
new ArrayAsIterable<>(
"one", expected, "three"
)
),
// @checkstyle MagicNumberCheck (1 line)
7
),
new ScalarHasValue<>(
expected
)
);
}

@Test(expected = NoSuchElementException.class)
public void notCycledEmptyTest() throws Exception {
new CycledIterator<>(
Collections::emptyIterator
).next();
}
}

0 comments on commit 972a69e

Please sign in to comment.