]> gerrit.simantics Code Review - simantics/platform.git/commitdiff
Added functional FileOperation interface to FileService. 72/372/2
authorTuukka Lehtonen <tuukka.lehtonen@semantum.fi>
Mon, 20 Mar 2017 11:59:02 +0000 (13:59 +0200)
committerTuukka Lehtonen <tuukka.lehtonen@semantum.fi>
Mon, 20 Mar 2017 11:59:50 +0000 (13:59 +0200)
This makes using method references with FileService easier/possible and
provides for cleaner code.

refs #7100

Change-Id: I6929654b5f9d41cbad9fe4b35b2219d91e64a791

bundles/org.simantics.utils/src/org/simantics/utils/FileService.java

index c500c66350b937e6d5d6d61ff6e9d7800ffc4115..ab3d249d3bd95e157dcdb6af12caa87dba737353 100644 (file)
@@ -22,6 +22,31 @@ public interface FileService {
                }
        }
 
+       @FunctionalInterface
+       public static interface FileOperation {
+               IOperation<Boolean, IOException> perform(File file);
+
+               default void perform(File... files) {
+                       for (File f : files)
+                               perform(f);
+               }
+       }
+
+       public static class DeleteOperation implements FileOperation {
+               private final FileService service;
+               private final DeleteOption[] options;
+
+               public DeleteOperation(FileService service, DeleteOption... options) {
+                       this.service = service;
+                       this.options = options;
+               }
+
+               @Override
+               public IOperation<Boolean, IOException> perform(File file) {
+                       return service.scheduleDeleteIfExists(file, options);
+               }
+       }
+
        /**
         * Schedules a file to be deleted at some point in the future when the
         * system allows it to be deleted if ever.
@@ -33,4 +58,16 @@ public interface FileService {
         */
        IOperation<Boolean, IOException> scheduleDeleteIfExists(File file, DeleteOption... options);
 
+       /**
+        * @param options
+        *            the deletion options to be used by the returned FileOperation
+        * @return a FileOperation that runs
+        *         {@link #scheduleDeleteIfExists(File, DeleteOption...)} for any
+        *         files given to it
+        * @since 1.28.0
+        */
+       default FileOperation deleteOperation(DeleteOption... options) {
+               return new DeleteOperation(this, options);
+       }
+
 }