1
2
3
4 package de.powerstat.validation.values;
5
6
7 import java.util.Objects;
8 import java.util.regex.Pattern;
9
10 import de.powerstat.validation.interfaces.IValueObject;
11
12
13
14
15
16
17
18 public final class City implements Comparable<City>, IValueObject
19 {
20
21
22
23
24
25
26
27
28 private static final Pattern CITY_REGEXP = Pattern.compile("^[\\p{L}][\\p{L} -]*$");
29
30
31
32
33 private final String city;
34
35
36
37
38
39
40
41
42
43 private City(final String city)
44 {
45 super();
46 Objects.requireNonNull(city, "city");
47 if ((city.length() < 1) || (city.length() > 85))
48 {
49 throw new IllegalArgumentException("City with wrong length");
50 }
51 if (!City.CITY_REGEXP.matcher(city).matches())
52 {
53 throw new IllegalArgumentException("City with wrong format");
54 }
55 this.city = city;
56 }
57
58
59
60
61
62
63
64
65 public static City of(final String city)
66 {
67
68
69
70
71
72
73
74
75
76
77
78
79
80 return new City(city);
81 }
82
83
84
85
86
87
88
89 @Override
90 public String stringValue()
91 {
92 return this.city;
93 }
94
95
96
97
98
99
100
101
102 @Override
103 public int hashCode()
104 {
105 return this.city.hashCode();
106 }
107
108
109
110
111
112
113
114
115
116 @Override
117 public boolean equals(final Object obj)
118 {
119 if (this == obj)
120 {
121 return true;
122 }
123 if (!(obj instanceof City))
124 {
125 return false;
126 }
127 final City other = (City)obj;
128 return this.city.equals(other.city);
129 }
130
131
132
133
134
135
136
137
138
139
140
141
142 @Override
143 public String toString()
144 {
145 final var builder = new StringBuilder();
146 builder.append("City[city=").append(this.city).append(']');
147 return builder.toString();
148 }
149
150
151
152
153
154
155
156
157
158 @Override
159 public int compareTo(final City obj)
160 {
161 Objects.requireNonNull(obj, "obj");
162 return this.city.compareTo(obj.city);
163 }
164
165 }