-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPathStorage.java
More file actions
111 lines (95 loc) · 3.28 KB
/
PathStorage.java
File metadata and controls
111 lines (95 loc) · 3.28 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
99
100
101
102
103
104
105
106
107
108
109
110
111
package webapp.storage;
import webapp.exceptions.StorageException;
import webapp.model.Resume;
import webapp.storage.serialization.SerializationStrategy;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Objects;
import java.util.UUID;
import java.util.stream.Collectors;
public class PathStorage extends AbstractStorage<Path> {
private Path dir;
private SerializationStrategy serializationStrategy;
PathStorage(String directory, SerializationStrategy serializationStrategy) {
Objects.requireNonNull(directory, "directory must not be null");
this.dir = Paths.get(directory);
this.serializationStrategy = serializationStrategy;
if (!Files.isDirectory(dir) || !Files.isWritable(dir)) {
throw new IllegalArgumentException(dir + " is not directory or is not writable");
}
}
@Override
public void clear() {
try {
Files.list(dir).forEach(this::doDelete);
} catch (IOException e) {
throw new StorageException("Clear: Path delete error", e);
}
}
@Override
public int size() {
try {
return (int) Files.list(dir).count();
} catch (IOException e) {
throw new StorageException("Size: Directory read error", e);
}
}
@Override
protected Path getSearchKey(UUID uuid) {
return dir.resolve(String.valueOf(uuid));
}
@Override
protected void doUpdate(Path path, Resume r) {
try {
serializationStrategy.doWrite(r, new BufferedOutputStream(Files.newOutputStream(path)));
} catch (IOException e) {
throw new StorageException("doUpdate: Path write error " + r.getUuid(), e);
}
}
@Override
protected boolean isExist(Path path) {
return Files.isRegularFile(path);
}
@Override
protected void doSave(Resume r, Path path) {
try {
Files.createFile(path);
} catch (IOException e) {
throw new StorageException("doSave: Couldn't create path " + path, e);
}
doUpdate(path, r);
}
@Override
protected Resume doGet(Path path) {
try {
return serializationStrategy.doRead(new BufferedInputStream(Files.newInputStream(path)));
} catch (IOException e) {
throw new StorageException("doGet: Path read error" + path, e);
}
}
@Override
protected void doDelete(Path path) {
if (!Files.exists(path)) {
throw new StorageException("doDelete: File " + path + " is not exist");
} else if (Files.isDirectory(path)) {
throw new StorageException("doDelete: File " + path + " is directory");
} else try {
Files.deleteIfExists(path);
} catch (IOException e) {
throw new StorageException("doDelete: Path delete error" + path, e);
}
}
@Override
protected List<Resume> doGetAll() {
try {
return Files.list(dir).map(this::doGet).collect(Collectors.toList());
} catch (IOException e) {
throw new StorageException("doGetAll: Directory not found ", e);
}
}
}