1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 package com.rapiddweller.domain.address;
28
29 import com.rapiddweller.benerator.primitive.RandomVarLengthStringGenerator;
30 import com.rapiddweller.common.ArrayUtil;
31 import com.rapiddweller.common.Escalator;
32 import com.rapiddweller.common.LoggerEscalator;
33 import com.rapiddweller.common.NullSafeComparator;
34 import com.rapiddweller.common.StringUtil;
35
36 import java.util.Locale;
37
38
39
40
41
42
43
44
45 public class City {
46
47 private static final Escalator escalator = new LoggerEscalator();
48 private static final RandomVarLengthStringGenerator localNumberGenerator;
49
50 static {
51 localNumberGenerator =
52 new RandomVarLengthStringGenerator("\\d", 7, 8, 1);
53 localNumberGenerator.init(null);
54 }
55
56 private final String name;
57 private String nameExtension;
58 private String[] postalCodes;
59 private String areaCode;
60 private State state;
61 private Locale language;
62 private int population;
63
64
65
66
67
68
69
70
71
72
73 public City(State state, String name, String addition, String[] postalCodes,
74 String areaCode) {
75 if (areaCode == null) {
76 throw new IllegalArgumentException("Area Code is null for " + name);
77 }
78 this.state = state;
79 this.name = name;
80 this.nameExtension = addition;
81 this.postalCodes = (postalCodes != null ? postalCodes : new String[0]);
82 this.areaCode = areaCode;
83 }
84
85
86
87
88
89
90 public String getNameExtension() {
91 return nameExtension;
92 }
93
94
95
96
97
98
99 public void setNameExtension(String nameExtension) {
100 this.nameExtension = nameExtension;
101 }
102
103
104
105
106
107
108 public String[] getPostalCodes() {
109 return postalCodes;
110 }
111
112
113
114
115
116
117 public void setPostalCodes(String[] postalCodes) {
118 this.postalCodes = postalCodes;
119 }
120
121
122
123
124
125
126 public void addPostalCode(String postalCode) {
127 postalCodes = ArrayUtil.append(postalCode, postalCodes);
128 }
129
130
131
132
133
134
135
136 @Deprecated
137 public String[] getZipCodes() {
138 escalator.escalate(
139 "property City.zipCode is deprecated, use City.postalCode instead",
140 City.class, "Invoked getZipCodes()");
141 return getPostalCodes();
142 }
143
144
145
146
147
148
149
150 @Deprecated
151 public void setZipCodes(String[] zipCodes) {
152 escalator.escalate(
153 "property City.zipCode is deprecated, use City.postalCode instead",
154 City.class, "Invoked setZipCodes()");
155 this.postalCodes = zipCodes;
156 }
157
158
159
160
161
162
163
164 @Deprecated
165 public void addZipCode(String zipCode) {
166 escalator.escalate(
167 "property City.zipCode is deprecated, use City.postalCode instead",
168 City.class, "Invoked addZipCode()");
169 postalCodes = ArrayUtil.append(zipCode, postalCodes);
170 }
171
172
173
174
175
176
177 public String getAreaCode() {
178 return areaCode;
179 }
180
181
182
183
184
185
186 public void setAreaCode(String phoneCode) {
187 this.areaCode = phoneCode;
188 }
189
190
191
192
193
194
195 public State getState() {
196 return state;
197 }
198
199
200
201
202
203
204 public void setState(State state) {
205 this.state = state;
206 }
207
208
209
210
211
212
213 public Country getCountry() {
214 return (state != null ? state.getCountry() : null);
215 }
216
217
218
219
220
221
222 public String getName() {
223 return name;
224 }
225
226
227
228
229
230
231 public Locale getLanguage() {
232 if (language != null) {
233 return language;
234 }
235 if (state != null) {
236 return state.getDefaultLanguageLocale();
237 }
238 Country country = getCountry();
239 return (country != null ? country.getDefaultLanguageLocale() : null);
240 }
241
242
243
244
245
246
247 public void setLanguage(Locale language) {
248 this.language = language;
249 }
250
251
252
253
254
255
256 public int getPopulation() {
257 return population;
258 }
259
260
261
262
263
264
265 public void setPopulation(int population) {
266 this.population = population;
267 }
268
269
270
271
272
273
274 public PhoneNumber generateMobileNumber() {
275 return getCountry().generateMobileNumber(this);
276 }
277
278
279
280
281
282
283 public PhoneNumber generateLandlineNumber() {
284 return new PhoneNumber(getCountry().getPhoneCode(), areaCode,
285 localNumberGenerator.generate());
286 }
287
288
289
290 @Override
291 public String toString() {
292 return name + (StringUtil.isEmpty(nameExtension) ? "" :
293 (Character.isLetter(nameExtension.charAt(0)) ? " " : "") +
294 nameExtension);
295 }
296
297 @Override
298 public boolean equals(Object o) {
299 if (this == o) {
300 return true;
301 }
302 if (o == null || getClass() != o.getClass()) {
303 return false;
304 }
305 final Cityef="../../../../com/rapiddweller/domain/address/City.html#City">City that = (City) o;
306 if (!this.name.equals(that.name)) {
307 return false;
308 }
309 if (!NullSafeComparator
310 .equals(this.nameExtension, that.nameExtension)) {
311 return false;
312 }
313 return NullSafeComparator.equals(this.state, that.state);
314 }
315
316 @Override
317 public int hashCode() {
318 int result;
319 result = name.hashCode();
320 result = 29 * result + NullSafeComparator.hashCode(nameExtension);
321 result = 29 * result + NullSafeComparator.hashCode(state);
322 return result;
323 }
324
325 }