alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Scala example source code file (t3620.scala)

This example Scala source code file (t3620.scala) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Scala tags/keywords

application, hashmap, k, k, none, option, option, some, store, unit, unit, v, v, waiting

The Scala t3620.scala source code

import scala.collection.mutable.HashMap
import scala.util.continuations._

object Test extends Application {

  class Store[K,V] {

    trait Waiting {
      def key: K
      def inform(value: V): Unit
    }

    private val map = new HashMap[K, V]
    private var waiting: List[Waiting] = Nil

    def waitFor(k: K, f: (V => Unit)) {
      map.get(k) match {
        case Some(v) => f(v)
        case None => {
          val w = new Waiting {
            def key = k
            def inform(v: V) = f(v)
          }
          waiting = w :: waiting
        }
      }
    }


    def add(key: K, value: V) {
      map(key) = value
      val p = waiting.partition(_.key == key)
      waiting = p._2
      p._1.foreach(_.inform(value))
    }

    def required(key: K) = {
      shift {
        c: (V => Unit) => {
          waitFor(key, c)
        }
      }
    }

    def option(key: Option[K]) = {
      shift {
        c: (Option[V] => Unit) => {
          key match {
            case Some(key) => waitFor(key, (v: V) => c(Some(v)))
            case None => c(None)
          }

        }
      }
    }

  }

  val store = new Store[String, Int]

  def test(p: Option[String]): Unit = {
    reset {
      // uncommenting the following two lines makes the compiler happy!
//      val o = store.option(p)
//      println(o)
      val i = store.option(p).getOrElse(1)
      println(i)
    }
  }

  test(Some("a"))

}

Other Scala examples (source code examples)

Here is a short list of links related to this Scala t3620.scala source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.