-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPNCounter.scala
79 lines (56 loc) · 2.86 KB
/
PNCounter.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*
* Copyright 2014 Aurel Paulovic ([email protected]) (aurelpaulovic.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.aurelpaulovic.crdt.immutable.state
import com.aurelpaulovic.crdt.Id
import com.aurelpaulovic.crdt.replica.Replica
import scala.reflect.runtime.universe._
class PNCounter[T] private (val id: Id, val replica: Replica, private val counter: component.PNCounter[T])(implicit num: Numeric[T], paramType: TypeTag[T]) extends CRDT[T, PNCounter[T]] {
def this(id: Id, replica: Replica)(implicit num: Numeric[T], paramType: TypeTag[T]) = this(id, replica, component.PNCounter[T](replica))
def setToOne(): PNCounter[T] = setTo(num.one)
def setToZero(): PNCounter[T] = setTo(num.zero)
private def setTo(newValue: T): PNCounter[T] = {
if (newValue == value) this
else new PNCounter[T](id, replica, counter.setTo(newValue))
}
lazy val value: T = counter.value
def increment(): PNCounter[T] = increment(num.one)
def decrement(): PNCounter[T] = decrement(num.one)
def increment(by: T): PNCounter[T] = new PNCounter[T](id, replica, counter.increment(by))
def decrement(by: T): PNCounter[T] = new PNCounter[T](id, replica, counter.decrement(by))
def isZero(): Boolean = counter.isZero
def isNegative(): Boolean = counter.isNegative
def leq(other: PNCounter[T]): Option[Boolean] = {
if (other.id == id) Some(counter /<=\ other.counter)
else None
}
def merge(other: PNCounter[T]): Option[PNCounter[T]] = {
if (other.id == id) Some(new PNCounter[T](id, replica, counter /+\ other.counter))
else None
}
protected def canRdtTypeEqual[OTHER: TypeTag](other: Any) = {
typeOf[OTHER] == typeOf[T] && other.isInstanceOf[PNCounter[_]]
}
def rdtTypeEquals(other: Any) = other match {
case that: PNCounter[_] => that.canRdtTypeEqual[T](this)
case _ => false
}
def copyForReplica(newReplica: Replica): PNCounter[T] = new PNCounter[T](id, newReplica, counter.copyForReplica(newReplica))
override def toString(): String = s"PNCounter($id, $replica, $counter) with value $value"
}
object PNCounter {
def apply[T](id: Id, replica: Replica)(implicit num: Numeric[T], paramType: TypeTag[T]): PNCounter[T] = new PNCounter[T](id, replica)
def apply[T](id: Id, replica: Replica, value: T)(implicit num: Numeric[T], paramType: TypeTag[T]): PNCounter[T] = new PNCounter[T](id, replica).increment(value)
}