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.common.NullSafeComparator;
30  import com.rapiddweller.common.OrderedMap;
31  
32  import java.util.Collection;
33  import java.util.Locale;
34  import java.util.Map;
35  
36  /**
37   * Represents a state.<br/>
38   * <br/>
39   * Created: 27.07.2007 17:37:12
40   *
41   * @author Volker Bergmann
42   * @since 0.3
43   */
44  public class State {
45  
46    private String id;
47    private String name;
48    private Country country;
49    private final Map<CityId, City> cities;
50    private int population;
51    private Locale defaultLanguageLocale;
52  
53    // constructors ----------------------------------------------------------------------------------------------------
54  
55    /**
56     * Instantiates a new State.
57     */
58    public State() {
59      this(null);
60    }
61  
62    /**
63     * Instantiates a new State.
64     *
65     * @param id the id
66     */
67    public State(String id) {
68      this.id = id;
69      this.cities = new OrderedMap<>();
70    }
71  
72    // properties ------------------------------------------------------------------------------------------------------
73  
74    /**
75     * Gets id.
76     *
77     * @return the id
78     */
79    public String getId() {
80      return id;
81    }
82  
83    /**
84     * Sets id.
85     *
86     * @param id the id
87     */
88    public void setId(String id) {
89      this.id = id;
90    }
91  
92    /**
93     * Gets name.
94     *
95     * @return the name
96     */
97    public String getName() {
98      return name;
99    }
100 
101   /**
102    * Sets name.
103    *
104    * @param name the name
105    */
106   public void setName(String name) {
107     this.name = name;
108   }
109 
110   /**
111    * Gets default language.
112    *
113    * @return the default language
114    */
115   public String getDefaultLanguage() {
116     return getDefaultLanguageLocale().getLanguage();
117   }
118 
119   /**
120    * Sets default language.
121    *
122    * @param defaultLanguage the default language
123    */
124   public void setDefaultLanguage(String defaultLanguage) {
125     setDefaultLanguageLocale(new Locale(defaultLanguage));
126   }
127 
128   /**
129    * Gets default language locale.
130    *
131    * @return the default language locale
132    */
133   public Locale getDefaultLanguageLocale() {
134     if (defaultLanguageLocale != null) {
135       return defaultLanguageLocale;
136     } else {
137       return country.getDefaultLanguageLocale();
138     }
139   }
140 
141   /**
142    * Sets default language locale.
143    *
144    * @param defaultLanguage the default language
145    */
146   public void setDefaultLanguageLocale(Locale defaultLanguage) {
147     this.defaultLanguageLocale = defaultLanguage;
148   }
149 
150   /**
151    * Gets population.
152    *
153    * @return the population
154    */
155   public int getPopulation() {
156     return population;
157   }
158 
159   /**
160    * Sets population.
161    *
162    * @param population the population
163    */
164   public void setPopulation(int population) {
165     this.population = population;
166   }
167 
168   /**
169    * Gets country.
170    *
171    * @return the country
172    */
173   public Country getCountry() {
174     return country;
175   }
176 
177   /**
178    * Sets country.
179    *
180    * @param country the country
181    */
182   public void setCountry(Country country) {
183     this.country = country;
184   }
185 
186   // city handling ---------------------------------------------------------------------------------------------------
187 
188   /**
189    * Gets city.
190    *
191    * @param id the id
192    * @return the city
193    */
194   public City getCity(CityId id) {
195     country.checkCities();
196     return cities.get(id);
197   }
198 
199   /**
200    * Gets cities.
201    *
202    * @return the cities
203    */
204   public Collection<City> getCities() {
205     country.checkCities();
206     return cities.values();
207   }
208 
209   /**
210    * Add city.
211    *
212    * @param id   the id
213    * @param city the city
214    */
215   public void addCity(CityId id, City city) {
216     city.setState(this);
217     cities.put(id, city);
218   }
219 
220   // java.lang.Object overrides --------------------------------------------------------------------------------------
221 
222   @Override
223   public String toString() {
224     return (name != null ? name : id);
225   }
226 
227   @Override
228   public int hashCode() {
229     return NullSafeComparator.hashCode(country) * 31 + name.hashCode();
230   }
231 
232   @Override
233   public boolean equals(Object obj) {
234     if (this == obj) {
235       return true;
236     }
237     if (obj == null) {
238       return false;
239     }
240     if (getClass() != obj.getClass()) {
241       return false;
242     }
243     final Statef="../../../../com/rapiddweller/domain/address/State.html#State">State that = (State) obj;
244     if (!NullSafeComparator.equals(this.country, that.country)) {
245       return false;
246     }
247     return name.equals(that.name);
248   }
249 
250 }