-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMonitor.java
More file actions
98 lines (85 loc) · 2.66 KB
/
Monitor.java
File metadata and controls
98 lines (85 loc) · 2.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package com.yurii.salimov.lesson08.task04;
import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* @author Yuriy Salimov (yuriy.alex.salimov@gmail.com)
* @version 1.0
*/
public class Monitor {
private final String path;
private final int timeout;
private final IFileEvents events;
private final List<String> prev = new ArrayList<>();
private final List<String> curr = new ArrayList<>();
private final List<String> dateList = new ArrayList<>();
public Monitor(final String path, final int timeout, final IFileEvents events) {
this.path = path;
this.timeout = timeout;
this.events = events;
}
public void start() {
checkDirectory(this.path);
while (true) {
createArray(this.prev);
createArray(this.curr);
compareArrays(this.prev, this.curr);
this.prev.clear();
this.prev.addAll(curr);
System.out.println("Waiting...");
try {
Thread.sleep(this.timeout);
} catch (InterruptedException ex) {
return;
}
}
}
public int getTimeout() {
return this.timeout;
}
public IFileEvents getEvents() {
return this.events;
}
public List getDateList() {
return this.dateList;
}
private void compareArrays(final List<String> list1, final List<String> list2) {
list1.stream()
.filter(path -> !list2.contains(path))
.forEach(this::doFileDeleted);
list2.stream()
.filter(path -> !list1.contains(path))
.forEach(this::doFileAdded);
}
private void doFileAdded(final String path) {
if (this.events != null) {
this.events.onFileAdded(path);
}
}
private void doFileDeleted(final String path) {
if (this.events != null) {
this.events.onFileDeleted(path);
}
}
private void createArray(final List<String> output) {
try {
output.clear();
File[] list = new File(this.path).listFiles();
String info;
for (File file : list) {
info = file.getCanonicalPath() + ", date: " + new Date(file.lastModified());
output.add(info);
this.dateList.add(info);
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
private static void checkDirectory(final String path) {
File directory = new File(path);
if (!directory.exists() || !directory.isDirectory()) {
directory.mkdirs();
}
}
}