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+ }
0 commit comments