-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
VirtualDirectory.scala
68 lines (55 loc) · 2.04 KB
/
VirtualDirectory.scala
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
57
58
59
60
61
62
63
64
65
66
67
68
/* NSC -- new Scala compiler
* Copyright 2005-2013 LAMP/EPFL
*/
package dotty.tools.io
import scala.language.unsafeNulls
import scala.collection.mutable
import java.io.{InputStream, OutputStream}
/**
* An in-memory directory.
*
* @author Lex Spoon
*
* ''Note: This library is considered experimental and should not be used unless you know what you are doing.''
*/
class VirtualDirectory(val name: String, maybeContainer: Option[VirtualDirectory] = None)
extends AbstractFile {
def path: String =
maybeContainer match {
case None => name
case Some(parent) => parent.path + '/' + name
}
def absolute: AbstractFile = this
def container: VirtualDirectory = maybeContainer.get
def isDirectory: Boolean = true
override def isVirtual: Boolean = true
val lastModified: Long = System.currentTimeMillis
override def jpath: JPath = null
override def input: InputStream = sys.error("directories cannot be read")
override def output: OutputStream = sys.error("directories cannot be written")
/** Returns an abstract file with the given name. It does not
* check that it exists.
*/
def lookupNameUnchecked(name: String, directory: Boolean): AbstractFile = unsupported()
private val files = mutable.Map.empty[String, AbstractFile]
// the toList is so that the directory may continue to be
// modified while its elements are iterated
def iterator(): Iterator[AbstractFile] = files.values.toList.iterator
override def lookupName(name: String, directory: Boolean): AbstractFile =
(files get name filter (_.isDirectory == directory)).orNull
override def fileNamed(name: String): AbstractFile =
Option(lookupName(name, directory = false)) getOrElse {
val newFile = new VirtualFile(name, s"$path/$name")
files(name) = newFile
newFile
}
override def subdirectoryNamed(name: String): AbstractFile =
Option(lookupName(name, directory = true)) getOrElse {
val dir = new VirtualDirectory(name, Some(this))
files(name) = dir
dir
}
def clear(): Unit = {
files.clear()
}
}