Created
July 4, 2016 13:58
-
-
Save natewave/d347f616cdf38178d7ec217a20b71a9e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sealed trait NodeState | |
trait Follower extends NodeState | |
trait Candidate extends NodeState | |
trait Leader extends NodeState | |
final class NotLeader extends NodeState with Follower with Candidate | |
final class NotFollower extends NodeState with Candidate with Leader | |
final class NotCandidate extends NodeState with Follower with Candidate | |
class Node[State <: NodeState] private () { | |
// Only a non leader can become a candidate | |
def becomeCandidate[T >: State <: NotLeader]() = this.asInstanceOf[Node[Candidate]] | |
// become follower | |
def becomeFollower[T >: State <: NotFollower]() = this.asInstanceOf[Node[Follower]] | |
// only a non leader can become a leader | |
def becomeLeader[T >: State <: NotLeader]() = this.asInstanceOf[Node[Leader]] | |
} | |
object Node { | |
// A node starts as a Follower | |
def create() = new Node[Follower] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment