Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,29 @@ 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]
Expand All @@ -29,6 +43,10 @@ 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
Expand All @@ -41,6 +59,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
Expand All @@ -52,6 +73,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
Expand All @@ -64,6 +88,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]
Expand All @@ -76,39 +103,78 @@ 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 {
Expand All @@ -131,12 +197,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)
Expand All @@ -153,6 +225,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]()
Expand Down