View Javadoc

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