Skip to content

Commit 4b37c38

Browse files
author
Tanechka
committed
Lesson09 AbstractPathStorage
1 parent 7a80d1d commit 4b37c38

1 file changed

Lines changed: 104 additions & 0 deletions

File tree

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package ru.javawebinar.basejava.storage;
2+
3+
import ru.javawebinar.basejava.exception.StorageException;
4+
import ru.javawebinar.basejava.model.Resume;
5+
6+
import java.io.*;
7+
import java.nio.file.Files;
8+
import java.nio.file.Path;
9+
import java.nio.file.Paths;
10+
import java.util.ArrayList;
11+
import java.util.List;
12+
import java.util.Objects;
13+
14+
public abstract class AbstractPathStorage extends AbstractStorage<Path> {
15+
private Path directory;
16+
17+
protected abstract void doWrite(Resume r, OutputStream os) throws IOException;
18+
19+
protected abstract Resume doRead(InputStream is) throws IOException;
20+
21+
protected AbstractPathStorage(String dir) {
22+
directory = Paths.get(dir);
23+
Objects.requireNonNull(directory, "directory must not be null");
24+
if (!Files.isDirectory(directory) || !Files.isWritable(directory)) {
25+
throw new IllegalArgumentException(dir + " is not directory or is not writable");
26+
}
27+
}
28+
29+
@Override
30+
public void clear() {
31+
try {
32+
Files.list(directory).forEach(this::doDelete);
33+
} catch (IOException e) {
34+
throw new StorageException("Path delete error", null);
35+
}
36+
}
37+
38+
@Override
39+
public int size() {
40+
String[] list = directory.list();
41+
if (list == null) {
42+
throw new StorageException("Directory read error", null);
43+
}
44+
return list.length;
45+
}
46+
47+
@Override
48+
protected Path getSearchKey(String uuid) {
49+
return new Path(directory, uuid);
50+
}
51+
52+
@Override
53+
protected void doUpdate(Resume r, Path Path) {
54+
try {
55+
doWrite(r, new BufferedOutputStream(new PathOutputStream(Path)));
56+
} catch (IOException e) {
57+
throw new StorageException("Path write error", r.getUuid(), e);
58+
}
59+
}
60+
61+
@Override
62+
protected boolean isExist(Path Path) {
63+
return Path.exists();
64+
}
65+
66+
@Override
67+
protected void doSave(Resume r, Path Path) {
68+
try {
69+
Path.createNewPath();
70+
} catch (IOException e) {
71+
throw new StorageException("Couldn't create Path " + Path.getAbsolutePath(), Path.getName(), e);
72+
}
73+
doUpdate(r, Path);
74+
}
75+
76+
@Override
77+
protected Resume doGet(Path Path) {
78+
try {
79+
return doRead(new BufferedInputStream(new PathInputStream(Path)));
80+
} catch (IOException e) {
81+
throw new StorageException("Path read error", Path.getName(), e);
82+
}
83+
}
84+
85+
@Override
86+
protected void doDelete(Path Path) {
87+
if (!Path.delete()) {
88+
throw new StorageException("Path delete error", Path.getName());
89+
}
90+
}
91+
92+
@Override
93+
protected List<Resume> doCopyAll() {
94+
Path[] Paths = directory.listPaths();
95+
if (Paths == null) {
96+
throw new StorageException("Directory read error", null);
97+
}
98+
List<Resume> list = new ArrayList<>(Paths.length);
99+
for (Path Path : Paths) {
100+
list.add(doGet(Path));
101+
}
102+
return list;
103+
}
104+
}

0 commit comments

Comments
 (0)