lesson 9 · AlbertoJava/basejava@b79f7cb · GitHub
Skip to content

Commit b79f7cb

Browse files
committed
lesson 9
добавлен интерфейс StreamSerializer
1 parent 8cd2a4f commit b79f7cb

10 files changed

Lines changed: 55 additions & 48 deletions

src/com/urise/webapp/storage/AbstractFileStorage.java renamed to src/com/urise/webapp/storage/FileStorage.java

Lines changed: 7 additions & 7 deletions

src/com/urise/webapp/storage/ObjectStreamStorage.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,21 @@
22

33
import com.urise.webapp.exception.StorageException;
44
import com.urise.webapp.model.Resume;
5+
import com.urise.webapp.storage.serializer.StreamSerializer;
56

67
import java.io.*;
78

8-
public class ObjectStreamStorage extends AbstractFileStorage {
9-
10-
public ObjectStreamStorage(File directory) {
11-
super(directory);
12-
}
9+
public class ObjectStreamStorage implements StreamSerializer {
1310

1411
@Override
15-
protected void doWrite(Resume resume, OutputStream os) throws IOException {
12+
public void doWrite(Resume resume, OutputStream os) throws IOException {
1613
try (ObjectOutputStream oos = new ObjectOutputStream(os)) {
1714
oos.writeObject(resume);
1815
}
1916
}
2017

2118
@Override
22-
protected Resume doRead(InputStream is) throws IOException {
19+
public Resume doRead(InputStream is) throws IOException {
2320
try (ObjectInputStream ois = new ObjectInputStream(is)) {
2421
return (Resume) ois.readObject();
2522
} catch (ClassNotFoundException e) {

src/com/urise/webapp/storage/AbstractPathStorage.java renamed to src/com/urise/webapp/storage/PathStorage.java

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

33
import com.urise.webapp.exception.StorageException;
44
import com.urise.webapp.model.Resume;
5+
import com.urise.webapp.storage.serializer.StreamSerializer;
56

67
import java.io.*;
78
import java.nio.file.Files;
@@ -13,27 +14,25 @@
1314
import java.util.function.Consumer;
1415
import java.util.stream.Collectors;
1516

16-
public abstract class AbstractPathStorage extends AbstractStorage {
17+
public class PathStorage extends AbstractStorage {
1718
private Path directory;
19+
private StreamSerializer streamSerializer;
1820

19-
protected AbstractPathStorage(String dir) {
21+
protected PathStorage(String dir, StreamSerializer streamSerializer) {
2022
Path directory = Paths.get(dir);
2123
Objects.requireNonNull(directory, "directory must not null");
24+
this.streamSerializer=streamSerializer;
2225
if (!Files.isDirectory(directory)||!Files.isWritable(directory) ) {
2326
throw new IllegalArgumentException(dir + " is not directory or is not writable");
2427
}
2528
this.directory = directory;
2629
}
2730

28-
protected abstract void doWrite(Resume resume, OutputStream os) throws IOException;
29-
30-
protected abstract Resume doRead(InputStream is) throws IOException;
31-
3231
@Override
3332
protected void doUpdate(Resume resume, Object searchKey) {
3433
Path file = (Path) searchKey;
3534
try {
36-
doWrite(resume, new BufferedOutputStream(new FileOutputStream(file.toString())));
35+
streamSerializer.doWrite(resume, new BufferedOutputStream(new FileOutputStream(file.toString())));
3736
} catch (IOException e) {
3837
throw new StorageException("File write error", resume.getUuid(), e);
3938
}
@@ -60,7 +59,7 @@ protected void doSave(Resume resume, Object file) {
6059
protected Resume doGet(Object searchKey) {
6160
Path path = (Path) searchKey;
6261
try {
63-
return doRead(new BufferedInputStream(new FileInputStream(path.toString())));
62+
return streamSerializer.doRead(new BufferedInputStream(new FileInputStream(path.toString())));
6463
} catch (IOException e) {
6564
throw new StorageException("File read error", path.toString(), e);
6665
}

src/com/urise/webapp/storage/ObjectStreamPathStorage.java renamed to src/com/urise/webapp/storage/serializer/ObjectStreamSerializer.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,21 @@
1-
package com.urise.webapp.storage;
1+
package com.urise.webapp.storage.serializer;
22

33
import com.urise.webapp.exception.StorageException;
44
import com.urise.webapp.model.Resume;
55

66
import java.io.*;
77

8-
public class ObjectStreamPathStorage extends AbstractPathStorage {
9-
10-
public ObjectStreamPathStorage(String directory) {
11-
super(directory);
12-
}
8+
public class ObjectStreamSerializer implements StreamSerializer {
139

1410
@Override
15-
protected void doWrite(Resume resume, OutputStream os) throws IOException {
11+
public void doWrite(Resume resume, OutputStream os) throws IOException {
1612
try (ObjectOutputStream oos = new ObjectOutputStream(os)) {
1713
oos.writeObject(resume);
1814
}
1915
}
2016

2117
@Override
22-
protected Resume doRead(InputStream is) throws IOException {
18+
public Resume doRead(InputStream is) throws IOException {
2319
try (ObjectInputStream ois = new ObjectInputStream(is)) {
2420
return (Resume) ois.readObject();
2521
} catch (ClassNotFoundException e) {
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.urise.webapp.storage.serializer;
2+
3+
import com.urise.webapp.model.Resume;
4+
5+
import java.io.IOException;
6+
import java.io.InputStream;
7+
import java.io.OutputStream;
8+
9+
public interface StreamSerializer {
10+
void doWrite (Resume r, OutputStream os) throws IOException;
11+
Resume doRead (InputStream is) throws IOException;
12+
}

test/com/urise/webapp/storage/AllStorageTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
ListStorageTest.class,
1111
MapStorageTest.class,
1212
MapResumeStorageTest.class,
13-
ObjectStreamStorageTest.class,
14-
ObjectStreamPathStorageTest.class
13+
ObjectFileStorageTest.class,
14+
ObjectPathStorageTest.class
1515
})
1616
public class AllStorageTest {
1717
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.urise.webapp.storage;
2+
3+
import com.urise.webapp.storage.serializer.ObjectStreamSerializer;
4+
5+
public class ObjectFileStorageTest extends AbstractStorageTest {
6+
public ObjectFileStorageTest() {
7+
super(new FileStorage(STORAGE_DIR, new ObjectStreamSerializer()));
8+
}
9+
}
Lines changed: 10 additions & 0 deletions

test/com/urise/webapp/storage/ObjectStreamPathStorageTest.java

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

test/com/urise/webapp/storage/ObjectStreamStorageTest.java

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

0 commit comments

Comments
 (0)