View Javadoc

1   /*
2    * Copyright 2006-2011 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.kuali.rice.shareddata.api.county;
18  
19  import java.io.Serializable;
20  import java.util.Collection;
21  
22  import javax.xml.bind.annotation.XmlAccessType;
23  import javax.xml.bind.annotation.XmlAccessorType;
24  import javax.xml.bind.annotation.XmlAnyElement;
25  import javax.xml.bind.annotation.XmlElement;
26  import javax.xml.bind.annotation.XmlRootElement;
27  import javax.xml.bind.annotation.XmlType;
28  
29  import org.apache.commons.lang.StringUtils;
30  import org.apache.commons.lang.builder.EqualsBuilder;
31  import org.apache.commons.lang.builder.HashCodeBuilder;
32  import org.apache.commons.lang.builder.ToStringBuilder;
33  import org.kuali.rice.core.api.CoreConstants;
34  import org.kuali.rice.core.api.mo.ModelBuilder;
35  import org.kuali.rice.core.api.mo.ModelObjectComplete;
36  import org.w3c.dom.Element;
37  
38  /**
39   * An immutable representation of a {@link CountyContract}.
40   *
41   * <p>To construct an instance of a County, use the {@link County.Builder} class.
42   *
43   * @see CountyContract
44   */
45  @XmlRootElement(name = County.Constants.ROOT_ELEMENT_NAME)
46  @XmlAccessorType(XmlAccessType.NONE)
47  @XmlType(name = County.Constants.TYPE_NAME, propOrder = {
48          County.Elements.CODE,
49          County.Elements.NAME,
50          County.Elements.COUNTRY_CODE,
51          County.Elements.STATE_CODE,
52          County.Elements.ACTIVE,
53          CoreConstants.CommonElements.VERSION_NUMBER,
54          CoreConstants.CommonElements.FUTURE_ELEMENTS
55  })
56  public final class County implements CountyContract, ModelObjectComplete {
57  
58      private static final long serialVersionUID = 6097498602725305353L;
59  
60      @XmlElement(name = Elements.CODE, required = true)
61      private final String code;
62  
63      @XmlElement(name = Elements.NAME, required = true)
64      private final String name;
65  
66      @XmlElement(name = Elements.COUNTRY_CODE, required = true)
67      private final String countryCode;
68  
69      @XmlElement(name = Elements.STATE_CODE, required = true)
70      private final String stateCode;
71  
72      @XmlElement(name = Elements.ACTIVE, required = true)
73      private final boolean active;
74  
75      @XmlElement(name = CoreConstants.CommonElements.VERSION_NUMBER, required = false)
76      private final Long versionNumber;
77  
78      @SuppressWarnings("unused")
79      @XmlAnyElement
80      private final Collection<Element> _futureElements = null;
81  
82      /**
83       * This constructor should never be called except during JAXB unmarshalling.
84       */
85      @SuppressWarnings("unused")
86      private County() {
87          this.code = null;
88          this.name = null;
89          this.countryCode = null;
90          this.stateCode = null;
91          this.active = false;
92          this.versionNumber = null;
93      }
94  
95      private County(Builder builder) {
96          code = builder.getCode();
97          name = builder.getName();
98          countryCode = builder.getCountryCode();
99          stateCode = builder.getStateCode();
100         active = builder.isActive();
101         versionNumber = builder.getVersionNumber();
102     }
103 
104     @Override
105     public String getCode() {
106         return code;
107     }
108 
109     @Override
110     public String getName() {
111         return name;
112     }
113 
114     @Override
115     public String getCountryCode() {
116         return countryCode;
117     }
118 
119     @Override
120     public String getStateCode() {
121         return stateCode;
122     }
123 
124     @Override
125     public boolean isActive() {
126         return active;
127     }
128 
129     @Override
130     public Long getVersionNumber() {
131         return versionNumber;
132     }
133 
134     /**
135      * This builder constructs an County enforcing the constraints of the {@link CountyContract}.
136      */
137     public static class Builder implements CountyContract, ModelBuilder, Serializable {
138 
139         private static final long serialVersionUID = 7077484401017765844L;
140 
141         private String code;
142         private String name;
143         private String countryCode;
144         private String stateCode;
145         private boolean active;
146         private Long versionNumber;
147 
148         private Builder(String code, String name, String countryCode, String stateCode) {
149             setCode(code);
150             setName(name);
151             setCountryCode(countryCode);
152             setStateCode(stateCode);
153             setVersionNumber(versionNumber);
154         }
155 
156         /**
157          * creates a County Builder with the required fields.
158          */
159         public static Builder create(String code, String name, String countryCode, String stateCode) {
160             final Builder builder = new Builder(code, name, countryCode, stateCode);
161             builder.setActive(true);
162             return builder;
163         }
164 
165         /**
166          * creates a County Builder from an existing {@link CountyContract}.
167          */
168         public static Builder create(CountyContract contract) {
169             final Builder builder = new Builder(contract.getCode(), contract.getName(), contract.getCountryCode(), contract.getStateCode());
170             builder.setActive(contract.isActive());
171             builder.setVersionNumber(contract.getVersionNumber());
172             return builder;
173         }
174 
175         @Override
176         public String getCode() {
177             return code;
178         }
179 
180         /**
181          * Sets the code to be used for the County created from this Builder.
182          * @param code String code for a County
183          * @throws IllegalArgumentException if the passed in code is null or a blank String.
184          */
185         public void setCode(String code) {
186             if (StringUtils.isBlank(code)) {
187                 throw new IllegalArgumentException("code is blank");
188             }
189 
190             this.code = code;
191         }
192 
193         @Override
194         public String getName() {
195             return name;
196         }
197 
198         /**
199          * Sets the full name of the County created from this Builder.
200          * @param name String representing the full name for the County
201          * @throws IllegalArgumentException if the passed in name is null or a blank String.
202          */
203         public void setName(String name) {
204             if (StringUtils.isBlank(name)) {
205                 throw new IllegalArgumentException("name is blank");
206             }
207 
208             this.name = name;
209         }
210 
211         @Override
212         public String getCountryCode() {
213             return countryCode;
214         }
215 
216         /**
217          * Sets the Country code to be associated with the County created from this Builder.
218          * @param countryCode String representing the Country Code
219          * @throws IllegalArgumentException if the passed in countryCode is null or a blank String.
220          * @see org.kuali.rice.shareddata.api.country.CountryContract
221          */
222         public void setCountryCode(String countryCode) {
223             if (StringUtils.isBlank(countryCode)) {
224                 throw new IllegalArgumentException("countryCode is blank");
225             }
226 
227             this.countryCode = countryCode;
228         }
229 
230         @Override
231         public String getStateCode() {
232             return stateCode;
233         }
234 
235         /**
236          * Sets the State code to be associated with the County created from this Builder.
237          * @param stateCode String representing the State code
238          * @throws  IllegalArgumentException if the passed in statecode is null or a blank String.
239          * @see org.kuali.rice.shareddata.api.state.StateContract
240          */
241         public void setStateCode(String stateCode) {
242             if (StringUtils.isBlank(stateCode)) {
243                 throw new IllegalArgumentException("stateCode is blank");
244             }
245 
246             this.stateCode = stateCode;
247         }
248 
249         @Override
250         public boolean isActive() {
251             return active;
252         }
253 
254         /**
255          * Sets the active flag for the County created from this Builder.
256          * @param active
257          */
258         public void setActive(boolean active) {
259             this.active = active;
260         }
261 
262         @Override
263         public Long getVersionNumber() {
264             return versionNumber;
265         }
266 
267         public void setVersionNumber(Long versionNumber) {
268             this.versionNumber = versionNumber;
269         }
270 
271         @Override
272         public County build() {
273             return new County(this);
274         }
275     }
276 
277     @Override
278     public int hashCode() {
279         return HashCodeBuilder.reflectionHashCode(this, Constants.HASH_CODE_EQUALS_EXCLUDE);
280     }
281 
282     @Override
283     public boolean equals(Object obj) {
284         return EqualsBuilder.reflectionEquals(obj, this, Constants.HASH_CODE_EQUALS_EXCLUDE);
285     }
286 
287     @Override
288     public String toString() {
289         return ToStringBuilder.reflectionToString(this);
290     }
291 
292     /**
293      * Defines some internal constants used on this class.
294      */
295     static class Constants {
296         final static String ROOT_ELEMENT_NAME = "county";
297         final static String TYPE_NAME = "CountyType";
298         final static String[] HASH_CODE_EQUALS_EXCLUDE = {CoreConstants.CommonElements.FUTURE_ELEMENTS};
299     }
300 
301     /**
302      * A private class which exposes constants which define the XML element names to use
303      * when this object is marshalled to XML.
304      */
305     static class Elements {
306         final static String CODE = "code";
307         final static String NAME = "name";
308         final static String COUNTRY_CODE = "countryCode";
309         final static String STATE_CODE = "stateCode";
310         final static String ACTIVE = "active";
311     }
312 }