Skip to content

Commit

Permalink
#463 ListEnvelope
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Nov 18, 2017
1 parent 66c1446 commit e5f0ffd
Show file tree
Hide file tree
Showing 14 changed files with 300 additions and 761 deletions.
4 changes: 2 additions & 2 deletions src/main/java/org/cactoos/collection/CollectionEnvelope.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
* @since 0.23
*/
@SuppressWarnings("PMD.TooManyMethods")
class CollectionEnvelope<X> implements Collection<X> {
public class CollectionEnvelope<X> implements Collection<X> {

/**
* Shuffled one.
Expand All @@ -50,7 +50,7 @@ class CollectionEnvelope<X> implements Collection<X> {
* Ctor.
* @param slr The scalar
*/
CollectionEnvelope(final Scalar<Collection<X>> slr) {
public CollectionEnvelope(final Scalar<Collection<X>> slr) {
this.col = new UncheckedScalar<>(slr);
}

Expand Down
161 changes: 5 additions & 156 deletions src/main/java/org/cactoos/list/Joined.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,7 @@
*/
package org.cactoos.list;

import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.stream.Collectors;

/**
Expand All @@ -39,13 +36,7 @@
* @param <X> Type of source item
* @since 0.20
*/
@SuppressWarnings("PMD.TooManyMethods")
public final class Joined<X> implements List<X> {

/**
* The original lists.
*/
private final Iterable<List<X>> lists;
public final class Joined<X> extends ListEnvelope<X> {

/**
* Ctor.
Expand All @@ -62,152 +53,10 @@ public Joined(final List<X>... src) {
* @param src Source lists
*/
public Joined(final Iterable<List<X>> src) {
this.lists = src;
}

@Override
public int size() {
int result = 0;
for (final List<X> list : this.lists) {
result += list.size();
}
return result;
}

@Override
public boolean isEmpty() {
boolean result = true;
for (final List<X> list : this.lists) {
if (!list.isEmpty()) {
result = false;
break;
}
}
return result;
}

@Override
public boolean contains(final Object obj) {
boolean result = false;
for (final List<X> list : this.lists) {
if (list.contains(obj)) {
result = true;
break;
}
}
return result;
}

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

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

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

@Override
public boolean add(final X item) {
throw new UnsupportedOperationException("#add()");
}

@Override
public boolean remove(final Object item) {
throw new UnsupportedOperationException("#remove()");
}

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

@Override
public boolean addAll(final Collection<? extends X> items) {
throw new UnsupportedOperationException("#addAll()");
}

@Override
public boolean addAll(final int index,
final Collection<? extends X> items) {
throw new UnsupportedOperationException("#addAll(index)");
}

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

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

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

@Override
public X get(final int index) {
return this.joined().get(index);
}

@Override
public X set(final int index, final X element) {
throw new UnsupportedOperationException("#set()");
}

@Override
public void add(final int index, final X element) {
throw new UnsupportedOperationException("#add(index)");
}

@Override
public X remove(final int index) {
throw new UnsupportedOperationException("#remove(index)");
}

@Override
public int indexOf(final Object item) {
throw new UnsupportedOperationException("#indexOf()");
}

@Override
public int lastIndexOf(final Object item) {
throw new UnsupportedOperationException("#lastIndexOf()");
}

@Override
public ListIterator<X> listIterator() {
throw new UnsupportedOperationException("#listIterator()");
}

@Override
public ListIterator<X> listIterator(final int index) {
throw new UnsupportedOperationException("#listIterator(index)");
}

@Override
public List<X> subList(final int start, final int end) {
return this.joined().subList(start, end);
}

/**
* Joined list.
*
* @return Result list
*/
private List<X> joined() {
return new ListOf<>(this.lists).stream()
super(() -> new ListOf<>(src).stream()
.flatMap(List::stream)
.collect(Collectors.toList());
.collect(Collectors.toList())
);
}

}
110 changes: 110 additions & 0 deletions src/main/java/org/cactoos/list/ListEnvelope.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/**
* 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.Collection;
import java.util.List;
import java.util.ListIterator;
import org.cactoos.Scalar;
import org.cactoos.collection.CollectionEnvelope;
import org.cactoos.scalar.UncheckedScalar;

/**
* List envelope.
*
* <p>There is no thread-safety guarantee.</p>
*
* @author Yegor Bugayenko (yegor256@gmail.com)
* @version $Id$
* @param <T> Element type
* @since 0.23
*/
@SuppressWarnings("PMD.TooManyMethods")
class ListEnvelope<T> extends CollectionEnvelope<T> implements List<T> {

/**
* Encapsulated list.
*/
private final UncheckedScalar<List<T>> list;

/**
* Ctor.
* @param src Source
*/
ListEnvelope(final Scalar<List<T>> src) {
super(src::value);
this.list = new UncheckedScalar<>(src);
}

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

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

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

@Override
public final void add(final int index, final T element) {
throw new UnsupportedOperationException("#add()");
}

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

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

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

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

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

@Override
public final List<T> subList(final int start, final int end) {
return this.list.value().subList(start, end);
}
}
Loading

0 comments on commit e5f0ffd

Please sign in to comment.