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 c3e5d07
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 31 deletions.
25 changes: 11 additions & 14 deletions src/main/java/org/cactoos/set/SetOf.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,13 @@
package org.cactoos.set;

import java.util.HashSet;
import java.util.Iterator;
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 @@ -52,19 +48,20 @@ public SetOf(final T... array) {
this(new IterableOf<>(array));
}

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

/**
* Ctor.
* @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);
}
}
27 changes: 13 additions & 14 deletions src/main/java/org/cactoos/set/Sorted.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,14 @@
package org.cactoos.set;

import java.util.Comparator;
import java.util.Iterator;
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 @@ -53,20 +49,23 @@ public Sorted(final Comparator<T> cmp, final T... array) {
this(cmp, new IterableOf<>(array));
}

/**
* Ctor.
* @param cmp Comparator
* @param src An {@link Iterator}
*/
public Sorted(final Comparator<T> cmp, final Iterator<T> src) {
this(cmp, () -> src);
}

/**
* Ctor.
* @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 c3e5d07

Please sign in to comment.