From b99858db61a2d49b741cd8d1dd6ab7d6349b8d5a Mon Sep 17 00:00:00 2001 From: Tim Hinkes Date: Sat, 10 Jun 2017 18:37:49 +0200 Subject: [PATCH 1/2] Refactored IterableAsList to remove unnecessary call to size() and remove not working cache no more calls to size() if not needed. Modifications in the underlying Iterable would lead to inconsistent cache. Removed cache completely. Caches do not work for modifyable Iterables. --- README.md | 1 + .../java/org/cactoos/list/IterableAsList.java | 49 ++++++------------- 2 files changed, 17 insertions(+), 33 deletions(-) diff --git a/README.md b/README.md index 9111c559a7..ec628cdba4 100644 --- a/README.md +++ b/README.md @@ -252,6 +252,7 @@ Note: [Checkstyle](https://en.wikipedia.org/wiki/Checkstyle) is used as a static - [Andriy Kryvtsun](https://github.com/englishman) - [Vseslav Sekorin](https://github.com/VsSekorin) - [Andrey Valyaev](https://github.com/DronMDF) + - [Tim Hinkes](https://github.com/timmeey) [Blog](https://blog.timmeey.de) ## License (MIT) diff --git a/src/main/java/org/cactoos/list/IterableAsList.java b/src/main/java/org/cactoos/list/IterableAsList.java index a76c176eb9..1c5d6426af 100644 --- a/src/main/java/org/cactoos/list/IterableAsList.java +++ b/src/main/java/org/cactoos/list/IterableAsList.java @@ -24,7 +24,6 @@ package org.cactoos.list; import java.util.AbstractList; -import java.util.ArrayList; import java.util.List; import org.cactoos.func.StickyScalar; import org.cactoos.func.UncheckedScalar; @@ -48,11 +47,6 @@ public final class IterableAsList extends AbstractList { */ private final Iterable source; - /** - * Cache for source. - */ - private final List cache; - /** * Iterable length. */ @@ -66,7 +60,6 @@ public final class IterableAsList extends AbstractList { public IterableAsList(final Iterable iterable) { super(); this.source = iterable; - this.cache = new ArrayList<>(0); this.length = new UncheckedScalar<>( new StickyScalar<>( new LengthOfIterable(iterable) @@ -76,19 +69,23 @@ public IterableAsList(final Iterable iterable) { @Override public T get(final int index) { - if (index < 0 || index >= this.size()) { - throw new IndexOutOfBoundsException( - new UncheckedText( - new FormattedText( - "index=%d, bounds=[%d; %d]", - index, - 0, - this.size() - ) - ).asString() - ); + int position = 0; + for (final T elem : this.source) { + if (position == index) { + return elem; + } + position += 1; } - return this.cachedItem(index); + throw new IndexOutOfBoundsException( + new UncheckedText( + new FormattedText( + "index=%d, bounds=[%d; %d]", + index, + 0, + this.size() + ) + ).asString() + ); } @Override @@ -96,18 +93,4 @@ public int size() { return this.length.asValue(); } - /** - * Find item in cache by index. - * - * @param index Item index - * @return Cached item - */ - private T cachedItem(final int index) { - if (this.cache.size() != this.size()) { - for (final T item : this.source) { - this.cache.add(item); - } - } - return this.cache.get(index); - } } From da64c705b53f3e92c378d5ad2a204692cc52a243 Mon Sep 17 00:00:00 2001 From: Tim Hinkes Date: Sat, 10 Jun 2017 18:54:20 +0200 Subject: [PATCH 2/2] Switched to non Cached size Since size() is not called all the time, and the premise is a mutable iterable, the size() can not be cached. --- src/main/java/org/cactoos/list/IterableAsList.java | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/main/java/org/cactoos/list/IterableAsList.java b/src/main/java/org/cactoos/list/IterableAsList.java index 1c5d6426af..8dd9c642d1 100644 --- a/src/main/java/org/cactoos/list/IterableAsList.java +++ b/src/main/java/org/cactoos/list/IterableAsList.java @@ -25,7 +25,6 @@ import java.util.AbstractList; import java.util.List; -import org.cactoos.func.StickyScalar; import org.cactoos.func.UncheckedScalar; import org.cactoos.text.FormattedText; import org.cactoos.text.UncheckedText; @@ -61,9 +60,7 @@ public IterableAsList(final Iterable iterable) { super(); this.source = iterable; this.length = new UncheckedScalar<>( - new StickyScalar<>( - new LengthOfIterable(iterable) - ) + new LengthOfIterable(iterable) ); }