Skip to content

Commit

Permalink
Optimize IterableAsList yegor256#157
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-semenyuk committed Jun 19, 2017
1 parent a12bc61 commit b83c546
Showing 1 changed file with 148 additions and 33 deletions.
181 changes: 148 additions & 33 deletions src/main/java/org/cactoos/list/IterableAsList.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,33 +23,32 @@
*/
package org.cactoos.list;

import java.util.AbstractList;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import org.cactoos.func.StickyScalar;
import org.cactoos.func.UncheckedScalar;
import org.cactoos.text.FormattedText;
import org.cactoos.text.UncheckedText;

/**
* Iterable as {@link List}.
*
* <p>There is no thread-safety guarantee.
*
* @author Alexey Semenyuk ([email protected])
* @author Kirill ([email protected])
* @version $Id$
* @param <T> List type
* @since 0.1
*/
public final class IterableAsList<T> extends AbstractList<T> {
@SuppressWarnings("PMD.TooManyMethods")
public final class IterableAsList<T> implements List<T> {

/**
* Iterable source.
* List source.
*/
private final Iterable<T> source;

/**
* Iterable length.
*/
private final UncheckedScalar<Integer> length;
private final UncheckedScalar<List<T>> list;

/**
* Ctor.
Expand All @@ -68,37 +67,153 @@ public IterableAsList(final T... array) {
* @param iterable An {@link Iterable}
*/
public IterableAsList(final Iterable<T> iterable) {
super();
this.source = iterable;
this.length = new UncheckedScalar<>(
new LengthOfIterable(iterable)
this.list = new UncheckedScalar<>(
new StickyScalar<>(
() -> {
final List<T> temp = new ArrayList<>(0);
for (final T elem : iterable) {
temp.add(elem);
}
return temp;
}
)
);
}

@Override
public int size() {
return this.list.asValue().size();
}

@Override
public boolean isEmpty() {
return this.list.asValue().isEmpty();
}

@Override
public boolean contains(final Object object) {
return this.list.asValue().contains(object);
}

@Override
public Iterator<T> iterator() {
return this.list.asValue().iterator();
}

@Override
public Object[] toArray() {
return this.list.asValue().toArray();
}

@Override
@SuppressWarnings("PMD.UseVarargs")
public <Y> Y[] toArray(final Y[] array) {
return this.list.asValue().toArray(array);
}

@Override
public boolean add(final T element) {
throw new UnsupportedOperationException(
"#add(final T element) is not supported"
);
}

@Override
public boolean remove(final Object object) {
throw new UnsupportedOperationException(
"#remove(final Object object) is not supported"
);
}

@Override
public boolean containsAll(final Collection<?> collection) {
return this.list.asValue().containsAll(collection);
}

@Override
public boolean addAll(final Collection<? extends T> collection) {
throw new UnsupportedOperationException(
"#addAll(final Collection<? extends T> collection) is not supported"
);
}

@Override
public boolean addAll(final int index,
final Collection<? extends T> collection) {
throw new UnsupportedOperationException(
"#addAll() is not supported"
);
}

@Override
public boolean removeAll(final Collection<?> collection) {
throw new UnsupportedOperationException(
"#removeAll(final Collection<?> collection) is not supported"
);
}

@Override
public boolean retainAll(final Collection<?> collection) {
throw new UnsupportedOperationException(
"#retainAll(final Collection<?> collection) is not supported"
);
}

@Override
public void clear() {
throw new UnsupportedOperationException(
"#clear() is not supported"
);
}

@Override
public T get(final int index) {
int position = 0;
for (final T elem : this.source) {
if (position == index) {
return elem;
}
position += 1;
}
throw new IndexOutOfBoundsException(
new UncheckedText(
new FormattedText(
"index=%d, bounds=[%d; %d]",
index,
0,
this.size()
)
).asString()
return this.list.asValue().get(index);
}

@Override
public T set(final int index, final T element) {
throw new UnsupportedOperationException(
"#set() is not supported"
);
}

@Override
public int size() {
return this.length.asValue();
public void add(final int index, final T element) {
throw new UnsupportedOperationException(
"#add(final int index, final T element) is not supported"
);
}

@Override
public T remove(final int index) {
throw new UnsupportedOperationException(
"#remove(final int index) is not supported"
);
}

@Override
public int indexOf(final Object object) {
return this.list.asValue().indexOf(object);
}

@Override
public int lastIndexOf(final Object object) {
return this.list.asValue().lastIndexOf(object);
}

@Override
public ListIterator<T> listIterator() {
return this.list.asValue().listIterator();
}

@Override
public ListIterator<T> listIterator(final int index) {
return this.list.asValue().listIterator(index);
}

@Override
public List<T> subList(final int fromindex, final int toindex) {
return this.list.asValue().subList(fromindex, toindex);
}
}

0 comments on commit b83c546

Please sign in to comment.