Skip to content

Commit

Permalink
(yegor256#1292) SetOf and Sorted are real in memory sets
Browse files Browse the repository at this point in the history
  • Loading branch information
victornoel committed Feb 29, 2020
1 parent 671155e commit 88ba4d9
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 31 deletions.
16 changes: 2 additions & 14 deletions src/main/java/org/cactoos/set/SetOf.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,10 @@
import java.util.HashSet;
import java.util.Set;
import org.cactoos.iterable.IterableOf;
import org.cactoos.scalar.Unchecked;

/**
* Iterable as {@link Set}.
*
* <p>This class should be used very carefully. You must understand that
* it will fetch the entire content of the encapsulated {@link Set} on each
* method call. It doesn't cache the data anyhow. </p>
*
* <p>There is no thread-safety guarantee.
*
* @param <T> Set type
Expand All @@ -57,14 +52,7 @@ public SetOf(final T... array) {
* @param src An {@link Iterable}
*/
public SetOf(final Iterable<T> src) {
super(
new Unchecked<>(
() -> {
final Set<T> tmp = new HashSet<>();
src.forEach(tmp::add);
return tmp;
}
).value()
);
super(new HashSet<>());
src.forEach(super::add);
}
}
17 changes: 3 additions & 14 deletions src/main/java/org/cactoos/set/Sorted.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,10 @@
import java.util.Set;
import java.util.TreeSet;
import org.cactoos.iterable.IterableOf;
import org.cactoos.scalar.Unchecked;

/**
* Sorted Iterable as {@link Set}.
*
* <p>This class should be used very carefully. You must understand that
* it will fetch the entire content of the encapsulated {@link Set} on each
* method call. It doesn't cache the data anyhow. </p>
*
* <p>There is no thread-safety guarantee.
*
* @param <T> Set type
Expand All @@ -58,15 +53,9 @@ public Sorted(final Comparator<T> cmp, final T... array) {
* @param cmp Comparator
* @param src An {@link Iterable}
*/
@SuppressWarnings("PMD.ConstructorOnlyInitializesOrCallOtherConstructors")
public Sorted(final Comparator<T> cmp, final Iterable<T> src) {
super(
new Unchecked<>(
() -> {
final Set<T> set = new TreeSet<>(cmp);
src.forEach(set::add);
return set;
}
).value()
);
super(new TreeSet<>(cmp));
src.forEach(super::add);
}
}
3 changes: 0 additions & 3 deletions src/main/java/org/cactoos/set/package-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,5 @@
* Sets.
*
* @since 0.49.2
* @todo #1242:30min The SetOf class should be implemented as an in-memory set,
* for example based on HashSet. See ListOf for an example and #1242 for the
* rationale behind this design.
*/
package org.cactoos.set;

0 comments on commit 88ba4d9

Please sign in to comment.