11package com .urise .webapp .model ;
22
3+ import com .urise .webapp .util .DateUtil ;
4+
35import java .time .LocalDate ;
6+ import java .time .Month ;
47import java .util .ArrayList ;
8+ import java .util .Arrays ;
59import java .util .List ;
610import java .util .Objects ;
711
12+ import static com .urise .webapp .util .DateUtil .NOW ;
13+ import static com .urise .webapp .util .DateUtil .of ;
14+
815public class Organization {
916 private final Link homePage ;
10- private final String title ;
11- private final String description ;
12- private List < DateInterval > periods = new ArrayList <>();
13-
14- public Organization ( String name , String url , LocalDate startDate , LocalDate endDate , String title , String description ) {
15- Objects . requireNonNull ( startDate , "startDate must not null" );
16- Objects . requireNonNull ( endDate , "endDate must not null" );
17- Objects . requireNonNull ( title , "title must not null" );
18-
19- this . homePage = new Link ( name , url );
20- periods . add ( new DateInterval ( startDate , endDate ));
21- this .title = title ;
22- this .description = description ;
17+ private List < Position > positions = new ArrayList <>() ;
18+
19+ public Organization ( String name , String url , Position ... positions ) {
20+ this ( new Link ( name , url ), Arrays . asList ( positions ));
21+ }
22+
23+ public Organization ( String name , String url , List < Position > positions ) {
24+ this ( new Link ( name , url ), positions );
25+ }
26+
27+ public Organization ( Link homePage , List < Position > positions ) {
28+ this .homePage = homePage ;
29+ this .positions = positions ;
2330 }
2431
2532 @ Override
2633 public String toString () {
27- final String periodDescription = periods .stream ().map ((x ) -> x .toString ()).reduce ((x , y ) -> x + ", " + y ).toString ();
28- return "Organization{" +
29- "homePage=" + homePage +
30- ", workPeriod=" + periodDescription +
31- ", title='" + title + '\'' +
32- ", description='" + description + '\'' +
33- '}' ;
34+ return "Organization(" + homePage +positions +')' ;
3435 }
3536
3637 @ Override
@@ -41,32 +42,87 @@ public boolean equals(Object o) {
4142 Organization that = (Organization ) o ;
4243
4344 if (homePage != null ? !homePage .equals (that .homePage ) : that .homePage != null ) return false ;
44- if (!periods .equals (that .periods )) return false ;
45- if (!title .equals (that .title )) return false ;
46- return description != null ? description .equals (that .description ) : that .description == null ;
45+ return positions != null ? positions .equals (that .positions ) : that .positions == null ;
4746 }
4847
4948 @ Override
5049 public int hashCode () {
5150 int result = homePage != null ? homePage .hashCode () : 0 ;
52- result = 31 * result + periods .hashCode ();
53- result = 31 * result + title .hashCode ();
54- result = 31 * result + (description != null ? description .hashCode () : 0 );
51+ result = 31 * result + (positions != null ? positions .hashCode () : 0 );
5552 return result ;
5653 }
5754
58- private class DateInterval {
59- private LocalDate startDate ;
60- private LocalDate endDate ;
55+ public static class Position {
56+ private final LocalDate startDate ;
57+ private final LocalDate endDate ;
58+ private final String title ;
59+ private final String description ;
6160
62- private DateInterval (LocalDate startDate , LocalDate endDate ) {
61+ public Position (int startYear , Month startMonth , String title , String description ) {
62+ this (DateUtil .of (startYear ,startMonth ), NOW ,title , description );
63+ }
64+
65+ public Position (int startYear , Month startMonth , int endYear , Month endMonth , String title , String description ) {
66+ this (DateUtil .of (startYear ,startMonth ), DateUtil .of (endYear ,endMonth ),title , description );
67+ }
68+
69+ public Position (LocalDate startDate , LocalDate endDate , String title , String description ) {
70+ Objects .requireNonNull (startDate , "startDate must not be null" );
71+ Objects .requireNonNull (endDate , "endDate must not be null" );
72+ Objects .requireNonNull (title , "title must not be null" );
6373 this .startDate = startDate ;
6474 this .endDate = endDate ;
75+ this .title = title ;
76+ this .description = description ;
77+ }
78+
79+ public LocalDate getStartDate () {
80+ return startDate ;
81+ }
82+
83+ public LocalDate getEndDate () {
84+ return endDate ;
85+ }
86+
87+ public String getTitle () {
88+ return title ;
89+ }
90+
91+ public String getDescription () {
92+ return description ;
93+ }
94+
95+ @ Override
96+ public boolean equals (Object o ) {
97+ if (this == o ) return true ;
98+ if (o == null || getClass () != o .getClass ()) return false ;
99+
100+ Position position = (Position ) o ;
101+
102+ if (!startDate .equals (position .startDate )) return false ;
103+ if (!endDate .equals (position .endDate )) return false ;
104+ if (!title .equals (position .title )) return false ;
105+ return description != null ? description .equals (position .description ) : position .description == null ;
106+ }
107+
108+ @ Override
109+ public int hashCode () {
110+ int result = startDate .hashCode ();
111+ result = 31 * result + endDate .hashCode ();
112+ result = 31 * result + title .hashCode ();
113+ result = 31 * result + (description != null ? description .hashCode () : 0 );
114+ return result ;
65115 }
66116
67117 @ Override
68118 public String toString () {
69- return startDate + " - " + endDate ;
119+ return "Position{" +
120+ "startDate=" + startDate +
121+ ", endDate=" + endDate +
122+ ", title='" + title + '\'' +
123+ ", description='" + description + '\'' +
124+ '}' ;
70125 }
71126 }
127+
72128}
0 commit comments