Skip to content

Commit cf53271

Browse files
author
Tanechka
committed
Lesson10 HW09
1 parent 4b37c38 commit cf53271

13 files changed

Lines changed: 179 additions & 139 deletions

src/ru/javawebinar/basejava/MainFile.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,19 @@ public static void main(String[] args) {
3333
} catch (IOException e) {
3434
throw new RuntimeException(e);
3535
}
36-
printDirectoryDeeply(dir);
36+
printDirectoryDeeply(dir, "");
3737
}
38-
39-
// TODO: make pretty output
40-
public static void printDirectoryDeeply(File dir) {
38+
39+
public static void printDirectoryDeeply(File dir, String offset) {
4140
File[] files = dir.listFiles();
4241

4342
if (files != null) {
4443
for (File file : files) {
4544
if (file.isFile()) {
46-
System.out.println("File: " + file.getName());
45+
System.out.println(offset + "F: " + file.getName());
4746
} else if (file.isDirectory()) {
48-
System.out.println("Directory: " + file.getName());
49-
printDirectoryDeeply(file);
47+
System.out.println(offset + "D: " + file.getName());
48+
printDirectoryDeeply(file, offset + " ");
5049
}
5150
}
5251
}

src/ru/javawebinar/basejava/storage/MainUtil.java renamed to src/ru/javawebinar/basejava/MainUtil.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package ru.javawebinar.basejava.storage;
1+
package ru.javawebinar.basejava;
22

33
public class MainUtil {
44
public static void main(String[] args) {

src/ru/javawebinar/basejava/exception/StorageException.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,19 @@
33
public class StorageException extends RuntimeException {
44
private final String uuid;
55

6+
public StorageException(String message) {
7+
this(message, null, null);
8+
}
9+
610
public StorageException(String message, String uuid) {
711
super(message);
812
this.uuid = uuid;
913
}
1014

15+
public StorageException(String message, Exception e) {
16+
this(message, null, e);
17+
}
18+
1119
public StorageException(String message, String uuid, Exception e) {
1220
super(message, e);
1321
this.uuid = uuid;

src/ru/javawebinar/basejava/storage/AbstractPathStorage.java

Lines changed: 0 additions & 104 deletions
This file was deleted.

src/ru/javawebinar/basejava/storage/AbstractFileStorage.java renamed to src/ru/javawebinar/basejava/storage/FileStorage.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import ru.javawebinar.basejava.exception.StorageException;
44
import ru.javawebinar.basejava.model.Resume;
5+
import ru.javawebinar.basejava.storage.serializer.StreamSerializer;
56

67
import java.io.*;
78
import java.util.ArrayList;
@@ -12,15 +13,15 @@
1213
* gkislin
1314
* 22.07.2016
1415
*/
15-
public abstract class AbstractFileStorage extends AbstractStorage<File> {
16+
public class FileStorage extends AbstractStorage<File> {
1617
private File directory;
1718

18-
protected abstract void doWrite(Resume r, OutputStream os) throws IOException;
19+
private StreamSerializer streamSerializer;
1920

20-
protected abstract Resume doRead(InputStream is) throws IOException;
21-
22-
protected AbstractFileStorage(File directory) {
21+
protected FileStorage(File directory, StreamSerializer streamSerializer) {
2322
Objects.requireNonNull(directory, "directory must not be null");
23+
24+
this.streamSerializer = streamSerializer;
2425
if (!directory.isDirectory()) {
2526
throw new IllegalArgumentException(directory.getAbsolutePath() + " is not directory");
2627
}
@@ -44,7 +45,7 @@ public void clear() {
4445
public int size() {
4546
String[] list = directory.list();
4647
if (list == null) {
47-
throw new StorageException("Directory read error", null);
48+
throw new StorageException("Directory read error");
4849
}
4950
return list.length;
5051
}
@@ -57,7 +58,7 @@ protected File getSearchKey(String uuid) {
5758
@Override
5859
protected void doUpdate(Resume r, File file) {
5960
try {
60-
doWrite(r, new BufferedOutputStream(new FileOutputStream(file)));
61+
streamSerializer.doWrite(r, new BufferedOutputStream(new FileOutputStream(file)));
6162
} catch (IOException e) {
6263
throw new StorageException("File write error", r.getUuid(), e);
6364
}
@@ -81,7 +82,7 @@ protected void doSave(Resume r, File file) {
8182
@Override
8283
protected Resume doGet(File file) {
8384
try {
84-
return doRead(new BufferedInputStream(new FileInputStream(file)));
85+
return streamSerializer.doRead(new BufferedInputStream(new FileInputStream(file)));
8586
} catch (IOException e) {
8687
throw new StorageException("File read error", file.getName(), e);
8788
}
@@ -98,7 +99,7 @@ protected void doDelete(File file) {
9899
protected List<Resume> doCopyAll() {
99100
File[] files = directory.listFiles();
100101
if (files == null) {
101-
throw new StorageException("Directory read error", null);
102+
throw new StorageException("Directory read error");
102103
}
103104
List<Resume> list = new ArrayList<>(files.length);
104105
for (File file : files) {
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package ru.javawebinar.basejava.storage;
2+
3+
import ru.javawebinar.basejava.exception.StorageException;
4+
import ru.javawebinar.basejava.model.Resume;
5+
import ru.javawebinar.basejava.storage.serializer.StreamSerializer;
6+
7+
import java.io.BufferedInputStream;
8+
import java.io.BufferedOutputStream;
9+
import java.io.IOException;
10+
import java.nio.file.Files;
11+
import java.nio.file.Path;
12+
import java.nio.file.Paths;
13+
import java.util.List;
14+
import java.util.Objects;
15+
import java.util.stream.Collectors;
16+
import java.util.stream.Stream;
17+
18+
public class PathStorage extends AbstractStorage<Path> {
19+
private Path directory;
20+
21+
private StreamSerializer streamSerializer;
22+
23+
protected PathStorage(String dir, StreamSerializer streamSerializer) {
24+
Objects.requireNonNull(dir, "directory must not be null");
25+
26+
this.streamSerializer = streamSerializer;
27+
directory = Paths.get(dir);
28+
if (!Files.isDirectory(directory) || !Files.isWritable(directory)) {
29+
throw new IllegalArgumentException(dir + " is not directory or is not writable");
30+
}
31+
}
32+
33+
@Override
34+
public void clear() {
35+
getFilesList().forEach(this::doDelete);
36+
}
37+
38+
@Override
39+
public int size() {
40+
return (int) getFilesList().count();
41+
}
42+
43+
@Override
44+
protected Path getSearchKey(String uuid) {
45+
return directory.resolve(uuid);
46+
}
47+
48+
@Override
49+
protected void doUpdate(Resume r, Path path) {
50+
try {
51+
streamSerializer.doWrite(r, new BufferedOutputStream(Files.newOutputStream(path)));
52+
} catch (IOException e) {
53+
throw new StorageException("Path write error", r.getUuid(), e);
54+
}
55+
}
56+
57+
@Override
58+
protected boolean isExist(Path path) {
59+
return Files.isRegularFile(path);
60+
}
61+
62+
@Override
63+
protected void doSave(Resume r, Path path) {
64+
try {
65+
Files.createFile(path);
66+
} catch (IOException e) {
67+
throw new StorageException("Couldn't create path " + path, getFileName(path), e);
68+
}
69+
doUpdate(r, path);
70+
}
71+
72+
@Override
73+
protected Resume doGet(Path path) {
74+
try {
75+
return streamSerializer.doRead(new BufferedInputStream(Files.newInputStream(path)));
76+
} catch (IOException e) {
77+
throw new StorageException("Path read error", getFileName(path), e);
78+
}
79+
}
80+
81+
@Override
82+
protected void doDelete(Path path) {
83+
try {
84+
Files.delete(path);
85+
} catch (IOException e) {
86+
throw new StorageException("Path delete error", getFileName(path), e);
87+
}
88+
}
89+
90+
@Override
91+
protected List<Resume> doCopyAll() {
92+
return getFilesList().map(this::doGet).collect(Collectors.toList());
93+
}
94+
95+
private String getFileName(Path path) {
96+
return path.getFileName().toString();
97+
}
98+
99+
private Stream<Path> getFilesList() {
100+
try {
101+
return Files.list(directory);
102+
} catch (IOException e) {
103+
throw new StorageException("Directory read error", e);
104+
}
105+
}
106+
}

src/ru/javawebinar/basejava/storage/ObjectStreamStorage.java renamed to src/ru/javawebinar/basejava/storage/serializer/ObjectStreamSerializer.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
1-
package ru.javawebinar.basejava.storage;
1+
package ru.javawebinar.basejava.storage.serializer;
22

33
import ru.javawebinar.basejava.exception.StorageException;
44
import ru.javawebinar.basejava.model.Resume;
55

66
import java.io.*;
77

8-
public class ObjectStreamStorage extends AbstractFileStorage {
9-
protected ObjectStreamStorage(File directory) {
10-
super(directory);
11-
}
8+
/**
9+
* Created by val on 2017-08-05.
10+
*/
11+
public class ObjectStreamSerializer implements StreamSerializer {
1212

1313
@Override
14-
protected void doWrite(Resume r, OutputStream os) throws IOException {
14+
public void doWrite(Resume r, OutputStream os) throws IOException {
1515
try (ObjectOutputStream oos = new ObjectOutputStream(os)) {
1616
oos.writeObject(r);
1717
}
1818
}
1919

2020
@Override
21-
protected Resume doRead(InputStream is) throws IOException {
21+
public Resume doRead(InputStream is) throws IOException {
2222
try (ObjectInputStream ois = new ObjectInputStream(is)) {
2323
return (Resume) ois.readObject();
2424
} catch (ClassNotFoundException e) {
2525
throw new StorageException("Error read resume", null, e);
2626
}
2727
}
28-
}
28+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package ru.javawebinar.basejava.storage.serializer;
2+
3+
import ru.javawebinar.basejava.model.Resume;
4+
5+
import java.io.IOException;
6+
import java.io.InputStream;
7+
import java.io.OutputStream;
8+
9+
/**
10+
* Created by val on 2017-08-05.
11+
*/
12+
public interface StreamSerializer {
13+
void doWrite(Resume r, OutputStream os) throws IOException;
14+
15+
Resume doRead(InputStream is) throws IOException;
16+
}

test/ru/javawebinar/basejava/storage/AbstractStorageTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import static org.junit.Assert.assertTrue;
1616

1717
public abstract class AbstractStorageTest {
18-
protected static final File STORAGE_DIR = new File("C:\\projects\\storage");
18+
protected static final File STORAGE_DIR = new File("C:\\projects\\basejava\\storage");
1919

2020
protected Storage storage;
2121

0 commit comments

Comments
 (0)