Made example-vfs compilable and workable.
This commit is contained in:
@@ -82,19 +82,19 @@ public object FileSystem {
|
||||
|
||||
/* Scans file recursively and adds info about it to fileToInfo map */
|
||||
private fun scanAndAddRecursivelyNoEvents(file : VirtualFile) {
|
||||
assert(FileSystem.fileToInfo[file.path] == null)
|
||||
require(FileSystem.fileToInfo[file.path] == null)
|
||||
|
||||
val fileInfo = VirtualFileInfo(file)
|
||||
FileSystem.fileToInfo[file.path] = fileInfo
|
||||
fileInfo.children.foreach{ scanAndAddRecursivelyNoEvents(it) }
|
||||
fileInfo.children.forEach{ scanAndAddRecursivelyNoEvents(it) }
|
||||
}
|
||||
|
||||
internal inline fun assertCanRead() {
|
||||
assert(lock.getReadHoldCount() != 0 || lock.isWriteLockedByCurrentThread())
|
||||
check(lock.getReadHoldCount() != 0 || lock.isWriteLockedByCurrentThread())
|
||||
}
|
||||
|
||||
internal inline fun assertCanWrite() {
|
||||
assert(lock.isWriteLockedByCurrentThread())
|
||||
check(lock.isWriteLockedByCurrentThread())
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -107,12 +107,8 @@ public object FileSystem {
|
||||
/**
|
||||
* Adds file system listener which should be notified about changing of file system.
|
||||
*/
|
||||
public fun addVirtualFileListener(listener : VirtualFileListener.(VirtualFileEvent)->Unit) : VirtualFileListener {
|
||||
val vfl = object: VirtualFileListener{
|
||||
override fun eventHappened(event: VirtualFileEvent) {
|
||||
listener(event)
|
||||
}
|
||||
}
|
||||
public fun addVirtualFileListener(listener : (VirtualFileEvent)->Unit) : VirtualFileListener {
|
||||
val vfl = SimpleVirtualFileListener(listener)
|
||||
addVirtualFileListener(vfl)
|
||||
return vfl
|
||||
}
|
||||
|
||||
@@ -12,7 +12,6 @@ import java.util.Timer
|
||||
import java.util.TimerTask
|
||||
|
||||
import org.jetbrains.jet.samples.vfs.utils.*
|
||||
import org.jetbrains.jet.samples.vfs.utils.listDifference
|
||||
|
||||
/**
|
||||
* Singleton which creates thread for periodically checking if there are changes in
|
||||
@@ -37,13 +36,13 @@ internal object RefreshQueue {
|
||||
}
|
||||
|
||||
private fun takeAndRefreshFiles() {
|
||||
refreshFiles(taskQueue.take())
|
||||
refreshFiles(taskQueue.take()!!)
|
||||
}
|
||||
|
||||
/* Acquires write lock and refreshes file system */
|
||||
private fun refreshFiles(files : List<VirtualFile>) {
|
||||
FileSystem.write {
|
||||
files.foreach{ refreshFile(it) }
|
||||
files.forEach{ refreshFile(it) }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -51,7 +50,7 @@ internal object RefreshQueue {
|
||||
private fun refreshFile(file : VirtualFile) {
|
||||
FileSystem.assertCanWrite()
|
||||
val fileInfo = FileSystem.fileToInfo[file.path]
|
||||
assert(fileInfo != null)
|
||||
check(fileInfo != null)
|
||||
if (fileInfo == null) {
|
||||
return
|
||||
}
|
||||
@@ -64,13 +63,13 @@ internal object RefreshQueue {
|
||||
val deletedChildren = listDifference(oldChildren, newChildren)
|
||||
val commonChildren = listIntersection(oldChildren, newChildren)
|
||||
|
||||
addedChildren.foreach{ addRecursively(it) }
|
||||
deletedChildren.foreach{ deleteRecursively(it) }
|
||||
addedChildren.forEach{ addRecursively(it) }
|
||||
deletedChildren.forEach{ deleteRecursively(it) }
|
||||
|
||||
fileInfo.children.clear()
|
||||
fileInfo.children.addAll(newChildren)
|
||||
|
||||
commonChildren.foreach{ refreshFile(it) }
|
||||
commonChildren.forEach{ refreshFile(it) }
|
||||
} else {
|
||||
val newModificationTime = file.modificationTime()
|
||||
if (fileInfo.lastModified != newModificationTime) {
|
||||
@@ -83,21 +82,21 @@ internal object RefreshQueue {
|
||||
|
||||
/* Adds file to file system recursively, notifying listeners */
|
||||
private fun addRecursively(file : VirtualFile) {
|
||||
assert(FileSystem.fileToInfo[file] == null)
|
||||
require(FileSystem.fileToInfo[file] == null)
|
||||
|
||||
val fileInfo = VirtualFileInfo(file)
|
||||
|
||||
FileSystem.fileToInfo[file.path] = fileInfo
|
||||
FileSystem.notifyEventHappened(VirtualFileCreateEvent(file))
|
||||
|
||||
fileInfo.children.foreach{ addRecursively(it) }
|
||||
fileInfo.children.forEach{ addRecursively(it) }
|
||||
}
|
||||
|
||||
/* Deletes file from file system recursively, notifying listeners */
|
||||
private fun deleteRecursively(file : VirtualFile) {
|
||||
val fileInfoMaybe : VirtualFileInfo? = FileSystem.fileToInfo[file.path]
|
||||
val fileInfo = fileInfoMaybe.sure()
|
||||
fileInfo.children.foreach{ deleteRecursively(it) }
|
||||
fileInfo.children.forEach{ deleteRecursively(it) }
|
||||
FileSystem.notifyEventHappened(VirtualFileDeletedEvent(file))
|
||||
FileSystem.fileToInfo.remove(file)
|
||||
}
|
||||
@@ -115,7 +114,7 @@ internal object RefreshQueue {
|
||||
public fun scheduleRefresh(vararg files : VirtualFile) {
|
||||
// FIXME This could be written more concise, using map() & toList() (KT-1164 & KT-1172)
|
||||
val filesList = ArrayList<VirtualFile>()
|
||||
files.foreach{ filesList.add(it) }
|
||||
files.forEach{ filesList.add(it) }
|
||||
taskQueue.put(filesList)
|
||||
|
||||
// taskQueue.put(ArrayList<VirtualFile>(files.map{ it }))
|
||||
|
||||
@@ -63,38 +63,38 @@ public abstract class VirtualFile(public val path : String) {
|
||||
* Type of virtual file which corresponds to real file in file system of OS.
|
||||
*/
|
||||
public class PhysicalVirtualFile(path : String) : VirtualFile(path) {
|
||||
override fun kind() : String = "Physical"
|
||||
override public fun kind() : String = "Physical"
|
||||
|
||||
private val ioFile : File
|
||||
get() = File(this.path.toSystemDependentPath())
|
||||
|
||||
override fun exists(): Boolean {
|
||||
override public fun exists(): Boolean {
|
||||
FileSystem.assertCanRead()
|
||||
return ioFile.exists()
|
||||
}
|
||||
|
||||
override fun size(): Long {
|
||||
override public fun size(): Long {
|
||||
FileSystem.assertCanRead()
|
||||
return ioFile.length()
|
||||
}
|
||||
|
||||
override fun modificationTime(): Long {
|
||||
override public fun modificationTime(): Long {
|
||||
FileSystem.assertCanRead()
|
||||
return ioFile.lastModified()
|
||||
}
|
||||
|
||||
override fun isDirectory(): Boolean {
|
||||
override public fun isDirectory(): Boolean {
|
||||
FileSystem.assertCanRead()
|
||||
return ioFile.isDirectory()
|
||||
}
|
||||
|
||||
override fun children(): List<VirtualFile> {
|
||||
override public fun children(): List<VirtualFile> {
|
||||
FileSystem.assertCanRead()
|
||||
return (ioFile.listFiles() ?: Array<File>(0)).
|
||||
return (ioFile.listFiles() ?: array<File?>()).
|
||||
map{ FileSystem.getFileByIoFile(it.sure()) }?.toList()
|
||||
}
|
||||
|
||||
override fun openInputStream(): InputStream {
|
||||
override public fun openInputStream(): InputStream {
|
||||
FileSystem.assertCanRead()
|
||||
if (isDirectory()) {
|
||||
throw IllegalArgumentException("Can't open directory for reading");
|
||||
@@ -117,47 +117,47 @@ private fun String.toSystemIndependentPath() : String {
|
||||
* InputStream wrapper which checks that file system read lock is acquired on each operation.
|
||||
*/
|
||||
private class CheckedInputStream(private val wrapped : InputStream) : InputStream() {
|
||||
override fun read(): Int {
|
||||
override public fun read(): Int {
|
||||
FileSystem.assertCanRead()
|
||||
return wrapped.read()
|
||||
}
|
||||
|
||||
override fun read(b: ByteArray?, off: Int, len: Int) : Int {
|
||||
override public fun read(b: ByteArray?, off: Int, len: Int) : Int {
|
||||
FileSystem.assertCanRead()
|
||||
return wrapped.read(b, off, len)
|
||||
}
|
||||
|
||||
override fun markSupported(): Boolean {
|
||||
override public fun markSupported(): Boolean {
|
||||
FileSystem.assertCanRead()
|
||||
return wrapped.markSupported()
|
||||
}
|
||||
|
||||
override fun skip(n: Long): Long {
|
||||
override public fun skip(n: Long): Long {
|
||||
FileSystem.assertCanRead()
|
||||
return wrapped.skip(n)
|
||||
}
|
||||
|
||||
override fun close() {
|
||||
override public fun close() {
|
||||
FileSystem.assertCanRead()
|
||||
return wrapped.close()
|
||||
}
|
||||
|
||||
override fun mark(readlimit: Int) {
|
||||
override public fun mark(readlimit: Int) {
|
||||
FileSystem.assertCanRead()
|
||||
return wrapped.mark(readlimit)
|
||||
}
|
||||
|
||||
override fun read(b: ByteArray?): Int {
|
||||
override public fun read(b: ByteArray?): Int {
|
||||
FileSystem.assertCanRead()
|
||||
return wrapped.read(b)
|
||||
}
|
||||
|
||||
override fun reset() {
|
||||
override public fun reset() {
|
||||
FileSystem.assertCanRead()
|
||||
return wrapped.reset()
|
||||
}
|
||||
|
||||
override fun available(): Int {
|
||||
override public fun available(): Int {
|
||||
FileSystem.assertCanRead()
|
||||
return wrapped.available()
|
||||
}
|
||||
|
||||
@@ -7,6 +7,14 @@ public trait VirtualFileListener : java.util.EventListener {
|
||||
fun eventHappened(val event : VirtualFileEvent)
|
||||
}
|
||||
|
||||
// FIXME using this wrapper because of codegen bug KT-1737, should be replaced with
|
||||
private class SimpleVirtualFileListener(val listenerFunction : (VirtualFileEvent)->Unit) : VirtualFileListener {
|
||||
override fun eventHappened(event : VirtualFileEvent) {
|
||||
val f = listenerFunction // FIXME saving to local variable because of the bug in codegen (KT-1739)
|
||||
f(event)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Base type of virtual file events
|
||||
* @property file affected by event
|
||||
@@ -18,7 +26,7 @@ public abstract class VirtualFileEvent(val file : VirtualFile) : Object() {
|
||||
* Event of creating file
|
||||
*/
|
||||
public class VirtualFileCreateEvent(file : VirtualFile) : VirtualFileEvent(file) {
|
||||
override fun toString(): String? {
|
||||
override public fun toString(): String? {
|
||||
return "created ${file}"
|
||||
}
|
||||
}
|
||||
@@ -27,7 +35,7 @@ public class VirtualFileCreateEvent(file : VirtualFile) : VirtualFileEvent(file)
|
||||
* Event of deleting file
|
||||
*/
|
||||
public class VirtualFileDeletedEvent(file : VirtualFile) : VirtualFileEvent(file) {
|
||||
override fun toString(): String? {
|
||||
override public fun toString(): String? {
|
||||
return "deleted ${file}"
|
||||
}
|
||||
}
|
||||
@@ -36,7 +44,7 @@ public class VirtualFileDeletedEvent(file : VirtualFile) : VirtualFileEvent(file
|
||||
* Event of changing file contents
|
||||
*/
|
||||
public class VirtualFileChangedEvent(file : VirtualFile) : VirtualFileEvent(file) {
|
||||
override fun toString(): String? {
|
||||
override public fun toString(): String? {
|
||||
return "changed ${file}"
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user