Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/yegor256/cactoos into conj
Browse files Browse the repository at this point in the history
  • Loading branch information
vssekorin committed Jun 20, 2017
2 parents ebc18dd + 9f0c01c commit a7e44d0
Show file tree
Hide file tree
Showing 20 changed files with 1,186 additions and 92 deletions.
7 changes: 3 additions & 4 deletions src/main/java/org/cactoos/list/ArrayAsIterable.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

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

/**
* Array as iterable.
Expand All @@ -42,7 +41,7 @@ public final class ArrayAsIterable<X> implements Iterable<X> {
/**
* The array.
*/
private final UncheckedScalar<Iterable<X>> result;
private final X[] array;

/**
* Ctor.
Expand All @@ -51,12 +50,12 @@ public final class ArrayAsIterable<X> implements Iterable<X> {
@SafeVarargs
@SuppressWarnings("varargs")
public ArrayAsIterable(final X... items) {
this.result = new UncheckedScalar<>(() -> Arrays.asList(items));
this.array = items;
}

@Override
public Iterator<X> iterator() {
return this.result.asValue().iterator();
return Arrays.asList(this.array).iterator();
}

}
72 changes: 41 additions & 31 deletions src/main/java/org/cactoos/list/IterableAsList.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,32 +23,40 @@
*/
package org.cactoos.list;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
import org.cactoos.func.StickyScalar;
import org.cactoos.func.UncheckedScalar;

/**
* Iterable as {@link List}.
*
* <p>This class should be used very carefully. You must understand that
* it will fetch the entire content of the encapsulated {@link List} on each
* method call. It doesn't cache the data anyhow.</p>
*
* <p>If you don't need this {@link List} to re-fresh its content on every call,
* by doing round-trips to the encapsulated iterable, use
* {@link StickyList}.</p>
*
* <p>There is no thread-safety guarantee.
*
* @author Alexey Semenyuk (semenyukalexey88@gmail.com)
* @author Kirill (g4s8.public@gmail.com)
* @author Yegor Bugayenko (yegor256@gmail.com)
* @version $Id$
* @param <T> List type
* @see StickyList
* @since 0.1
*/
@SuppressWarnings("PMD.TooManyMethods")
public final class IterableAsList<T> implements List<T> {

/**
* List source.
* The source.
*/
private final UncheckedScalar<List<T>> list;
private final Iterable<T> iterable;

/**
* Ctor.
Expand All @@ -64,51 +72,41 @@ public IterableAsList(final T... array) {
/**
* Ctor.
*
* @param iterable An {@link Iterable}
* @param src An {@link Iterable}
*/
public IterableAsList(final Iterable<T> iterable) {
this.list = new UncheckedScalar<>(
new StickyScalar<>(
() -> {
final List<T> temp = new ArrayList<>(0);
for (final T elem : iterable) {
temp.add(elem);
}
return temp;
}
)
);
public IterableAsList(final Iterable<T> src) {
this.iterable = src;
}

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

@Override
public boolean isEmpty() {
return this.list.asValue().isEmpty();
return !this.iterable.iterator().hasNext();
}

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

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

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

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

@Override
Expand All @@ -127,7 +125,7 @@ public boolean remove(final Object object) {

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

@Override
Expand Down Expand Up @@ -168,7 +166,7 @@ public void clear() {

@Override
public T get(final int index) {
return this.list.asValue().get(index);
return this.list().get(index);
}

@Override
Expand All @@ -194,26 +192,38 @@ public T remove(final int index) {

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

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

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

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

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

/**
* Build a list.
* @return List
*/
private List<T> list() {
final List<T> list = new LinkedList<>();
for (final T item : this.iterable) {
list.add(item);
}
return list;
}
}
68 changes: 39 additions & 29 deletions src/main/java/org/cactoos/list/IterableAsMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,78 +27,76 @@
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import org.cactoos.func.StickyScalar;
import org.cactoos.func.UncheckedScalar;

/**
* Iterable as {@link Map}.
*
* <p>This class should be used very carefully. You must understand that
* it will fetch the entire content of the encapsulated {@link Map} on each
* method call. It doesn't cache the data anyhow.</p>
*
* <p>If you don't need this {@link Map} to re-fresh its content on every call,
* by doing round-trips to the encapsulated iterable, use
* {@link StickyMap}.</p>
*
* <p>There is no thread-safety guarantee.
*
* @author Yegor Bugayenko (yegor256@gmail.com)
* @version $Id$
* @param <X> Type of key
* @param <Y> Type of value
* @see StickyMap
* @since 0.4
*/
@SuppressWarnings("PMD.TooManyMethods")
public final class IterableAsMap<X, Y> implements Map<X, Y> {

/**
* The map.
* The iterable.
*/
private final UncheckedScalar<Map<X, Y>> map;
private final Iterable<Map.Entry<X, Y>> entries;

/**
* Ctor.
* @param entries Entries for the map
* @param list List of entries
*/
@SafeVarargs
@SuppressWarnings("varargs")
public IterableAsMap(final Map.Entry<X, Y>... entries) {
this(new ArrayAsIterable<>(entries));
public IterableAsMap(final Map.Entry<X, Y>... list) {
this(new ArrayAsIterable<>(list));
}

/**
* Ctor.
* @param entries Entries for the map
* @param list Entries for the entries
*/
public IterableAsMap(final Iterable<Map.Entry<X, Y>> entries) {
this.map = new UncheckedScalar<>(
new StickyScalar<>(
() -> {
final Map<X, Y> temp = new HashMap<>(0);
for (final Map.Entry<X, Y> entry : entries) {
temp.put(entry.getKey(), entry.getValue());
}
return temp;
}
)
);
public IterableAsMap(final Iterable<Map.Entry<X, Y>> list) {
this.entries = list;
}

@Override
public int size() {
return this.map.asValue().size();
return new LengthOfIterable(this.entries).asValue();
}

@Override
public boolean isEmpty() {
return this.map.asValue().isEmpty();
return !this.entries.iterator().hasNext();
}

@Override
public boolean containsKey(final Object key) {
return this.map.asValue().containsKey(key);
return this.map().containsKey(key);
}

@Override
public boolean containsValue(final Object value) {
return this.map.asValue().containsValue(value);
return this.map().containsValue(value);
}

@Override
public Y get(final Object key) {
return this.map.asValue().get(key);
return this.map().get(key);
}

@Override
Expand All @@ -116,7 +114,7 @@ public Y remove(final Object key) {
}

@Override
public void putAll(final Map<? extends X, ? extends Y> entries) {
public void putAll(final Map<? extends X, ? extends Y> list) {
throw new UnsupportedOperationException(
"#putAll() is not supported"
);
Expand All @@ -131,17 +129,29 @@ public void clear() {

@Override
public Set<X> keySet() {
return this.map.asValue().keySet();
return this.map().keySet();
}

@Override
public Collection<Y> values() {
return this.map.asValue().values();
return this.map().values();
}

@Override
public Set<Map.Entry<X, Y>> entrySet() {
return this.map.asValue().entrySet();
return this.map().entrySet();
}

/**
* Make a map.
* @return Map
*/
private Map<X, Y> map() {
final Map<X, Y> temp = new HashMap<>(0);
for (final Map.Entry<X, Y> entry : this.entries) {
temp.put(entry.getKey(), entry.getValue());
}
return temp;
}

}
Loading

0 comments on commit a7e44d0

Please sign in to comment.