Skip to content

Commit

Permalink
#114 StickyIterable
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Jun 21, 2017
1 parent 972a69e commit 3f42592
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 4 deletions.
21 changes: 17 additions & 4 deletions src/main/java/org/cactoos/list/StickyIterable.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@
*/
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;

/**
Expand All @@ -39,21 +42,31 @@
public final class StickyIterable<X> implements Iterable<X> {

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

/**
* Ctor.
* @param iterable The iterable
*/
public StickyIterable(final Iterable<X> iterable) {
this.cache = new UncheckedScalar<>(iterable::iterator);
this.gate = new UncheckedScalar<>(
new StickyScalar<>(
() -> {
final Collection<X> temp = new LinkedList<>();
for (final X item : iterable) {
temp.add(item);
}
return temp;
}
)
);
}

@Override
public Iterator<X> iterator() {
return this.cache.asValue();
return this.gate.asValue().iterator();
}

}
57 changes: 57 additions & 0 deletions src/test/java/org/cactoos/list/StickyIterableTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/**
* 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.concurrent.atomic.AtomicInteger;
import org.cactoos.ScalarHasValue;
import org.hamcrest.MatcherAssert;
import org.junit.Test;

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

@Test
public void ignoresChangesInIterable() throws Exception {
final AtomicInteger size = new AtomicInteger(2);
final Iterable<Integer> list = new StickyIterable<>(
new IterableAsList<>(
() -> Collections.nCopies(size.incrementAndGet(), 0).iterator()
)
);
MatcherAssert.assertThat(
"Can't ignore the changes in the underlying iterable",
new LengthOfIterable(list),
new ScalarHasValue<>(new LengthOfIterable(list).asValue())
);
}

}

0 comments on commit 3f42592

Please sign in to comment.