Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removed calls to size() and cache from IterableAsList #105

Merged
merged 3 commits into from
Jun 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,8 @@ Note: [Checkstyle](https://en.wikipedia.org/wiki/Checkstyle) is used as a static
- [Vseslav Sekorin](https://github.com/VsSekorin)
- [Andrey Valyaev](https://github.com/DronMDF)
- [Dušan Rychnovský](https://github.com/dusan-rychnovsky) [Blog](http://blog.dusanrychnovsky.cz/)
- [Tim Hinkes](https://github.com/timmeey) [Blog](https://blog.timmeey.de)


## License (MIT)

Expand Down
54 changes: 17 additions & 37 deletions src/main/java/org/cactoos/list/IterableAsList.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@
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;
import org.cactoos.text.FormattedText;
import org.cactoos.text.UncheckedText;
Expand All @@ -48,11 +46,6 @@ public final class IterableAsList<T> extends AbstractList<T> {
*/
private final Iterable<T> source;

/**
* Cache for source.
*/
private final List<T> cache;

/**
* Iterable length.
*/
Expand All @@ -66,48 +59,35 @@ public final class IterableAsList<T> extends AbstractList<T> {
public IterableAsList(final Iterable<T> iterable) {
super();
this.source = iterable;
this.cache = new ArrayList<>(0);
this.length = new UncheckedScalar<>(
new StickyScalar<>(
new LengthOfIterable(iterable)
)
new LengthOfIterable(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
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);
}
}