All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.cloudstate.javasupport.impl.crdt.FlagImpl.scala Maven / Gradle / Ivy

There is a newer version: 0.6.0
Show newest version
package io.cloudstate.javasupport.impl.crdt

import io.cloudstate.javasupport.crdt.Flag
import io.cloudstate.protocol.crdt.{CrdtDelta, CrdtState, FlagDelta, FlagState}

private[crdt] final class FlagImpl extends InternalCrdt with Flag {
  override final val name = "Flag"
  private var value: Boolean = false
  private var deltaValue: Boolean = false

  override def isEnabled: Boolean = value

  override def enable(): Unit =
    if (!deltaValue && !value) {
      deltaValue = true
      value = true
    }

  override def hasDelta: Boolean = deltaValue

  override def delta: Option[CrdtDelta.Delta] =
    if (hasDelta) {
      Some(CrdtDelta.Delta.Flag(FlagDelta(deltaValue)))
    } else None

  override def resetDelta(): Unit = deltaValue = false

  override def state: CrdtState.State = CrdtState.State.Flag(FlagState(value))

  override val applyDelta = {
    case CrdtDelta.Delta.Flag(FlagDelta(value)) =>
      this.value |= value
  }

  override val applyState = {
    case CrdtState.State.Flag(FlagState(value)) =>
      this.value = value
  }

  override def toString = s"Flag($value)"
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy