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

Scala example source code file (t3356.scala)

This example Scala source code file (t3356.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

a, couldn't, download, download, futureinput, imagedata, imagedata, imageinfo, imageinfo, list, string, string, test, uncaughtexception

The Scala t3356.scala source code

import scala.actors.{Actor, Exit, !, UncaughtException}
import Actor._

case class ImageInfo(text: String) {
  def downloadImage(): ImageData = {
    ImageData(text)
  }
}

case class ImageData(text: String)
case class Download(info: ImageInfo)

object Test {

  def scanForImageInfo(url: String): List[ImageInfo] =
    List(ImageInfo("A"), ImageInfo("B"))

  def renderImage(data: ImageData) {
    println("rendering image "+data.text)
  }

  def renderImages(url: String) {
    val imageInfos = scanForImageInfo(url)
    println("sending download requests")
    val dataFutures = for (info <- imageInfos) yield {
      val loader = link {
        react { case Download(info) =>
          throw new Exception("no connection")
          reply(info.downloadImage())
        }; {}
      }
      loader !! Download(info)
    }
    var i = 0
    loopWhile (i < imageInfos.size) {
      i += 1
      val FutureInput = dataFutures(i-1).inputChannel
      react {
        case FutureInput ! (data @ ImageData(_)) =>
          renderImage(data)
        case Exit(from, UncaughtException(_, Some(Download(info)), _, _, cause)) =>
          println("Couldn't download image because of "+cause)
      }
    }
    println("OK, all images rendered.")
  }

  def main(args: Array[String]) {
    actor {
      self.trapExit = true
      renderImages("panorama.epfl.ch")
    }
  }

}

Other Scala examples (source code examples)

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

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

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 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.