11package 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
65import java .io .*;
6+ import java .time .LocalDate ;
7+ import java .util .ArrayList ;
8+ import java .util .Collection ;
9+ import java .util .List ;
710import java .util .Map ;
811
912public 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