Skip to content

Commit f649a68

Browse files
committed
Added support to create sets from iterable, iterator, set and list. Deprecated overloaded methods. Added methods to convert to standard java.util classes
1 parent 99f38a1 commit f649a68

5 files changed

Lines changed: 93 additions & 13 deletions

File tree

core/src/main/java/fj/F2Functions.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import static fj.P.p;
88
import static fj.data.IterableW.wrap;
9+
import static fj.data.Set.fromIterable;
910
import static fj.data.Set.iterableSet;
1011
import static fj.data.Tree.node;
1112
import static fj.data.TreeZipper.treeZipper;
@@ -200,7 +201,7 @@ static public <A, B, C> F2<NonEmptyList<A>, NonEmptyList<B>, NonEmptyList<C>> zi
200201
* @return A function that zips two sets with this function.
201202
*/
202203
static public <A, B, C> F2<Set<A>, Set<B>, Set<C>> zipSetM(final F2<A, B, C> f, final Ord<C> o) {
203-
return (as, bs) -> iterableSet(o, as.toStream().zipWith(bs.toStream(), f));
204+
return (as, bs) -> fromIterable(o, as.toStream().zipWith(bs.toStream(), f));
204205
}
205206

206207
/**

core/src/main/java/fj/data/Set.java

Lines changed: 85 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,43 @@ public final List<A> toList() {
291291
return foldMap(List.cons(List.<A>nil()), Monoid.<A>listMonoid());
292292
}
293293

294-
/**
294+
/**
295+
* Returns a java.util.Set representation of this set.
296+
*
297+
* @return a java.util.Set representation of this set.
298+
*/
299+
public final java.util.Set<A> toJavaSet() {
300+
return toJavaHashSet();
301+
}
302+
303+
/**
304+
* Returns a java.util.HashSet representation of this set.
305+
*
306+
* @return a java.util.HashSet representation of this set.
307+
*/
308+
public final java.util.HashSet<A> toJavaHashSet() {
309+
return new java.util.HashSet<A>(toStream().toCollection());
310+
}
311+
312+
/**
313+
* Returns a java.util.TreeSet representation of this set.
314+
*
315+
* @return a java.util.TreeSet representation of this set.
316+
*/
317+
public final java.util.TreeSet<A> toJavaTreeSet() {
318+
return new java.util.TreeSet<A>(toStream().toCollection());
319+
}
320+
321+
/**
322+
* Returns a java.util.List representation of this set.
323+
*
324+
* @return a java.util.List representation of this set.
325+
*/
326+
public final java.util.List toJavaList() {
327+
return new java.util.ArrayList<A>(toStream().toCollection());
328+
}
329+
330+
/**
295331
* Returns a list representation of this set in reverse order.
296332
*
297333
* @return a list representation of this set in reverse order.
@@ -509,17 +545,53 @@ public static <A> Set<A> join(final Ord<A> o, final Set<Set<A>> s) {
509545
/**
510546
* Return the elements of the given iterable as a set.
511547
*
548+
* @deprecated As of release 4.5, use {@link #fromIterable}
549+
*
512550
* @param o An order for the elements of the new set.
513551
* @param as An iterable of elements to add to a set.
514552
* @return A new set containing the elements of the given iterable.
515553
*/
554+
@Deprecated
516555
public static <A> Set<A> iterableSet(final Ord<A> o, final Iterable<A> as) {
556+
return fromIterable(o, as);
557+
}
558+
559+
/**
560+
* Return the elements of the given iterable as a set.
561+
*
562+
* @param o An order for the elements of the new set.
563+
* @param as An iterable of elements to add to a set.
564+
* @return A new set containing the elements of the given iterable.
565+
*/
566+
public static <A> Set<A> fromIterable(final Ord<A> o, final Iterable<A> as) {
517567
Set<A> s = empty(o);
518568
for (final A a : as)
519569
s = s.insert(a);
520570
return s;
521571
}
522572

