1 package cz.cuni.amis.utils;
2
3 import java.io.File;
4 import java.io.FileOutputStream;
5 import java.util.regex.Pattern;
6
7 public class FileMarker {
8
9 public static final String FILE_SEPARATOR = System.getProperty("file.separator");
10
11 private String prefix;
12 private File directory;
13
14 public FileMarker(String prefix, File directory) {
15 this.prefix = prefix;
16 this.directory = directory;
17 if (this.directory.exists()) {
18 if (!this.directory.isDirectory()) {
19 throw new RuntimeException("'directory' exists but it is not directory: " + directory.getAbsolutePath());
20 }
21 }
22 }
23
24 public FileMarker(String prefix, String pathToDirectory) {
25 this(prefix, new File(pathToDirectory));
26 }
27
28 public Pattern getMarkFileNamePattern() {
29 return Pattern.compile("^" + prefix + "\\-.*\\.mark$");
30 }
31
32 public File getFileMark(String mark) {
33 return new File(directory, prefix + "-" + mark + ".mark");
34 }
35
36 public File getFileMark(String mark, int index) {
37 return new File(directory, prefix + "-" + mark + "." + index + ".mark");
38 }
39
40 protected boolean isExists(File markFile) {
41 return markFile.exists() && markFile.isFile();
42 }
43
44 protected void touch(File markFile) {
45 if (!directory.exists()) {
46 directory.mkdirs();
47 }
48 if (markFile.exists()) {
49 if (markFile.isFile()) {
50 if (!markFile.delete()) {
51 throw new RuntimeException("Failed to touch the mark, could not delete old mark: " + markFile.getAbsolutePath());
52 }
53 } else {
54 throw new RuntimeException("Cannot touch the mark, because it already exists and it is not file: " + markFile.getAbsolutePath());
55 }
56 }
57
58 try {
59 new FileOutputStream(markFile).close();
60 } catch (Exception e) {
61 throw new RuntimeException("Failed to touch the mark: " + markFile.getAbsolutePath(), e);
62 }
63 }
64
65 public void remove(File markFile) {
66 if (markFile.exists() && markFile.isFile()) {
67 if (!markFile.delete()) {
68 throw new RuntimeException("Failed to delete the mark: " + markFile.getAbsolutePath());
69 }
70 }
71 }
72
73 public boolean isExists(String mark) {
74 File markFile = getFileMark(mark);
75 return isExists(markFile);
76 }
77
78 public void touch(String mark) {
79 File markFile = getFileMark(mark);
80 touch(markFile);
81 }
82
83 public void remove(String mark) {
84 File markFile = getFileMark(mark);
85 remove(markFile);
86 }
87
88 public boolean isExists(String mark, int index) {
89 File markFile = getFileMark(mark, index);
90 return isExists(markFile);
91 }
92
93 public void touch(String mark, int index) {
94 File markFile = getFileMark(mark, index);
95 touch(markFile);
96 }
97
98 public void remove(String mark, int index) {
99 File markFile = getFileMark(mark, index);
100 remove(markFile);
101 }
102
103 public void removeAllMarks() {
104 if (!directory.exists()) return;
105 Pattern pattern = getMarkFileNamePattern();
106 for (File file : directory.listFiles()) {
107 String fileName = file.getAbsolutePath().substring(file.getAbsolutePath().lastIndexOf(FILE_SEPARATOR)+1);
108 if (pattern.matcher(fileName).matches()) {
109 if (!file.delete()) {
110 throw new RuntimeException("Failed to delete mark: " + file.getAbsolutePath());
111 }
112 }
113 }
114 }
115
116 }