From ddb6d67fa2363b3238f5e104ecaf602a30efbbdb Mon Sep 17 00:00:00 2001 From: Raul Estrada Date: Thu, 3 Feb 2022 15:39:40 -0600 Subject: [PATCH 1/2] ConcurrentSet Scaladocs --- .../scala/zio/concurrent/ConcurrentSet.scala | 69 +++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/concurrent/shared/src/main/scala/zio/concurrent/ConcurrentSet.scala b/concurrent/shared/src/main/scala/zio/concurrent/ConcurrentSet.scala index 9454cf5bbeeb..f3a301e418e2 100644 --- a/concurrent/shared/src/main/scala/zio/concurrent/ConcurrentSet.scala +++ b/concurrent/shared/src/main/scala/zio/concurrent/ConcurrentSet.scala @@ -7,15 +7,27 @@ import java.util.concurrent.ConcurrentHashMap import java.util.function.{Consumer, Predicate} import scala.collection.JavaConverters._ +/** + * A `ConcurrentSet` is a Set wrapper over `java.util.concurrent.ConcurrentHashMap`. + */ final class ConcurrentSet[A] private (private val underlying: ConcurrentHashMap.KeySetView[A, java.lang.Boolean]) extends AnyVal { + /** + * Adds a new value. + */ def add(x: A): UIO[Boolean] = UIO(underlying.add(x)) + /** + * Adds all new values. + */ def addAll(xs: Iterable[A]): UIO[Boolean] = UIO(underlying.addAll(xs.asJavaCollection): @silent("JavaConverters")) + /** + * Finds the first element of a set for which the partial function is defined and applies the function to it. + */ def collectFirst[B](pf: PartialFunction[A, B]): UIO[Option[B]] = UIO { var result = Option.empty[B] @@ -29,6 +41,9 @@ final class ConcurrentSet[A] private (private val underlying: ConcurrentHashMap. result } + /** + * Tests whether a given predicate holds true for at least one element in the set. + */ def exists(p: A => Boolean): UIO[Boolean] = UIO { var result = false @@ -41,6 +56,9 @@ final class ConcurrentSet[A] private (private val underlying: ConcurrentHashMap. result } + /** + * Folds the elements of a set using the given binary operator. + */ def fold[R, E, S](zero: S)(f: (S, A) => S): UIO[S] = UIO { var result: S = zero @@ -52,6 +70,9 @@ final class ConcurrentSet[A] private (private val underlying: ConcurrentHashMap. result } + /** + * Tests whether a predicate is satisfied by all elements of a set. + */ def forall(p: A => Boolean): UIO[Boolean] = UIO { var result = true @@ -64,6 +85,9 @@ final class ConcurrentSet[A] private (private val underlying: ConcurrentHashMap. result } + /** + * Retrieves the elements in which predicate is satisfied. + */ def find[B](p: A => Boolean): UIO[Option[A]] = UIO { var result = Option.empty[A] @@ -76,39 +100,75 @@ final class ConcurrentSet[A] private (private val underlying: ConcurrentHashMap. result } + /** + * Removes the entry for the given value if it is mapped to an existing element. + */ def remove(x: A): UIO[Boolean] = UIO(underlying.remove(x)) + /** + * Removes all the entries for the given values if they are mapped to an existing element. + */ def removeAll(xs: Iterable[A]): UIO[Boolean] = UIO(underlying.removeAll(xs.asJavaCollection): @silent("JavaConverters")) + /** + * Removes all elements which satisfy the given predicate. + */ def removeIf(p: A => Boolean): UIO[Boolean] = UIO(underlying.removeIf(makePredicate(a => !p(a)))) + /** + * Retain all the entries for the given values if they are mapped to an existing element. + */ def retainAll(xs: Iterable[A]): UIO[Boolean] = UIO(underlying.retainAll(xs.asJavaCollection): @silent("JavaConverters")) + /** + * Removes all elements which do not satisfy the given predicate. + */ def retainIf(p: A => Boolean): UIO[Boolean] = UIO(underlying.removeIf(makePredicate(p))) + /** + * Removes all elements. + */ def clear: UIO[Unit] = UIO(underlying.clear()) + /** + * Tests whether if the element is in the set. + */ def contains(x: A): UIO[Boolean] = UIO(underlying.contains(x)) + /** + * Tests if the elements in the collection are a subset of the set. + */ def containsAll(xs: Iterable[A]): UIO[Boolean] = UIO(xs.forall(x => underlying.contains(x))) + /** + * Number of elements in the set. + */ def size: UIO[Int] = UIO(underlying.size()) + /** + * True if there are no elements in the set. + */ def isEmpty: UIO[Boolean] = UIO(underlying.isEmpty) + /** + * Create a concurrent set from a set. + */ def toSet: UIO[Set[A]] = UIO(underlying.asScala.toSet: @silent("JavaConverters")) + /** + * Create a concurrent set from a collection. + */ @silent("JavaConverters") def transform(f: A => A): UIO[Unit] = UIO { @@ -131,12 +191,18 @@ final class ConcurrentSet[A] private (private val underlying: ConcurrentHashMap. object ConcurrentSet { + /** + * Makes an empty ConcurrentSet + */ def empty[A]: UIO[ConcurrentSet[A]] = UIO { val keySetView = ConcurrentHashMap.newKeySet[A]() new ConcurrentSet(keySetView) } + /** + * Makes an empty ConcurrentSet with initial capacity + */ def empty[A](initialCapacity: Int): UIO[ConcurrentSet[A]] = UIO { val keySetView = ConcurrentHashMap.newKeySet[A](initialCapacity) @@ -153,6 +219,9 @@ object ConcurrentSet { new ConcurrentSet(keySetView) } + /** + * Makes a new ConcurrentSet initialized with the provided elements + */ def make[A](as: A*): UIO[ConcurrentSet[A]] = UIO { val keySetView = ConcurrentHashMap.newKeySet[A]() From c6f8571be0da9e3eebca057ae58ca7671c43aebc Mon Sep 17 00:00:00 2001 From: Raul Estrada Date: Fri, 4 Feb 2022 01:49:17 -0600 Subject: [PATCH 2/2] ConcurrentSet Scaladocs --- .../scala/zio/concurrent/ConcurrentSet.scala | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/concurrent/shared/src/main/scala/zio/concurrent/ConcurrentSet.scala b/concurrent/shared/src/main/scala/zio/concurrent/ConcurrentSet.scala index f3a301e418e2..2ef243afc23c 100644 --- a/concurrent/shared/src/main/scala/zio/concurrent/ConcurrentSet.scala +++ b/concurrent/shared/src/main/scala/zio/concurrent/ConcurrentSet.scala @@ -8,7 +8,8 @@ import java.util.function.{Consumer, Predicate} import scala.collection.JavaConverters._ /** - * A `ConcurrentSet` is a Set wrapper over `java.util.concurrent.ConcurrentHashMap`. + * A `ConcurrentSet` is a Set wrapper over + * `java.util.concurrent.ConcurrentHashMap`. */ final class ConcurrentSet[A] private (private val underlying: ConcurrentHashMap.KeySetView[A, java.lang.Boolean]) extends AnyVal { @@ -26,7 +27,8 @@ final class ConcurrentSet[A] private (private val underlying: ConcurrentHashMap. UIO(underlying.addAll(xs.asJavaCollection): @silent("JavaConverters")) /** - * Finds the first element of a set for which the partial function is defined and applies the function to it. + * Finds the first element of a set for which the partial function is defined + * and applies the function to it. */ def collectFirst[B](pf: PartialFunction[A, B]): UIO[Option[B]] = UIO { @@ -42,7 +44,8 @@ final class ConcurrentSet[A] private (private val underlying: ConcurrentHashMap. } /** - * Tests whether a given predicate holds true for at least one element in the set. + * Tests whether a given predicate holds true for at least one element in the + * set. */ def exists(p: A => Boolean): UIO[Boolean] = UIO { @@ -101,13 +104,15 @@ final class ConcurrentSet[A] private (private val underlying: ConcurrentHashMap. } /** - * Removes the entry for the given value if it is mapped to an existing element. + * Removes the entry for the given value if it is mapped to an existing + * element. */ def remove(x: A): UIO[Boolean] = UIO(underlying.remove(x)) /** - * Removes all the entries for the given values if they are mapped to an existing element. + * Removes all the entries for the given values if they are mapped to an + * existing element. */ def removeAll(xs: Iterable[A]): UIO[Boolean] = UIO(underlying.removeAll(xs.asJavaCollection): @silent("JavaConverters")) @@ -119,7 +124,8 @@ final class ConcurrentSet[A] private (private val underlying: ConcurrentHashMap. UIO(underlying.removeIf(makePredicate(a => !p(a)))) /** - * Retain all the entries for the given values if they are mapped to an existing element. + * Retain all the entries for the given values if they are mapped to an + * existing element. */ def retainAll(xs: Iterable[A]): UIO[Boolean] = UIO(underlying.retainAll(xs.asJavaCollection): @silent("JavaConverters"))