Skip to content
This repository has been archived by the owner on Jun 6, 2022. It is now read-only.

Commit

Permalink
Alteration should return a Future
Browse files Browse the repository at this point in the history
  • Loading branch information
Regis Kuckaertz committed Feb 19, 2018
1 parent 6e10fe8 commit e98cb7d
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/main/scala/com/gu/Box.scala
Expand Up @@ -2,7 +2,7 @@ package com.gu

import java.util.concurrent.atomic.AtomicReference

import scala.concurrent.Future
import scala.concurrent.{ExecutionContext, Future}

abstract class Box[T] {
def get(): T
Expand All @@ -12,7 +12,7 @@ abstract class Box[T] {
def send(f: T => T): Unit

def alter(t: T): Future[T]
def alter(f: T => T): Future[T]
def alter(f: T => Future[T])(implicit ec: ExecutionContext): Future[T]

def map[A](f: T => A): Box[A]
def flatMap[A](f: T => Box[A]): Box[A]
Expand All @@ -32,7 +32,7 @@ private class AtomicRefBox[T](t: T) extends Box[T] {
def send(f: T => T): Unit = ref.updateAndGet(t => f(t))

def alter(t: T): Future[T] = Future.successful(ref.updateAndGet(_ => t))
def alter(f: T => T): Future[T] = Future.successful(ref.updateAndGet(t => f(t)))
def alter(f: T => Future[T])(implicit ec: ExecutionContext): Future[T] = f(t).map(t => ref.updateAndGet(_ => t))

def map[A](f: T => A): Box[A] = new AtomicRefBox[A](f(get()))
def flatMap[A](f: T => Box[A]): Box[A] = f(get())
Expand Down

0 comments on commit e98cb7d

Please sign in to comment.