Skip to content

Commit

Permalink
#464 SafeCollection
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Nov 18, 2017
1 parent 051d5df commit 6146395
Show file tree
Hide file tree
Showing 19 changed files with 187 additions and 80 deletions.
2 changes: 1 addition & 1 deletion src/main/java/org/cactoos/collection/CollectionOf.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public CollectionOf(final T... array) {
* @since 0.21
*/
public CollectionOf(final Iterator<T> src) {
this(() -> src);
this(new IterableOf<>(src));
}

/**
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/org/cactoos/collection/Joined.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
*/
package org.cactoos.collection;

import org.cactoos.list.ListOf;

/**
* Joined collection.
*
Expand All @@ -43,7 +41,7 @@ public final class Joined<X> extends CollectionEnvelope<X> {
*/
@SafeVarargs
public Joined(final Iterable<X>... list) {
this(new ListOf<>(list));
this(new CollectionOf<>(list));
}

/**
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/org/cactoos/collection/Limited.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import java.util.Collection;
import java.util.Iterator;
import org.cactoos.iterable.IterableOf;
import org.cactoos.list.ListOf;

/**
* Limited collection.
Expand Down Expand Up @@ -67,7 +66,7 @@ public Limited(final int lmt, final Iterator<X> src) {
* @param lmt Requested number of elements
*/
public Limited(final int lmt, final Iterable<X> src) {
this(lmt, new ListOf<>(src));
this(lmt, new CollectionOf<>(src));
}

/**
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/org/cactoos/collection/Mapped.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import java.util.Iterator;
import org.cactoos.Func;
import org.cactoos.iterable.IterableOf;
import org.cactoos.list.ListOf;

/**
* Mapped collection.
Expand Down Expand Up @@ -69,7 +68,7 @@ public Mapped(final Func<X, Y> fnc, final Iterator<X> src) {
* @param fnc Func
*/
public Mapped(final Func<X, Y> fnc, final Iterable<X> src) {
this(fnc, new ListOf<>(src));
this(fnc, new CollectionOf<>(src));
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/cactoos/collection/Reversed.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import org.cactoos.list.ListOf;
import org.cactoos.iterable.IterableOf;

/**
* Reversed collection.
Expand All @@ -54,15 +54,15 @@ public final class Reversed<X> extends CollectionEnvelope<X> {
*/
@SafeVarargs
public Reversed(final X... src) {
this(new ListOf<>(src));
this(new IterableOf<>(src));
}

/**
* Ctor.
* @param src Source collection
*/
public Reversed(final Iterable<X> src) {
this(new ListOf<>(src));
this(new CollectionOf<>(src));
}

/**
Expand Down
76 changes: 76 additions & 0 deletions src/main/java/org/cactoos/collection/SafeCollection.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/**
* 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.collection;

import java.util.Collection;
import java.util.Iterator;
import org.cactoos.iterable.IterableOf;

/**
* A {@link Collection} that is both synchronized and sticky.
*
* <p>Objects of this class are thread-safe.</p>
*
* @author Yegor Bugayenko (yegor256@gmail.com)
* @version $Id$
* @param <T> List type
* @see StickyCollection
* @since 0.24
*/
public final class SafeCollection<T> extends CollectionEnvelope<T> {

/**
* Ctor.
* @param array An array of some elements
*/
@SafeVarargs
public SafeCollection(final T... array) {
this(new IterableOf<>(array));
}

/**
* Ctor.
* @param src An {@link Iterator}
*/
public SafeCollection(final Iterator<T> src) {
this(new IterableOf<>(src));
}

/**
* Ctor.
* @param src An {@link Iterator}
*/
public SafeCollection(final Iterable<T> src) {
this(new CollectionOf<>(src));
}

/**
* Ctor.
* @param src An {@link Iterable}
*/
public SafeCollection(final Collection<T> src) {
super(() -> new SyncCollection<T>(new StickyCollection<>(src)));
}

}
4 changes: 2 additions & 2 deletions src/main/java/org/cactoos/collection/Shuffled.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
*/
package org.cactoos.collection;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

/**
Expand Down Expand Up @@ -69,7 +69,7 @@ public Shuffled(final Iterable<T> src) {
*/
public Shuffled(final Collection<T> src) {
super(() -> {
final List<T> items = new LinkedList<>();
final List<T> items = new ArrayList<>(src.size());
items.addAll(src);
Collections.shuffle(items);
return items;
Expand Down
16 changes: 9 additions & 7 deletions src/main/java/org/cactoos/collection/Sorted.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,11 @@
*/
package org.cactoos.collection;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import org.cactoos.iterable.IterableOf;
import org.cactoos.list.ListOf;

/**
Expand Down Expand Up @@ -69,7 +68,10 @@ public Sorted(final T... src) {
*/
@SuppressWarnings("unchecked")
public Sorted(final Iterable<T> src) {
this((Comparator<T>) Comparator.naturalOrder(), new ListOf<>(src));
this(
(Comparator<T>) Comparator.naturalOrder(),
new CollectionOf<>(src)
);
}

/**
Expand All @@ -79,7 +81,7 @@ public Sorted(final Iterable<T> src) {
*/
@SafeVarargs
public Sorted(final Comparator<T> cmp, final T... src) {
this(cmp, new ListOf<>(src));
this(cmp, new CollectionOf<>(src));
}

/**
Expand All @@ -89,7 +91,7 @@ public Sorted(final Comparator<T> cmp, final T... src) {
* @since 0.23
*/
public Sorted(final Comparator<T> cmp, final Iterator<T> src) {
this(cmp, new IterableOf<>(src));
this(cmp, new CollectionOf<>(src));
}

/**
Expand All @@ -98,7 +100,7 @@ public Sorted(final Comparator<T> cmp, final Iterator<T> src) {
* @param cmp The comparator
*/
public Sorted(final Comparator<T> cmp, final Iterable<T> src) {
this(cmp, new ListOf<>(src));
this(cmp, new CollectionOf<>(src));
}

/**
Expand All @@ -108,7 +110,7 @@ public Sorted(final Comparator<T> cmp, final Iterable<T> src) {
*/
public Sorted(final Comparator<T> cmp, final Collection<T> src) {
super(() -> {
final List<T> items = new LinkedList<>();
final List<T> items = new ArrayList<>(src.size());
items.addAll(src);
items.sort(cmp);
return items;
Expand Down
9 changes: 4 additions & 5 deletions src/main/java/org/cactoos/collection/StickyCollection.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,11 @@
*/
package org.cactoos.collection;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import org.cactoos.iterable.IterableOf;
import org.cactoos.list.ListOf;
import org.cactoos.scalar.StickyScalar;

/**
Expand Down Expand Up @@ -58,15 +57,15 @@ public StickyCollection(final E... items) {
* @since 0.21
*/
public StickyCollection(final Iterator<E> items) {
this(() -> items);
this(new IterableOf<>(items));
}

/**
* Ctor.
* @param items The array
*/
public StickyCollection(final Iterable<E> items) {
this(new ListOf<>(items));
this(new CollectionOf<>(items));
}

/**
Expand All @@ -77,7 +76,7 @@ public StickyCollection(final Collection<E> list) {
super(
new StickyScalar<>(
() -> {
final Collection<E> temp = new LinkedList<>();
final Collection<E> temp = new ArrayList<>(list.size());
temp.addAll(list);
return Collections.unmodifiableCollection(temp);
}
Expand Down
16 changes: 11 additions & 5 deletions src/main/java/org/cactoos/collection/SyncCollection.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
* its content on every call, by doing round-trips to
* the encapsulated iterable, use {@link StickyCollection}.</p>
*
* <p>There is no thread-safety guarantee.
* <p>Objects of this class are thread-safe.</p>
*
* @author Yegor Bugayenko (yegor256@gmail.com)
* @version $Id$
Expand All @@ -63,19 +63,25 @@ public SyncCollection(final T... array) {
* @param src An {@link Iterator}
*/
public SyncCollection(final Iterator<T> src) {
this(() -> src);
this(new IterableOf<>(src));
}

/**
* Ctor.
* @param src An {@link Iterable}
*/
public SyncCollection(final Iterable<T> src) {
this(new CollectionOf<>(src));
}

/**
* Ctor.
* @param src An {@link Iterable}
*/
public SyncCollection(final Collection<T> src) {
super(
new SyncScalar<>(
() -> Collections.synchronizedCollection(
new CollectionOf<>(src)
)
() -> Collections.synchronizedCollection(src)
)
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/cactoos/iterable/Cycled.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public Cycled(final T... itr) {
* @since 0.21
*/
public Cycled(final Iterator<T> itr) {
this(() -> itr);
this(new IterableOf<T>(itr));
}

/**
Expand Down
26 changes: 5 additions & 21 deletions src/main/java/org/cactoos/iterable/Filtered.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,7 @@
* @see Filtered
* @since 0.1
*/
public final class Filtered<X> implements Iterable<X> {

/**
* Iterable.
*/
private final Iterable<X> iterable;

/**
* Function.
*/
private final Func<X, Boolean> func;
public final class Filtered<X> extends IterableEnvelope<X> {

/**
* Ctor.
Expand All @@ -79,7 +69,7 @@ public Filtered(final Func<X, Boolean> fnc, final X... src) {
* @since 0.21
*/
public Filtered(final Func<X, Boolean> fnc, final Iterator<X> src) {
this(fnc, () -> src);
this(fnc, new IterableOf<>(src));
}

/**
Expand All @@ -88,15 +78,9 @@ public Filtered(final Func<X, Boolean> fnc, final Iterator<X> src) {
* @param src Source iterable
*/
public Filtered(final Func<X, Boolean> fnc, final Iterable<X> src) {
this.iterable = src;
this.func = fnc;
}

@Override
public Iterator<X> iterator() {
return new org.cactoos.iterator.Filtered<>(
this.func, this.iterable.iterator()
);
super(() -> () -> new org.cactoos.iterator.Filtered<>(
fnc, src.iterator()
));
}

}
Loading

0 comments on commit 6146395

Please sign in to comment.