Skip to content

Commit

Permalink
#85 StickyIterator
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Jun 21, 2017
1 parent 3f42592 commit 6a27707
Show file tree
Hide file tree
Showing 4 changed files with 165 additions and 3 deletions.
22 changes: 20 additions & 2 deletions src/main/java/org/cactoos/list/EndlessIterator.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
package org.cactoos.list;

import java.util.Iterator;
import org.cactoos.Scalar;
import org.cactoos.func.UncheckedScalar;

/**
* Iterator that never ends.
Expand All @@ -41,13 +43,29 @@ public final class EndlessIterator<T> implements Iterator<T> {
/**
* The element to repeat.
*/
private final T element;
private final UncheckedScalar<T> element;

/**
* Ctor.
* @param elm Element to repeat
*/
public EndlessIterator(final T elm) {
this(() -> elm);
}

/**
* Ctor.
* @param elm Element to repeat
*/
public EndlessIterator(final Scalar<T> elm) {
this(new UncheckedScalar<>(elm));
}

/**
* Ctor.
* @param elm Element to repeat
*/
public EndlessIterator(final UncheckedScalar<T> elm) {
this.element = elm;
}

Expand All @@ -58,6 +76,6 @@ public boolean hasNext() {

@Override
public T next() {
return this.element;
return this.element.asValue();
}
}
2 changes: 1 addition & 1 deletion src/main/java/org/cactoos/list/StickyIterable.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import org.cactoos.func.UncheckedScalar;

/**
* Iterable that returns an iterator only once.
* Iterable that returns the same set of elements, always.
*
* <p>There is no thread-safety guarantee.
*
Expand Down
76 changes: 76 additions & 0 deletions src/main/java/org/cactoos/list/StickyIterator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/**
* 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.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import org.cactoos.func.StickyScalar;
import org.cactoos.func.UncheckedScalar;

/**
* Iterator that returns the same set of elements always.
*
* <p>There is no thread-safety guarantee.
*
* @author Yegor Bugayenko (yegor256@gmail.com)
* @version $Id$
* @param <X> Type of item
* @since 0.8
*/
public final class StickyIterator<X> implements Iterator<X> {

/**
* The gate.
*/
private final UncheckedScalar<Iterator<X>> gate;

/**
* Ctor.
* @param src The iterable
*/
public StickyIterator(final Iterator<X> src) {
this.gate = new UncheckedScalar<>(
new StickyScalar<>(
() -> {
final Collection<X> temp = new LinkedList<>();
while (src.hasNext()) {
temp.add(src.next());
}
return temp.iterator();
}
)
);
}

@Override
public boolean hasNext() {
return this.gate.asValue().hasNext();
}

@Override
public X next() {
return this.gate.asValue().next();
}
}
68 changes: 68 additions & 0 deletions src/test/java/org/cactoos/list/StickyIteratorTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/**
* 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.concurrent.atomic.AtomicInteger;
import org.cactoos.Text;
import org.cactoos.TextHasString;
import org.cactoos.text.FormattedText;
import org.hamcrest.MatcherAssert;
import org.junit.Test;

/**
* Test case for {@link StickyIterator}.
*
* @author Yegor Bugayenko (yegor256@gmail.com)
* @version $Id$
* @since 0.8
* @checkstyle JavadocMethodCheck (500 lines)
*/
public final class StickyIteratorTest {

@Test
public void ignoresChangesInIterable() throws Exception {
final AtomicInteger count = new AtomicInteger(2);
final Text text = new FormattedText(
"%s",
String.join(
", ",
() -> new MappedIterator<>(
new StickyIterator<>(
new LimitedIterator<>(
new EndlessIterator<>(count::incrementAndGet),
2
)
),
Object::toString
)
)
);
MatcherAssert.assertThat(
"Can't ignore the changes in the underlying iterator",
text,
new TextHasString(text.asString())
);
}

}

0 comments on commit 6a27707

Please sign in to comment.