]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.utils/src/org/simantics/utils/FileService.java
Merge branch 'feature/funcwrite'
[simantics/platform.git] / bundles / org.simantics.utils / src / org / simantics / utils / FileService.java
1 package org.simantics.utils;
2
3 import java.io.File;
4 import java.io.IOException;
5
6 /**
7  * @author Tuukka Lehtonen
8  */
9 public interface FileService {
10
11         public static interface DeleteOption {}
12
13         public static class EffortOption implements DeleteOption {
14                 public final int maxTries;
15
16                 private EffortOption(int maxTries) {
17                         this.maxTries = maxTries;
18                 }
19
20                 public static EffortOption maxTries(int maxTries) {
21                         return new EffortOption(maxTries);
22                 }
23         }
24
25         @FunctionalInterface
26         public static interface FileOperation {
27                 IOperation<Boolean, IOException> perform(File file);
28
29                 default void perform(File... files) {
30                         for (File f : files)
31                                 perform(f);
32                 }
33         }
34
35         public static class DeleteOperation implements FileOperation {
36                 private final FileService service;
37                 private final DeleteOption[] options;
38
39                 public DeleteOperation(FileService service, DeleteOption... options) {
40                         this.service = service;
41                         this.options = options;
42                 }
43
44                 @Override
45                 public IOperation<Boolean, IOException> perform(File file) {
46                         return service.scheduleDeleteIfExists(file, options);
47                 }
48         }
49
50         /**
51          * Schedules a file to be deleted at some point in the future when the
52          * system allows it to be deleted if ever.
53          * 
54          * @param file
55          * @return an operation handle for tracking the progress of the deletion or
56          *         <code>null</code> if there's a deletion operation scheduled for
57          *         the provided file in the queue or the file does not exist.
58          */
59         IOperation<Boolean, IOException> scheduleDeleteIfExists(File file, DeleteOption... options);
60
61         /**
62          * @param options
63          *            the deletion options to be used by the returned FileOperation
64          * @return a FileOperation that runs
65          *         {@link #scheduleDeleteIfExists(File, DeleteOption...)} for any
66          *         files given to it
67          * @since 1.28.0
68          */
69         default FileOperation deleteOperation(DeleteOption... options) {
70                 return new DeleteOperation(this, options);
71         }
72
73 }