some tweaks of VFS example to be more Kotlin
This commit is contained in:
@@ -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.
|
||||
*/
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,8 +3,8 @@ package std
|
||||
import java.lang.Class
|
||||
import java.lang.Object
|
||||
|
||||
val <T> T.javaClass : Class<T>
|
||||
val <erased T> T.javaClass : Class<T>
|
||||
get() = jet.runtime.Intrinsics.getJavaClass(this) as Class<T>
|
||||
|
||||
val <T> TypeInfo<T>.javaClassForType : Class<T>
|
||||
val <erased T> TypeInfo<T>.javaClassForType : Class<T>
|
||||
get() = (this as org.jetbrains.jet.rt.TypeInfoImpl<T>).getJavaClass().sure()
|
||||
|
||||
Reference in New Issue
Block a user