some tweaks of VFS example to be more Kotlin

This commit is contained in:
Alex Tkachman
2012-02-01 10:48:28 +02:00
parent 5f9bab6c93
commit 9559decfc0
4 changed files with 35 additions and 38 deletions
+17 -2
View File
@@ -1,5 +1,6 @@
package org.jetbrains.jet.samples.vfs;
import std.concurrent.*
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import org.jetbrains.jet.samples.vfs.utils.*;
@@ -12,6 +13,7 @@ import java.util.Arrays
import java.util.Map
import std.util.*
import java.util.TimerTask
/**
* File system singleton. To work with virtual file system, read/write locks should be
@@ -52,14 +54,14 @@ public object FileSystem {
* Runs function with read lock.
*/
public inline fun read<T>(task : () -> T) : T {
return locked(lock.readLock().sure(), task)
return lock.read<T>(task)
}
/**
* Runs function with write lock.
*/
public inline fun write<T>(task : () -> T) : T {
return locked(lock.writeLock().sure(), task)
return lock.write(task)
}
/**
@@ -102,6 +104,19 @@ public object FileSystem {
listeners.add(listener)
}
/**
* 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)
}
}
addVirtualFileListener(vfl)
return vfl
}
/**
* Removes file system listener.
*/
+3 -5
View File
@@ -16,20 +16,18 @@ fun main(args : Array<String>) {
// add watched directory
FileSystem.write {
for (val arg in args) {
for (arg in args) {
FileSystem.addWatchedDirectory(FileSystem.getFileByPath(arg))
}
}
// add listener which prints out everything
FileSystem.addVirtualFileListener(object : VirtualFileListener {
override fun eventHappened(event: VirtualFileEvent) {
FileSystem.addVirtualFileListener{ event ->
println(event)
if (event is VirtualFileChangedEvent) {
println("new file size is ${event.file.size()}")
}
}
})
}
// wait for 1 minute
Thread.sleep(60000)
+13 -29
View File
@@ -7,6 +7,7 @@ import java.util.concurrent.LinkedBlockingQueue
import java.util.ArrayList
import std.util.*
import std.concurrent.*
import java.util.Timer
import java.util.TimerTask
@@ -16,18 +17,22 @@ import org.jetbrains.jet.samples.vfs.utils.*
* Singleton which creates thread for periodically checking if there are changes in
* file system and notifying file system listeners.
*/
internal object RefreshQueue : Runnable {
private val taskQueue = LinkedBlockingQueue<List<VirtualFile>>()
private val workerThread: Thread = Thread(this)
private val fullRefreshScheduler = Timer(true);
internal object RefreshQueue {
private val taskQueue = LinkedBlockingQueue<List<VirtualFile>>();
{
workerThread.setDaemon(true)
workerThread.start()
thread(daemon=true, name="refresher thread") {
while (!currentThread.isInterrupted()) {
try {
takeAndRefreshFiles()
} catch (e : InterruptedException) {
}
}
}
fullRefreshScheduler.scheduleAtFixedRate(createTimerTask {
fixedRateTimer(daemon=true, name="refresher timer", period=5000.lng) {
scheduleRefresh(FileSystem.watchedDirectories)
}, 0, 5000)
}
}
private fun takeAndRefreshFiles() {
@@ -114,25 +119,4 @@ internal object RefreshQueue : Runnable {
// taskQueue.put(ArrayList<VirtualFile>(files.map{ it }))
// taskQueue.put(files.map{ it }.toList())
}
// FIXME RefreshQueue implements Runnable because of error on accessing
// private fields and methods from anonymous class (KT-1157 & KT-1159) (
override fun run() {
while (!workerThread.isInterrupted()) {
try {
takeAndRefreshFiles()
} catch (e : InterruptedException) {
}
}
}
}
// FIXME this method is used because of error on accessing
// private methods from anonymous class (KT-1157) (
private fun createTimerTask(task : () -> Unit) : TimerTask {
return object : TimerTask() {
override fun run() {
task()
}
}
}