Skip to content

Commit 4e75634

Browse files
author
Tanechka
committed
Lesson12 HW10
1 parent a34c2ee commit 4e75634

File tree

3 files changed

+119
-19
lines changed

3 files changed

+119
-19
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public Link() {
2222
public Link(String name, String url) {
2323
Objects.requireNonNull(name, "name must not be null");
2424
this.name = name;
25-
this.url = url;
25+
this.url = url == null ? "" : url;
2626
}
2727

2828
public String getName() {

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,14 @@ public Organization(Link homePage, List<Position> positions) {
3535
this.positions = positions;
3636
}
3737

38+
public Link getHomePage() {
39+
return homePage;
40+
}
41+
42+
public List<Position> getPositions() {
43+
return positions;
44+
}
45+
3846
@Override
3947
public boolean equals(Object o) {
4048
if (this == o) return true;
@@ -54,10 +62,6 @@ public String toString() {
5462
return "Organization(" + homePage + "," + positions + ')';
5563
}
5664

57-
/**
58-
* gkislin
59-
* 28.07.2016
60-
*/
6165
@XmlAccessorType(XmlAccessType.FIELD)
6266
public static class Position implements Serializable {
6367
@XmlJavaTypeAdapter(LocalDateAdapter.class)
@@ -85,7 +89,7 @@ public Position(LocalDate startDate, LocalDate endDate, String title, String des
8589
this.startDate = startDate;
8690
this.endDate = endDate;
8791
this.title = title;
88-
this.description = description;
92+
this.description = description == null ? "" : description;
8993
}
9094

9195
public LocalDate getStartDate() {
@@ -125,4 +129,4 @@ public String toString() {
125129
return "Position(" + startDate + ',' + endDate + ',' + title + ',' + description + ')';
126130
}
127131
}
128-
}
132+
}
Lines changed: 108 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package ru.javawebinar.basejava.storage.serializer;
22

3-
import ru.javawebinar.basejava.model.ContactType;
4-
import ru.javawebinar.basejava.model.Resume;
3+
import ru.javawebinar.basejava.model.*;
54

65
import java.io.*;
6+
import java.time.LocalDate;
7+
import java.util.ArrayList;
8+
import java.util.Collection;
9+
import java.util.List;
710
import java.util.Map;
811

912
public class DataStreamSerializer implements StreamSerializer {
@@ -14,27 +17,120 @@ public void doWrite(Resume r, OutputStream os) throws IOException {
1417
dos.writeUTF(r.getUuid());
1518
dos.writeUTF(r.getFullName());
1619
Map<ContactType, String> contacts = r.getContacts();
17-
dos.writeInt(contacts.size());
18-
for (Map.Entry<ContactType, String> entry : contacts.entrySet()) {
20+
writeCollection(dos, contacts.entrySet(), entry -> {
1921
dos.writeUTF(entry.getKey().name());
2022
dos.writeUTF(entry.getValue());
21-
}
22-
// TODO implements sections
23+
});
24+
25+
writeCollection(dos, r.getSections().entrySet(), entry -> {
26+
SectionType type = entry.getKey();
27+
Section section = entry.getValue();
28+
dos.writeUTF(type.name());
29+
switch (type) {
30+
case PERSONAL:
31+
case OBJECTIVE:
32+
dos.writeUTF(((TextSection) section).getContent());
33+
break;
34+
case ACHIEVEMENT:
35+
case QUALIFICATIONS:
36+
writeCollection(dos, ((ListSection) section).getItems(), dos::writeUTF);
37+
break;
38+
case EXPERIENCE:
39+
case EDUCATION:
40+
writeCollection(dos, ((OrganizationSection) section).getOrganizations(), org -> {
41+
dos.writeUTF(org.getHomePage().getName());
42+
dos.writeUTF(org.getHomePage().getUrl());
43+
writeCollection(dos, org.getPositions(), position -> {
44+
writeLocalDate(dos, position.getStartDate());
45+
writeLocalDate(dos, position.getEndDate());
46+
dos.writeUTF(position.getTitle());
47+
dos.writeUTF(position.getDescription());
48+
});
49+
});
50+
break;
51+
}
52+
});
2353
}
2454
}
2555

56+
private void writeLocalDate(DataOutputStream dos, LocalDate ld) throws IOException {
57+
dos.writeInt(ld.getYear());
58+
dos.writeInt(ld.getMonth().getValue());
59+
}
60+
61+
private LocalDate readLocalDate(DataInputStream dis) throws IOException {
62+
return LocalDate.of(dis.readInt(), dis.readInt(), 1);
63+
}
64+
2665
@Override
2766
public Resume doRead(InputStream is) throws IOException {
2867
try (DataInputStream dis = new DataInputStream(is)) {
2968
String uuid = dis.readUTF();
3069
String fullName = dis.readUTF();
3170
Resume resume = new Resume(uuid, fullName);
32-
int size = dis.readInt();
33-
for (int i = 0; i < size; i++) {
34-
resume.addContact(ContactType.valueOf(dis.readUTF()), dis.readUTF());
35-
}
36-
// TODO implements sections
71+
readItems(dis, () -> resume.addContact(ContactType.valueOf(dis.readUTF()), dis.readUTF()));
72+
readItems(dis, () -> {
73+
SectionType sectionType = SectionType.valueOf(dis.readUTF());
74+
resume.addSection(sectionType, readSection(dis, sectionType));
75+
});
3776
return resume;
3877
}
3978
}
40-
}
79+
80+
private Section readSection(DataInputStream dis, SectionType sectionType) throws IOException {
81+
switch (sectionType) {
82+
case PERSONAL:
83+
case OBJECTIVE:
84+
return new TextSection(dis.readUTF());
85+
case ACHIEVEMENT:
86+
case QUALIFICATIONS:
87+
return new ListSection(readList(dis, dis::readUTF));
88+
case EXPERIENCE:
89+
case EDUCATION:
90+
return new OrganizationSection(
91+
readList(dis, () -> new Organization(
92+
new Link(dis.readUTF(), dis.readUTF()),
93+
readList(dis, () -> new Organization.Position(
94+
readLocalDate(dis), readLocalDate(dis), dis.readUTF(), dis.readUTF()
95+
))
96+
)));
97+
default:
98+
throw new IllegalStateException();
99+
}
100+
}
101+
102+
private <T> List<T> readList(DataInputStream dis, ElementReader<T> reader) throws IOException {
103+
int size = dis.readInt();
104+
List<T> list = new ArrayList<>(size);
105+
for (int i = 0; i < size; i++) {
106+
list.add(reader.read());
107+
}
108+
return list;
109+
}
110+
111+
private interface ElementProcessor {
112+
void process() throws IOException;
113+
}
114+
115+
private interface ElementReader<T> {
116+
T read() throws IOException;
117+
}
118+
119+
private interface ElementWriter<T> {
120+
void write(T t) throws IOException;
121+
}
122+
123+
private void readItems(DataInputStream dis, ElementProcessor processor) throws IOException {
124+
int size = dis.readInt();
125+
for (int i = 0; i < size; i++) {
126+
processor.process();
127+
}
128+
}
129+
130+
private <T> void writeCollection(DataOutputStream dos, Collection<T> collection, ElementWriter<T> writer) throws IOException {
131+
dos.writeInt(collection.size());
132+
for (T item : collection) {
133+
writer.write(item);
134+
}
135+
}
136+
}

0 commit comments

Comments
 (0)