-
Notifications
You must be signed in to change notification settings - Fork 169
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
4 changed files
with
272 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,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); | ||
} | ||
} |
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,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(); | ||
} | ||
} |
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,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) | ||
); | ||
} | ||
} |
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,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(); | ||
} | ||
} |