summaryrefslogtreecommitdiff
path: root/javalib/src/main/scala/java/util/concurrent/atomic/AtomicReference.scala
blob: 650b1e0c42789245bda5993180001aba6e8d531d (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
package java.util.concurrent.atomic

class AtomicReference[T <: AnyRef](
    private[this] var value: T) extends Serializable {

  def this() = this(null.asInstanceOf[T])

  final def get(): T = value

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

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

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

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

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

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