573+
/**
574+
* Return the elements of the given java.util.Set as a set.
575+
*
576+
* @param o An order for the elements of the new set.
577+
* @param as A set of elements to create the set.
578+
* @return A new set containing the elements of the given set.
579+
*/
580+
public static <A> Set<A> fromJavaSet(final Ord<A> o, final java.util.Set<A> as) {
581+
return fromIterable(o, as);
582+
}
583+
584+
/**
585+
* Return the elements of the given iterator as a set.
586+
*
587+
* @param o An order for the elements of the new set.
588+
* @param as An iterator of elements to add to a set.
589+
* @return A new set containing the elements of the given iterator.
590+
*/
591+
public static <A> Set<A> fromIterator(final Ord<A> o, final Iterator<A> as) {
592+
return fromIterable(o, () -> as);
593+
}
594+
523595
/**
524596
* Constructs a set from the given elements.
525597
*
@@ -535,17 +607,24 @@ public static <A> Set<A> iterableSet(final Ord<A> o, final Iterable<A> as) {
535607
}
536608

537609
/**
538-
* Constructs a set from the given elements.
610+
* Constructs a set from the list.
611+
*
612+
* @deprecated As of release 4.5, use {@link #fromList}
539613
*
540614
* @param o An order for the elements of the new set.
541615
* @param list The elements to add to a set.
542616
* @return A new set containing the elements of the given list.
543617
*/
618+
@Deprecated
544619
public static <A> Set<A> set(final Ord<A> o, List<A> list) {
545-
Set<A> s = empty(o);
546-
for (final A a : list)
547-
s = s.insert(a);
548-
return s;
620+
return fromList(o, list);
621+
}
622+
623+
/**
624+
* Constructs a set from the list.
625+
*/
626+
public static <A> Set<A> fromList(final Ord<A> o, List<A> list) {
627+
return fromIterable(o, list);
549628
}
550629

551630
}

core/src/test/java/fj/data/TreeMapTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public void split() {
5151
}
5252

5353
private static Set<String> toSetString(List<Integer> list) {
54-
return Set.set(Ord.stringOrd, list.map(i -> i.toString()));
54+
return Set.fromList(Ord.stringOrd, list.map(i -> i.toString()));
5555
}
5656

5757
@Test

props-core-scalacheck/src/test/scala/fj/data/CheckSet.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import Ord.intOrd
1010
import Ord.stringOrd
1111
import P.p
1212
import Unit.unit
13-
import Set.{empty, single, join, iterableSet}
13+
import Set.{empty, single, join, iterableSet, fromIterable}
1414
import org.scalacheck.Properties
1515

1616
object CheckSet extends Properties("Set") {
@@ -38,10 +38,10 @@ object CheckSet extends Properties("Set") {
3838
single(oi, n).member(n))
3939

4040
property("noDupesFromList") = forAll((a: List[String]) =>
41-
setEqual(stringEqual).eq(iterableSet(os, a.nub(stringEqual)), iterableSet(os, a)))
41+
setEqual(stringEqual).eq(fromIterable(os, a.nub(stringEqual)), fromIterable(os, a)))
4242

4343
property("noDupesToList") = forAll((a: List[String]) =>
44-
iterableSet(os, a).toList().length() == a.nub(stringEqual).length())
44+
fromIterable(os, a).toList().length() == a.nub(stringEqual).length())
4545

4646
property("subsetEmpty") = forAll((a: Set[Int]) =>
4747
empty(oi).subsetOf(a))

quickcheck/src/main/java/fj/test/Arbitrary.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -815,11 +815,11 @@ public static <A> Arbitrary<Seq<A>> arbSeq(final Arbitrary<A> aa) {
815815
}
816816

817817
public static <A> Arbitrary<Set<A>> arbSet(Ord<A> ord, final Arbitrary<A> aa) {
818-
return arbitrary(arbList(aa).gen.map(list -> Set.set(ord, list)));
818+
return arbitrary(arbList(aa).gen.map(list -> Set.fromList(ord, list)));
819819
}
820820

821821
public static <A> Arbitrary<Set<A>> arbSet(Ord<A> ord, final Arbitrary<A> aa, int max) {
822-
Gen<Set<A>> g = Gen.choose(0, max).bind(i -> Gen.sequenceN(i, aa.gen)).map(list -> Set.set(ord, list));
822+
Gen<Set<A>> g = Gen.choose(0, max).bind(i -> Gen.sequenceN(i, aa.gen)).map(list -> Set.fromList(ord, list));
823823
return arbitrary(g);
824824
}
825825

0 commit comments

Comments
 (0)