Skip to content

Commit 98f0eb0

Browse files
author
Cantor Chron
committed
Expand map interface, add some docs.
1 parent dcd6afc commit 98f0eb0

File tree

4 files changed

+84
-0
lines changed

4 files changed

+84
-0
lines changed

src/main/scala/scala/reactive/container/RCell.scala

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,27 @@ import scala.reflect.ClassTag
77

88

99

10+
/** The reactive cell abstraction represents a mutable memory location
11+
* whose changes may produce events.
12+
*
13+
* An `RCell` is conceptually similar to a reactive emitter lifted into a signal.
14+
*
15+
* @tparam T the type of the values it stores
16+
* @param value the initial value of the reactive cell
17+
*/
1018
class RCell[@spec(Int, Long, Double) T](private var value: T)
1119
extends Signal.Default[T] with ReactMutable {
1220
self =>
1321

22+
/** Returns the current value in the reactive cell.
23+
*/
1424
def apply(): T = value
1525

26+
/** Assigns a new value to the reactive cell,
27+
* and emits an event with the new value to all the subscribers.
28+
*
29+
* @param v the new value
30+
*/
1631
def :=(v: T): Unit = {
1732
value = v
1833
reactAll(v)
@@ -24,6 +39,8 @@ extends Signal.Default[T] with ReactMutable {
2439

2540
object RCell {
2641

42+
/** A factory method for creating reactive cells.
43+
*/
2744
def apply[@spec(Int, Long, Double) T](x: T) = new RCell[T](x)
2845

2946
}

src/main/scala/scala/reactive/container/RHashMap.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,8 @@ extends RMap[K, V] with RBuilder[(K, V), RHashMap[K, V]] with PairBuilder[K, V,
231231

232232
private def noKeyError(key: K) = throw new NoSuchElementException("key: " + key)
233233

234+
def nil: V = null
235+
234236
def applyOrNil(key: K): V = {
235237
val entry = lookup(key)
236238
if (entry == null || entry.value == null) null

src/main/scala/scala/reactive/container/RHashValMap.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ class RHashValMap[@spec(Int, Long, Double) K, @spec(Int, Long, Double) V](
2626

2727
init(emptyKey, emptyVal)
2828

29+
def nil: V = emptyVal.nil
30+
2931
def inserts: Reactive[(K, V)] = insertsEmitter
3032
def removes: Reactive[(K, V)] = removesEmitter
3133

src/main/scala/scala/reactive/container/RMap.scala

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,35 +8,98 @@ import scala.annotation.implicitNotFound
88

99

1010

11+
/** A reactive map is a reactive counterpart of a mutable map.
12+
*
13+
* Calling the `react` method of the `RMap` type returns.
14+
*
15+
* @tparam K the type of the keys in the reactive map
16+
* @tparam V the type of the values in the reactive map
17+
*/
1118
trait RMap[@spec(Int, Long, Double) K, V <: AnyRef] extends RContainer[(K, V)] {
1219

20+
/** The special value that represents an absence of the value
21+
* under the specified key.
22+
*
23+
* @returns the nil value
24+
*/
25+
def nil: V
26+
27+
/** Returns the value associated with the specified key or throws an exception if the key is not present.
28+
*
29+
* @param k the key
30+
* @returns the value associated with the key
31+
*/
1332
def apply(k: K): V
1433

34+
/** Returns the value associated with the specified key or a nil value if the key is not present.
35+
*
36+
* @param k the key
37+
* @returns the value associated with the key, or a nil value
38+
*/
39+
def applyOrNil(k: K): V
40+
41+
/** Optionally returns the value associated with the key, if it is present.
42+
*
43+
* @param k the key
44+
* @returns the optional value associated with the key
45+
*/
46+
def get(k: K): Option[V]
47+
48+
/** Returns the view over pairs in this map.
49+
*/
1550
def entries: PairContainer[K, V]
1651

52+
/** Returns the view over keys in this map.
53+
*/
1754
def keys: RContainer[K]
1855

56+
/** Returns the view over values in this map.
57+
*
58+
* This method may only be used if the reactive map is injective,
59+
* that is, no two keys are mapped into the same value.
60+
*/
1961
def values: RContainer[V]
2062

63+
/** Returns the lifted view of the reactive map, used to obtain reactive values in this map.
64+
*/
2165
def react: RMap.Lifted[K, V]
2266

2367
}
2468

2569

2670
object RMap {
2771

72+
/** Factory method for default reactive map creation.
73+
*/
2874
def apply[@spec(Int, Long, Double) K, V >: Null <: AnyRef](implicit can: RHashMap.Can[K, V]) = new RHashMap[K, V]
2975

76+
/** Reactive builder factory, automatically used for transforming containers into reactive maps.
77+
*/
3078
implicit def factory[@spec(Int, Long, Double) K, V >: Null <: AnyRef] = new RBuilder.Factory[(K, V), RMap[K, V]] {
3179
def apply() = RHashMap[K, V]
3280
}
3381

82+
/** Reactive pair builder factory, automatically used for transforming pair containers into reactive maps.
83+
*/
3484
implicit def pairFactory[@spec(Int, Long, Double) K, V >: Null <: AnyRef] = new PairBuilder.Factory[K, V, RMap[K, V]] {
3585
def apply() = RHashMap[K, V]
3686
}
3787

88+
/** Lifted view of the reactive map.
89+
*/
3890
trait Lifted[@spec(Int, Long, Double) K, V <: AnyRef] extends RContainer.Lifted[(K, V)] {
91+
/** The original reactive map container.
92+
*/
3993
val container: RMap[K, V]
94+
95+
/** Returns the reactive value associated with the key.
96+
*
97+
* The reactive value emits an event whenever a new value
98+
* is subsequently assigned to the specified key.
99+
*
100+
* @param key the key
101+
* @returns the reactive containing all the values assigned to the key
102+
*/
40103
def apply(key: K): Reactive[V]
41104
}
42105

0 commit comments

Comments
 (0)