Skip to content

Commit 7a80d1d

Browse files
author
Tanechka
committed
Lesson09 ObjectStreamStorage
1 parent 150e15f commit 7a80d1d

11 files changed

Lines changed: 72 additions & 19 deletions

File tree

src/ru/javawebinar/basejava/model/Link.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
package ru.javawebinar.basejava.model;
22

3+
import java.io.Serializable;
34
import java.util.Objects;
45

56
/**
67
* gkislin
78
* 14.07.2016
89
*/
9-
public class Link {
10+
public class Link implements Serializable {
11+
private static final long serialVersionUID = 1L;
12+
1013
private final String name;
1114
private final String url;
1215

src/ru/javawebinar/basejava/model/ListSection.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
* 14.07.2016
1010
*/
1111
public class ListSection extends Section {
12+
13+
private static final long serialVersionUID = 1L;
14+
1215
private final List<String> items;
1316

1417
public ListSection(String... items) {

src/ru/javawebinar/basejava/model/Organization.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package ru.javawebinar.basejava.model;
22

3+
import java.io.Serializable;
34
import java.time.LocalDate;
45
import java.time.Month;
56
import java.util.ArrayList;
@@ -10,7 +11,9 @@
1011
import static ru.javawebinar.basejava.util.DateUtil.NOW;
1112
import static ru.javawebinar.basejava.util.DateUtil.of;
1213

13-
public class Organization {
14+
public class Organization implements Serializable {
15+
private static final long serialVersionUID = 1L;
16+
1417
private final Link homePage;
1518
private List<Position> positions = new ArrayList<>();
1619

@@ -46,7 +49,7 @@ public String toString() {
4649
* gkislin
4750
* 28.07.2016
4851
*/
49-
public static class Position {
52+
public static class Position implements Serializable{
5053
private final LocalDate startDate;
5154
private final LocalDate endDate;
5255
private final String title;

src/ru/javawebinar/basejava/model/OrganizationSection.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
* 19.07.2016
1010
*/
1111
public class OrganizationSection extends Section {
12+
13+
private static final long serialVersionUID = 1L;
14+
1215
private final List<Organization> organizations;
1316

1417
public OrganizationSection(Organization... organizations) {

src/ru/javawebinar/basejava/model/Resume.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
package ru.javawebinar.basejava.model;
22

3+
import java.io.Serializable;
34
import java.util.EnumMap;
45
import java.util.Map;
56
import java.util.Objects;
67
import java.util.UUID;
78

8-
/**
9-
* com.urise.webapp.model.Resume class
10-
*/
11-
public class Resume implements Comparable<Resume> {
12-
9+
public class Resume implements Comparable<Resume>, Serializable {
10+
private static final long serialVersionUID = 1L;
1311
// Unique identifier
1412
private final String uuid;
1513

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package ru.javawebinar.basejava.model;
22

3+
import java.io.Serializable;
4+
35
/**
46
* gkislin
57
* 19.07.2016
68
*/
7-
abstract public class Section {
9+
abstract public class Section implements Serializable{
810
}

src/ru/javawebinar/basejava/model/TextSection.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
import java.util.Objects;
44

55
public class TextSection extends Section {
6+
7+
private static final long serialVersionUID = 1L;
8+
69
private final String content;
710

811
public TextSection(String content) {

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

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
import ru.javawebinar.basejava.exception.StorageException;
44
import ru.javawebinar.basejava.model.Resume;
55

6-
import java.io.File;
7-
import java.io.IOException;
6+
import java.io.*;
87
import java.util.ArrayList;
98
import java.util.List;
109
import java.util.Objects;
@@ -16,6 +15,10 @@
1615
public abstract class AbstractFileStorage extends AbstractStorage<File> {
1716
private File directory;
1817

18+
protected abstract void doWrite(Resume r, OutputStream os) throws IOException;
19+
20+
protected abstract Resume doRead(InputStream is) throws IOException;
21+
1922
protected AbstractFileStorage(File directory) {
2023
Objects.requireNonNull(directory, "directory must not be null");
2124
if (!directory.isDirectory()) {
@@ -54,7 +57,7 @@ protected File getSearchKey(String uuid) {
5457
@Override
5558
protected void doUpdate(Resume r, File file) {
5659
try {
57-
doWrite(r, file);
60+
doWrite(r, new BufferedOutputStream(new FileOutputStream(file)));
5861
} catch (IOException e) {
5962
throw new StorageException("File write error", r.getUuid(), e);
6063
}
@@ -75,14 +78,10 @@ protected void doSave(Resume r, File file) {
7578
doUpdate(r, file);
7679
}
7780

78-
protected abstract void doWrite(Resume r, File file) throws IOException;
79-
80-
protected abstract Resume doRead(File file) throws IOException;
81-
8281
@Override
8382
protected Resume doGet(File file) {
8483
try {
85-
return doRead(file);
84+
return doRead(new BufferedInputStream(new FileInputStream(file)));
8685
} catch (IOException e) {
8786
throw new StorageException("File read error", file.getName(), e);
8887
}
@@ -107,4 +106,4 @@ protected List<Resume> doCopyAll() {
107106
}
108107
return list;
109108
}
110-
}
109+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
8+
public class ObjectStreamStorage extends AbstractFileStorage {
9+
protected ObjectStreamStorage(File directory) {
10+
super(directory);
11+
}
12+
13+
@Override
14+
protected void doWrite(Resume r, OutputStream os) throws IOException {
15+
try (ObjectOutputStream oos = new ObjectOutputStream(os)) {
16+
oos.writeObject(r);
17+
}
18+
}
19+
20+
@Override
21+
protected Resume doRead(InputStream is) throws IOException {
22+
try (ObjectInputStream ois = new ObjectInputStream(is)) {
23+
return (Resume) ois.readObject();
24+
} catch (ClassNotFoundException e) {
25+
throw new StorageException("Error read resume", null, e);
26+
}
27+
}
28+
}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import ru.javawebinar.basejava.exception.NotExistStorageException;
77
import ru.javawebinar.basejava.model.*;
88

9+
import java.io.File;
910
import java.time.Month;
1011
import java.util.Arrays;
1112
import java.util.List;
@@ -14,6 +15,8 @@
1415
import static org.junit.Assert.assertTrue;
1516

1617
public abstract class AbstractStorageTest {
18+
protected static final File STORAGE_DIR = new File("C:\\projects\\storage");
19+
1720
protected Storage storage;
1821

1922
private static final String UUID_1 = "uuid1";
@@ -84,7 +87,7 @@ public void clear() throws Exception {
8487
public void update() throws Exception {
8588
Resume newResume = new Resume(UUID_1, "New Name");
8689
storage.update(newResume);
87-
assertTrue(newResume == storage.get(UUID_1));
90+
assertTrue(newResume.equals(storage.get(UUID_1)));
8891
}
8992

9093
@Test(expected = NotExistStorageException.class)

0 commit comments

Comments
 (0)