Kotlin function to monitor remote file system events

The name of the pictureThe name of the pictureThe name of the pictureClash 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 the apply, 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







share|improve this question

























    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 the apply, 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







    share|improve this question





















      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 the apply, 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







      share|improve this question











      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 the apply, 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









      share|improve this question










      share|improve this question




      share|improve this question









      asked Jul 17 at 17:23









      Scrambo

      1012




      1012

























          active

          oldest

          votes











          Your Answer




          StackExchange.ifUsing("editor", function ()
          return StackExchange.using("mathjaxEditing", function ()
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          );
          );
          , "mathjax-editing");

          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "196"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );








           

          draft saved


          draft discarded


















          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



































          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes










           

          draft saved


          draft discarded


























           


          draft saved


          draft discarded














          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













































































          Popular posts from this blog

          Chat program with C++ and SFML

          Function to Return a JSON Like Objects Using VBA Collections and Arrays

          Will my employers contract hold up in court?