Kotlin function to monitor remote file system events
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
0
down vote
favorite
I've been porting over a java library I wrote to Kotlin. The library itself attempts to abstract out much of the need to know about WatchService
's, WatchKey
's, and BlockingQueue
's that are necessary when monitoring the file system for events. This will be used by Java programs so taking steps to make it work as effortlessly as possible are required.
My goal is to provide concise, readable code that uses as many helpful kotlin features as possible. For example:
x?.let ...
(Really any of theapply
,also
,when
,run
)- Java interoperability
- Having as much null safety as I can
Are there any other ways I can improve this code by using (or removing if need be) Kotlin features?
(This is the 2nd largest function and lessons learned here can be applied to the others, which is why I haven't attached the whole class)
/**
* Acts pretty similarly to startLocalWatch
*
* @throws[RuntimeException] Thrown when a provided file path is invalid
*/
@Throws(RuntimeException::class)
fun watchRemote(timeout: Long = defaultPollTimeout): List<File>
val droppedFiles = ArrayList<File>()
this.ws.poll()?.let nonNullKey: WatchKey ->
nonNullKey.reset() // TODO: Test whether this works when placed AFTER for loop as well
for (event: WatchEvent<*> in nonNullKey.pollEvents())
(event.context() as? Path)?.also nonNullFilePath ->
logger.info("Path = $nonNullFilePath.toString()")
logger.debug("$event.kind() happened on file=$event.context()")
droppedFiles.add(nonNullFilePath.toFile())
?: throw RuntimeException("File path was not valid: $event.context()")
?: throw RuntimeException("The key returned from poll was null. Check your paths!")
return droppedFiles
kotlin
add a comment |Â
up vote
0
down vote
favorite
I've been porting over a java library I wrote to Kotlin. The library itself attempts to abstract out much of the need to know about WatchService
's, WatchKey
's, and BlockingQueue
's that are necessary when monitoring the file system for events. This will be used by Java programs so taking steps to make it work as effortlessly as possible are required.
My goal is to provide concise, readable code that uses as many helpful kotlin features as possible. For example:
x?.let ...
(Really any of theapply
,also
,when
,run
)- Java interoperability
- Having as much null safety as I can
Are there any other ways I can improve this code by using (or removing if need be) Kotlin features?
(This is the 2nd largest function and lessons learned here can be applied to the others, which is why I haven't attached the whole class)
/**
* Acts pretty similarly to startLocalWatch
*
* @throws[RuntimeException] Thrown when a provided file path is invalid
*/
@Throws(RuntimeException::class)
fun watchRemote(timeout: Long = defaultPollTimeout): List<File>
val droppedFiles = ArrayList<File>()
this.ws.poll()?.let nonNullKey: WatchKey ->
nonNullKey.reset() // TODO: Test whether this works when placed AFTER for loop as well
for (event: WatchEvent<*> in nonNullKey.pollEvents())
(event.context() as? Path)?.also nonNullFilePath ->
logger.info("Path = $nonNullFilePath.toString()")
logger.debug("$event.kind() happened on file=$event.context()")
droppedFiles.add(nonNullFilePath.toFile())
?: throw RuntimeException("File path was not valid: $event.context()")
?: throw RuntimeException("The key returned from poll was null. Check your paths!")
return droppedFiles
kotlin
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I've been porting over a java library I wrote to Kotlin. The library itself attempts to abstract out much of the need to know about WatchService
's, WatchKey
's, and BlockingQueue
's that are necessary when monitoring the file system for events. This will be used by Java programs so taking steps to make it work as effortlessly as possible are required.
My goal is to provide concise, readable code that uses as many helpful kotlin features as possible. For example:
x?.let ...
(Really any of theapply
,also
,when
,run
)- Java interoperability
- Having as much null safety as I can
Are there any other ways I can improve this code by using (or removing if need be) Kotlin features?
(This is the 2nd largest function and lessons learned here can be applied to the others, which is why I haven't attached the whole class)
/**
* Acts pretty similarly to startLocalWatch
*
* @throws[RuntimeException] Thrown when a provided file path is invalid
*/
@Throws(RuntimeException::class)
fun watchRemote(timeout: Long = defaultPollTimeout): List<File>
val droppedFiles = ArrayList<File>()
this.ws.poll()?.let nonNullKey: WatchKey ->
nonNullKey.reset() // TODO: Test whether this works when placed AFTER for loop as well
for (event: WatchEvent<*> in nonNullKey.pollEvents())
(event.context() as? Path)?.also nonNullFilePath ->
logger.info("Path = $nonNullFilePath.toString()")
logger.debug("$event.kind() happened on file=$event.context()")
droppedFiles.add(nonNullFilePath.toFile())
?: throw RuntimeException("File path was not valid: $event.context()")
?: throw RuntimeException("The key returned from poll was null. Check your paths!")
return droppedFiles
kotlin
I've been porting over a java library I wrote to Kotlin. The library itself attempts to abstract out much of the need to know about WatchService
's, WatchKey
's, and BlockingQueue
's that are necessary when monitoring the file system for events. This will be used by Java programs so taking steps to make it work as effortlessly as possible are required.
My goal is to provide concise, readable code that uses as many helpful kotlin features as possible. For example:
x?.let ...
(Really any of theapply
,also
,when
,run
)- Java interoperability
- Having as much null safety as I can
Are there any other ways I can improve this code by using (or removing if need be) Kotlin features?
(This is the 2nd largest function and lessons learned here can be applied to the others, which is why I haven't attached the whole class)
/**
* Acts pretty similarly to startLocalWatch
*
* @throws[RuntimeException] Thrown when a provided file path is invalid
*/
@Throws(RuntimeException::class)
fun watchRemote(timeout: Long = defaultPollTimeout): List<File>
val droppedFiles = ArrayList<File>()
this.ws.poll()?.let nonNullKey: WatchKey ->
nonNullKey.reset() // TODO: Test whether this works when placed AFTER for loop as well
for (event: WatchEvent<*> in nonNullKey.pollEvents())
(event.context() as? Path)?.also nonNullFilePath ->
logger.info("Path = $nonNullFilePath.toString()")
logger.debug("$event.kind() happened on file=$event.context()")
droppedFiles.add(nonNullFilePath.toFile())
?: throw RuntimeException("File path was not valid: $event.context()")
?: throw RuntimeException("The key returned from poll was null. Check your paths!")
return droppedFiles
kotlin
asked Jul 17 at 17:23
Scrambo
1012
1012
add a comment |Â
add a comment |Â
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f199702%2fkotlin-function-to-monitor-remote-file-system-events%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password