aboutsummaryrefslogtreecommitdiff
path: root/compiler/src/dotty/tools/io/package.scala
blob: 7acb827c9a976ca962fb451313474219289be286 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/* NSC -- new Scala compiler
 * Copyright 2005-2012 LAMP/EPFL
 * @author Paul Phillips
 */

package dotty.tools

import java.util.concurrent.{ Future, Callable }
import java.util.{ Timer, TimerTask }
import java.util.jar.{ Attributes }
import scala.language.implicitConversions

package object io {
  // Forwarders from scala.reflect.io
  type AbstractFile = scala.reflect.io.AbstractFile
  val AbstractFile = scala.reflect.io.AbstractFile
  type Directory = scala.reflect.io.Directory
  val Directory = scala.reflect.io.Directory
  type File = scala.reflect.io.File
  val File = scala.reflect.io.File
  type Path = scala.reflect.io.Path
  val Path = scala.reflect.io.Path
  val Streamable = scala.reflect.io.Streamable
  type VirtualDirectory = scala.reflect.io.VirtualDirectory
  type VirtualFile = scala.reflect.io.VirtualFile
  val ZipArchive = scala.reflect.io.ZipArchive
  type ZipArchive = scala.reflect.io.ZipArchive
  type JManifest = java.util.jar.Manifest
  type JFile = java.io.File

  implicit def enrichManifest(m: JManifest): Jar.WManifest = Jar.WManifest(m)
  private lazy val daemonThreadPool = DaemonThreadFactory.newPool()

  def runnable(body: => Unit): Runnable       = new Runnable { override def run() = body }
  def callable[T](body: => T): Callable[T]    = new Callable[T] { override def call() = body }
  def spawn[T](body: => T): Future[T]         = daemonThreadPool submit callable(body)
  def submit(runnable: Runnable)              = daemonThreadPool submit runnable

  // Create, start, and return a daemon thread
  def daemonize(body: => Unit): Thread = newThread(_ setDaemon true)(body)
  def newThread(f: Thread => Unit)(body: => Unit): Thread = {
    val thread = new Thread(runnable(body))
    f(thread)
    thread.start
    thread
  }

  // Set a timer to execute the given code.
  def timer(seconds: Int)(body: => Unit): Timer = {
    val alarm = new Timer(true) // daemon
    val tt    = new TimerTask { def run() = body }

    alarm.schedule(tt, seconds * 1000)
    alarm
  }
}