View Javadoc
1   /*
2    * (c) Copyright 2006-2020 by rapiddweller GmbH & Volker Bergmann. All rights reserved.
3    *
4    * Redistribution and use in source and binary forms, with or without
5    * modification, is permitted under the terms of the
6    * GNU General Public License.
7    *
8    * For redistributing this software or a derivative work under a license other
9    * than the GPL-compatible Free Software License as defined by the Free
10   * Software Foundation or approved by OSI, you must first obtain a commercial
11   * license to this software product from rapiddweller GmbH & Volker Bergmann.
12   *
13   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
14   * WITHOUT A WARRANTY OF ANY KIND. ALL EXPRESS OR IMPLIED CONDITIONS,
15   * REPRESENTATIONS AND WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF
16   * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE
17   * HEREBY EXCLUDED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
18   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24   * POSSIBILITY OF SUCH DAMAGE.
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   * Represents a city.<br/><br/>
40   * Created: 11.06.2006 08:19:23
41   *
42   * @author Volker Bergmann
43   * @since 0.1
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     * Instantiates a new City.
66     *
67     * @param state       the state
68     * @param name        the name
69     * @param addition    the addition
70     * @param postalCodes the postal codes
71     * @param areaCode    the area code
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     * Gets name extension.
87     *
88     * @return the name extension
89     */
90    public String getNameExtension() {
91      return nameExtension;
92    }
93  
94    /**
95     * Sets name extension.
96     *
97     * @param nameExtension the name extension
98     */
99    public void setNameExtension(String nameExtension) {
100     this.nameExtension = nameExtension;
101   }
102 
103   /**
104    * Get postal codes string [ ].
105    *
106    * @return the string [ ]
107    */
108   public String[] getPostalCodes() {
109     return postalCodes;
110   }
111 
112   /**
113    * Sets postal codes.
114    *
115    * @param postalCodes the postal codes
116    */
117   public void setPostalCodes(String[] postalCodes) {
118     this.postalCodes = postalCodes;
119   }
120 
121   /**
122    * Add postal code.
123    *
124    * @param postalCode the postal code
125    */
126   public void addPostalCode(String postalCode) {
127     postalCodes = ArrayUtil.append(postalCode, postalCodes);
128   }
129 
130   /**
131    * Get zip codes string [ ].
132    *
133    * @return the string [ ]
134    * @deprecated use property postalCodes
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    * Sets zip codes.
146    *
147    * @param zipCodes the zip codes
148    * @deprecated use property postalCodes
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    * Add zip code.
160    *
161    * @param zipCode the zip code
162    * @deprecated use property postalCodes
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    * Gets area code.
174    *
175    * @return the area code
176    */
177   public String getAreaCode() {
178     return areaCode;
179   }
180 
181   /**
182    * Sets area code.
183    *
184    * @param phoneCode the phone code
185    */
186   public void setAreaCode(String phoneCode) {
187     this.areaCode = phoneCode;
188   }
189 
190   /**
191    * Gets state.
192    *
193    * @return the state
194    */
195   public State getState() {
196     return state;
197   }
198 
199   /**
200    * Sets state.
201    *
202    * @param state the state
203    */
204   public void setState(State state) {
205     this.state = state;
206   }
207 
208   /**
209    * Gets country.
210    *
211    * @return the country
212    */
213   public Country getCountry() {
214     return (state != null ? state.getCountry() : null);
215   }
216 
217   /**
218    * Gets name.
219    *
220    * @return the name
221    */
222   public String getName() {
223     return name;
224   }
225 
226   /**
227    * Gets language.
228    *
229    * @return the language
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    * Sets language.
244    *
245    * @param language the language
246    */
247   public void setLanguage(Locale language) {
248     this.language = language;
249   }
250 
251   /**
252    * Gets population.
253    *
254    * @return the population
255    */
256   public int getPopulation() {
257     return population;
258   }
259 
260   /**
261    * Sets population.
262    *
263    * @param population the population
264    */
265   public void setPopulation(int population) {
266     this.population = population;
267   }
268 
269   /**
270    * Generate mobile number phone number.
271    *
272    * @return the phone number
273    */
274   public PhoneNumber generateMobileNumber() {
275     return getCountry().generateMobileNumber(this);
276   }
277 
278   /**
279    * Generate landline number phone number.
280    *
281    * @return the phone number
282    */
283   public PhoneNumber generateLandlineNumber() {
284     return new PhoneNumber(getCountry().getPhoneCode(), areaCode,
285         localNumberGenerator.generate());
286   }
287 
288   // java.lang.Object overrides --------------------------------------------------------------------------------------
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 }