some tweaks of VFS example to be more Kotlin
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
package org.jetbrains.jet.samples.vfs;
|
package org.jetbrains.jet.samples.vfs;
|
||||||
|
|
||||||
|
import std.concurrent.*
|
||||||
import java.util.concurrent.locks.ReadWriteLock;
|
import java.util.concurrent.locks.ReadWriteLock;
|
||||||
import java.util.concurrent.locks.ReentrantReadWriteLock;
|
import java.util.concurrent.locks.ReentrantReadWriteLock;
|
||||||
import org.jetbrains.jet.samples.vfs.utils.*;
|
import org.jetbrains.jet.samples.vfs.utils.*;
|
||||||
@@ -12,6 +13,7 @@ import java.util.Arrays
|
|||||||
import java.util.Map
|
import java.util.Map
|
||||||
|
|
||||||
import std.util.*
|
import std.util.*
|
||||||
|
import java.util.TimerTask
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* File system singleton. To work with virtual file system, read/write locks should be
|
* 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.
|
* Runs function with read lock.
|
||||||
*/
|
*/
|
||||||
public inline fun read<T>(task : () -> T) : T {
|
public inline fun read<T>(task : () -> T) : T {
|
||||||
return locked(lock.readLock().sure(), task)
|
return lock.read<T>(task)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Runs function with write lock.
|
* Runs function with write lock.
|
||||||
*/
|
*/
|
||||||
public inline fun write<T>(task : () -> T) : T {
|
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)
|
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.
|
* Removes file system listener.
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -16,20 +16,18 @@ fun main(args : Array<String>) {
|
|||||||
|
|
||||||
// add watched directory
|
// add watched directory
|
||||||
FileSystem.write {
|
FileSystem.write {
|
||||||
for (val arg in args) {
|
for (arg in args) {
|
||||||
FileSystem.addWatchedDirectory(FileSystem.getFileByPath(arg))
|
FileSystem.addWatchedDirectory(FileSystem.getFileByPath(arg))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// add listener which prints out everything
|
// add listener which prints out everything
|
||||||
FileSystem.addVirtualFileListener(object : VirtualFileListener {
|
FileSystem.addVirtualFileListener{ event ->
|
||||||
override fun eventHappened(event: VirtualFileEvent) {
|
|
||||||
println(event)
|
println(event)
|
||||||
if (event is VirtualFileChangedEvent) {
|
if (event is VirtualFileChangedEvent) {
|
||||||
println("new file size is ${event.file.size()}")
|
println("new file size is ${event.file.size()}")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
|
||||||
|
|
||||||
// wait for 1 minute
|
// wait for 1 minute
|
||||||
Thread.sleep(60000)
|
Thread.sleep(60000)
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import java.util.concurrent.LinkedBlockingQueue
|
|||||||
import java.util.ArrayList
|
import java.util.ArrayList
|
||||||
|
|
||||||
import std.util.*
|
import std.util.*
|
||||||
|
import std.concurrent.*
|
||||||
import java.util.Timer
|
import java.util.Timer
|
||||||
import java.util.TimerTask
|
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
|
* Singleton which creates thread for periodically checking if there are changes in
|
||||||
* file system and notifying file system listeners.
|
* file system and notifying file system listeners.
|
||||||
*/
|
*/
|
||||||
internal object RefreshQueue : Runnable {
|
internal object RefreshQueue {
|
||||||
private val taskQueue = LinkedBlockingQueue<List<VirtualFile>>()
|
private val taskQueue = LinkedBlockingQueue<List<VirtualFile>>();
|
||||||
private val workerThread: Thread = Thread(this)
|
|
||||||
private val fullRefreshScheduler = Timer(true);
|
|
||||||
|
|
||||||
{
|
{
|
||||||
workerThread.setDaemon(true)
|
thread(daemon=true, name="refresher thread") {
|
||||||
workerThread.start()
|
while (!currentThread.isInterrupted()) {
|
||||||
|
try {
|
||||||
|
takeAndRefreshFiles()
|
||||||
|
} catch (e : InterruptedException) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fullRefreshScheduler.scheduleAtFixedRate(createTimerTask {
|
fixedRateTimer(daemon=true, name="refresher timer", period=5000.lng) {
|
||||||
scheduleRefresh(FileSystem.watchedDirectories)
|
scheduleRefresh(FileSystem.watchedDirectories)
|
||||||
}, 0, 5000)
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun takeAndRefreshFiles() {
|
private fun takeAndRefreshFiles() {
|
||||||
@@ -114,25 +119,4 @@ internal object RefreshQueue : Runnable {
|
|||||||
// taskQueue.put(ArrayList<VirtualFile>(files.map{ it }))
|
// taskQueue.put(ArrayList<VirtualFile>(files.map{ it }))
|
||||||
// taskQueue.put(files.map{ it }.toList())
|
// 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.Class
|
||||||
import java.lang.Object
|
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>
|
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()
|
get() = (this as org.jetbrains.jet.rt.TypeInfoImpl<T>).getJavaClass().sure()
|
||||||
|
|||||||
Reference in New Issue
Block a user