Skip to content

Commit 62aba3d

Browse files
committed
lesson 10 ДЗ
1 parent 383f86d commit 62aba3d

File tree

13 files changed

+270
-6
lines changed

13 files changed

+270
-6
lines changed

src/com/urise/webapp/model/Link.java

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

2424
public String getName() {

src/com/urise/webapp/model/Organization.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,15 @@ public int hashCode() {
5656
result = 31 * result + (positions != null ? positions.hashCode() : 0);
5757
return result;
5858
}
59+
60+
public Link getHomePage() {
61+
return homePage;
62+
}
63+
64+
public List<Position> getPositions() {
65+
return positions;
66+
}
67+
5968
@XmlAccessorType(XmlAccessType.FIELD)
6069
public static class Position implements Serializable {
6170
@XmlJavaTypeAdapter(LocalDateAdapter.class)
@@ -82,7 +91,7 @@ public Position(LocalDate startDate, LocalDate endDate, String title, String des
8291
this.startDate = startDate;
8392
this.endDate = endDate;
8493
this.title = title;
85-
this.description = description;
94+
this.description = description==null?"":description;
8695
}
8796

8897
public void setStartDate(LocalDate startDate) {
@@ -148,6 +157,7 @@ public String toString() {
148157
", description='" + description + '\'' +
149158
'}';
150159
}
160+
151161
}
152162

153163
}

src/com/urise/webapp/model/Resume.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,14 @@ public String getFullName() {
6363
return fullName;
6464
}
6565

66+
public Map<ContactType, String> getContacts() {
67+
return contacts;
68+
}
69+
70+
public Map<SectionType, AbstractSection> getSections() {
71+
return sections;
72+
}
73+
6674
@Override
6775
public String toString() {
6876
return uuid + "(" + fullName + ")";

src/com/urise/webapp/model/SectionType.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ public enum SectionType {
55
OBJECTIVE("Позиция"),
66
ACHIEVEMENT("Достижения"),
77
QUALIFICATIONS("Квалификация"),
8-
EXPIRIENCE("Опыт работы"),
8+
EXPERIENCE("Опыт работы"),
99
EDUCATION("Образование");
1010

1111
private String title;
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
package com.urise.webapp.storage.serializer;
2+
3+
import com.urise.webapp.model.*;
4+
5+
import java.io.*;
6+
import java.time.LocalDate;
7+
import java.util.ArrayList;
8+
import java.util.Collection;
9+
import java.util.List;
10+
import java.util.Map;
11+
12+
public class DataStreamSerializer implements StreamSerializer {
13+
14+
@Override
15+
public void doWrite(Resume r, OutputStream os) throws IOException {
16+
try (DataOutputStream dos = new DataOutputStream(os)) {
17+
dos.writeUTF(r.getUuid());
18+
dos.writeUTF(r.getFullName());
19+
Map<ContactType, String> contacts = r.getContacts();
20+
writeCollection(dos, contacts.entrySet(), entry -> {
21+
dos.writeUTF(entry.getKey().name());
22+
dos.writeUTF(entry.getValue());
23+
});
24+
25+
writeCollection(dos, r.getSections().entrySet(), entry -> {
26+
SectionType type = entry.getKey();
27+
AbstractSection 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+
});
53+
}
54+
}
55+
56+
private void writeLocalDate(DataOutputStream dos, LocalDate ld) throws IOException {
57+
dos.writeInt(ld.getYear());
58+
dos.writeInt(ld.getMonth().getValue());
59+
dos.writeInt(ld.getDayOfMonth());
60+
}
61+
62+
private LocalDate readLocalDate(DataInputStream dis) throws IOException {
63+
return LocalDate.of(dis.readInt(), dis.readInt(), dis.readInt());
64+
}
65+
66+
@Override
67+
public Resume doRead(InputStream is) throws IOException {
68+
try (DataInputStream dis = new DataInputStream(is)) {
69+
String uuid = dis.readUTF();
70+
String fullName = dis.readUTF();
71+
Resume resume = new Resume(uuid, fullName);
72+
readItems(dis, () -> resume.addContact(ContactType.valueOf(dis.readUTF()), dis.readUTF()));
73+
readItems(dis, () -> {
74+
SectionType sectionType = SectionType.valueOf(dis.readUTF());
75+
resume.addSection(sectionType, readSection(dis, sectionType));
76+
});
77+
return resume;
78+
}
79+
}
80+
81+
private AbstractSection readSection(DataInputStream dis, SectionType sectionType) throws IOException {
82+
switch (sectionType) {
83+
case PERSONAL:
84+
case OBJECTIVE:
85+
return new TextSection(dis.readUTF());
86+
case ACHIEVEMENT:
87+
case QUALIFICATIONS:
88+
return new ListSection(readList(dis, dis::readUTF));
89+
case EXPERIENCE:
90+
case EDUCATION:
91+
return new OrganizationSection(
92+
readList(dis, () -> new Organization(
93+
new Link(dis.readUTF(), dis.readUTF()),
94+
readList(dis, () -> new Organization.Position(
95+
readLocalDate(dis), readLocalDate(dis), dis.readUTF(), dis.readUTF()
96+
))
97+
)));
98+
default:
99+
throw new IllegalStateException();
100+
}
101+
}
102+
103+
private <T> List<T> readList(DataInputStream dis, ElementReader<T> reader) throws IOException {
104+
int size = dis.readInt();
105+
List<T> list = new ArrayList<>(size);
106+
for (int i = 0; i < size; i++) {
107+
list.add(reader.read());
108+
}
109+
return list;
110+
}
111+
112+
private interface ElementProcessor {
113+
void process() throws IOException;
114+
}
115+
116+
private interface ElementReader<T> {
117+
T read() throws IOException;
118+
}
119+
120+
private interface ElementWriter<T> {
121+
void write(T t) throws IOException;
122+
}
123+
124+
private void readItems(DataInputStream dis, ElementProcessor processor) throws IOException {
125+
int size = dis.readInt();
126+
for (int i = 0; i < size; i++) {
127+
processor.process();
128+
}
129+
}
130+
131+
private <T> void writeCollection(DataOutputStream dos, Collection<T> collection, ElementWriter<T> writer) throws IOException {
132+
dos.writeInt(collection.size());
133+
for (T item : collection) {
134+
writer.write(item);
135+
}
136+
}
137+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.urise.webapp.storage.serializer;
2+
3+
import com.urise.webapp.model.*;
4+
import com.urise.webapp.util.JsonParser;
5+
import com.urise.webapp.util.XmlParser;
6+
7+
import java.io.*;
8+
import java.nio.charset.StandardCharsets;
9+
10+
public class JsonStreamSerializer implements StreamSerializer {
11+
12+
13+
@Override
14+
public void doWrite(Resume r, OutputStream os) throws IOException {
15+
try (Writer w = new OutputStreamWriter(os, StandardCharsets.UTF_8)) {
16+
JsonParser.write(r,w);
17+
}
18+
}
19+
20+
@Override
21+
public Resume doRead(InputStream is) throws IOException {
22+
try (Reader r = new InputStreamReader(is, StandardCharsets.UTF_8)) {
23+
return JsonParser.read(r, Resume.class);
24+
}
25+
26+
}
27+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.urise.webapp.util;
2+
3+
import com.google.gson.Gson;
4+
import com.google.gson.GsonBuilder;
5+
import com.urise.webapp.model.AbstractSection;
6+
7+
import java.io.Reader;
8+
import java.io.Writer;
9+
10+
public class JsonParser {
11+
private static Gson GSON = new GsonBuilder()
12+
.registerTypeAdapter(AbstractSection.class, new JsonSectionAdapter())
13+
.create();
14+
15+
public static <T> T read(Reader reader, Class<T> clazz) {
16+
return GSON.fromJson(reader, clazz);
17+
}
18+
19+
public static <T> void write(T object, Writer writer) {
20+
GSON.toJson(object, writer);
21+
}
22+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.urise.webapp.util;
2+
3+
import com.google.gson.*;
4+
5+
import java.lang.reflect.Type;
6+
7+
public class JsonSectionAdapter<T> implements JsonSerializer<T>, JsonDeserializer<T> {
8+
private static final String CLASSNAME = "CLASSNAME";
9+
private static final String INSTANCE = "INSTANCE";
10+
11+
@Override
12+
public T deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext context) throws JsonParseException {
13+
JsonObject jsonObject = jsonElement.getAsJsonObject();
14+
//считываем свойство CLASSNAME - имя класса
15+
JsonPrimitive prim = (JsonPrimitive) jsonObject.get(CLASSNAME);
16+
String className = prim.getAsString();
17+
try{
18+
Class clazz = Class.forName(className);
19+
return context.deserialize(jsonObject.get(INSTANCE),clazz);
20+
} catch (ClassNotFoundException e) {
21+
throw new JsonParseException(e.getMessage());
22+
}
23+
}
24+
25+
@Override
26+
public JsonElement serialize(T section, Type type, JsonSerializationContext context) {
27+
JsonObject retValue =new JsonObject();
28+
//добавляем свойство classname
29+
retValue.addProperty(CLASSNAME, section.getClass().getName());
30+
//сериализуем инстанс
31+
JsonElement elem =context.serialize(section);
32+
//добавляем инстанс
33+
retValue.add(INSTANCE,elem);
34+
return retValue;
35+
}
36+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ public void saveNotExist() {
6666

6767
@Test
6868
public void getExist() {
69+
R_1.equals(storage.get(UUID_1));
6970
assertEquals(R_1, storage.get(UUID_1));
7071
assertEquals(R_2, storage.get(UUID_2));
7172
assertEquals(R_3, storage.get(UUID_3));

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
MapResumeStorageTest.class,
1313
ObjectFileStorageTest.class,
1414
ObjectPathStorageTest.class,
15-
XmlPathStorageTest.class
15+
XmlPathStorageTest.class,
16+
JsonPathStorageTest.class,
17+
DataPathStorageTest.class
1618
})
1719
public class AllStorageTest {
1820
}

0 commit comments

Comments
 (0)