Skip to content

Commit

Permalink
#464 SyncMap
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Nov 18, 2017
1 parent 801ff4f commit e14c0d2
Show file tree
Hide file tree
Showing 6 changed files with 230 additions and 152 deletions.
9 changes: 6 additions & 3 deletions src/main/java/org/cactoos/list/Joined.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/
package org.cactoos.list;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -53,9 +54,11 @@ public Joined(final List<X>... src) {
* @param src Source lists
*/
public Joined(final Iterable<List<X>> src) {
super(() -> new ListOf<>(src).stream()
.flatMap(List::stream)
.collect(Collectors.toList())
super(() -> Collections.unmodifiableList(
new ListOf<>(src).stream()
.flatMap(List::stream)
.collect(Collectors.toList())
)
);
}

Expand Down
151 changes: 4 additions & 147 deletions src/main/java/org/cactoos/list/StickyList.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,12 @@
package org.cactoos.list;

import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
import org.cactoos.iterable.IterableOf;
import org.cactoos.scalar.StickyScalar;
import org.cactoos.scalar.UncheckedScalar;

/**
* List decorator that goes through the list only once.
Expand All @@ -44,13 +43,7 @@
* @param <X> Type of item
* @since 0.8
*/
@SuppressWarnings("PMD.TooManyMethods")
public final class StickyList<X> implements List<X> {

/**
* The gate.
*/
private final UncheckedScalar<List<X>> gate;
public final class StickyList<X> extends ListEnvelope<X> {

/**
* Ctor.
Expand Down Expand Up @@ -83,151 +76,15 @@ public StickyList(final Iterator<X> items) {
* @param list The iterable
*/
public StickyList(final Collection<X> list) {
this.gate = new UncheckedScalar<>(
super(
new StickyScalar<>(
() -> {
final List<X> temp = new LinkedList<>();
temp.addAll(list);
return temp;
return Collections.unmodifiableList(temp);
}
)
);
}

@Override
public int size() {
return this.gate.value().size();
}

@Override
public boolean isEmpty() {
return this.gate.value().isEmpty();
}

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

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

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

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

@Override
public boolean add(final X 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.gate.value().containsAll(collection);
}

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

@Override
public boolean addAll(final int index,
final Collection<? extends X> 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 X get(final int index) {
return this.gate.value().get(index);
}

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

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

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

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

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

@Override
public ListIterator<X> listIterator() {
return this.gate.value().listIterator();
}

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

@Override
public List<X> subList(final int fromindex, final int toindex) {
return this.gate.value().subList(fromindex, toindex);
}
}
3 changes: 2 additions & 1 deletion src/main/java/org/cactoos/map/MapOf.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/
package org.cactoos.map;

import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
Expand Down Expand Up @@ -163,7 +164,7 @@ public MapOf(final Iterable<Map.Entry<X, Y>> entries) {
for (final Map.Entry<X, Y> entry : entries) {
temp.put(entry.getKey(), entry.getValue());
}
return temp;
return Collections.unmodifiableMap(temp);
});
}

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/cactoos/map/StickyMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/
package org.cactoos.map;

import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
Expand Down Expand Up @@ -161,7 +162,7 @@ public StickyMap(final Map<X, Y> map) {
() -> {
final Map<X, Y> temp = new HashMap<>(0);
temp.putAll(map);
return temp;
return Collections.unmodifiableMap(temp);
}
)
);
Expand Down
Loading

0 comments on commit e14c0d2

Please sign in to comment.