summaryrefslogtreecommitdiff
path: root/examples/scala-js/javalib/src/main/scala/java/util/concurrent/atomic/AtomicInteger.scala
blob: 1f24b7b8f7b5db775b8002ac2c61a20807bce6c6 (plain) (blame)
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
package java.util.concurrent.atomic

class AtomicInteger(private[this] var value: Int)
    extends Number with Serializable {

  def this() = this(0)

  final def get(): Int = value

  final def set(newValue: Int): Unit =
    value = newValue

  final def lazySet(newValue: Int): Unit =
    set(newValue)

  final def getAndSet(newValue: Int): Int = {
    val old = value
    value = newValue
    old
  }

  final def compareAndSet(expect: Int, update: Int): Boolean = {
    if (expect != value) false else {
      value = update
      true
    }
  }

  final def weakCompareAndSet(expect: Int, update: Int): Boolean =
    compareAndSet(expect, update)

  final def getAndIncrement(): Int =
    getAndAdd(1)

  final def getAndDecrement(): Int =
    getAndAdd(-1)

  @inline final def getAndAdd(delta: Int): Int = {
    val old = value
    value = old + delta
    old
  }

  final def incrementAndGet(): Int =
    addAndGet(1)

  final def decrementAndGet(): Int =
    addAndGet(-1)

  @inline final def addAndGet(delta: Int): Int = {
    val newValue = value + delta
    value = newValue
    newValue
  }

  override def toString(): String =
    value.toString()

  def intValue(): Int = value
  def longValue(): Long = value.toLong
  def floatValue(): Float = value.toFloat
  def doubleValue(): Double = value.toDouble
}