001 /*
002 * Copyright 2011-2012 UnboundID Corp.
003 *
004 * This program is free software; you can redistribute it and/or modify
005 * it under the terms of the GNU General Public License (GPLv2 only)
006 * or the terms of the GNU Lesser General Public License (LGPLv2.1 only)
007 * as published by the Free Software Foundation.
008 *
009 * This program is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012 * GNU General Public License for more details.
013 *
014 * You should have received a copy of the GNU General Public License
015 * along with this program; if not, see <http://www.gnu.org/licenses>.
016 */
017
018 package com.unboundid.scim.data;
019
020 import com.unboundid.scim.schema.AttributeDescriptor;
021 import com.unboundid.scim.sdk.InvalidResourceException;
022 import com.unboundid.scim.sdk.SCIMAttribute;
023 import com.unboundid.scim.sdk.SCIMAttributeValue;
024
025 import java.util.ArrayList;
026 import java.util.List;
027
028 /**
029 * This class represents the address complex attribute in user resources.
030 */
031 public class Address
032 {
033 /**
034 * The <code>AttributeValueResolver</code> that resolves SCIM attribute values
035 * to/from <code>Address</code> instances.
036 */
037 public static final AttributeValueResolver<Address> ADDRESS_RESOLVER =
038 new AttributeValueResolver<Address>()
039 {
040 public Address toInstance(final SCIMAttributeValue value) {
041 Boolean p = value.getSubAttributeValue("primary",
042 BOOLEAN_RESOLVER);
043 return new Address(
044 value.getSubAttributeValue("formatted",
045 STRING_RESOLVER),
046 value.getSubAttributeValue("streetAddress",
047 STRING_RESOLVER),
048 value.getSubAttributeValue("locality",
049 STRING_RESOLVER),
050 value.getSubAttributeValue("region",
051 STRING_RESOLVER),
052 value.getSubAttributeValue("postalCode",
053 STRING_RESOLVER),
054 value.getSubAttributeValue("country",
055 STRING_RESOLVER),
056 value.getSubAttributeValue("type",
057 STRING_RESOLVER),
058 p == null ? false : p);
059 }
060
061 @Override
062 public SCIMAttributeValue fromInstance(
063 final AttributeDescriptor addressDescriptor, final Address value)
064 throws InvalidResourceException {
065 final List<SCIMAttribute> subAttributes =
066 new ArrayList<SCIMAttribute>(8);
067
068 if (value.type != null)
069 {
070 subAttributes.add(
071 SCIMAttribute.create(
072 addressDescriptor.getSubAttribute("type"),
073 SCIMAttributeValue.createStringValue(value.type)));
074 }
075
076 if (value.formatted != null)
077 {
078 subAttributes.add(
079 SCIMAttribute.create(
080 addressDescriptor.getSubAttribute("formatted"),
081 SCIMAttributeValue.createStringValue(value.formatted)));
082 }
083
084 if (value.streetAddress != null)
085 {
086 subAttributes.add(
087 SCIMAttribute.create(
088 addressDescriptor.getSubAttribute("streetAddress"),
089 SCIMAttributeValue.createStringValue(value.streetAddress)));
090 }
091
092 if (value.locality != null)
093 {
094 subAttributes.add(
095 SCIMAttribute.create(
096 addressDescriptor.getSubAttribute("locality"),
097 SCIMAttributeValue.createStringValue(value.locality)));
098 }
099
100 if (value.region != null)
101 {
102 subAttributes.add(
103 SCIMAttribute.create(
104 addressDescriptor.getSubAttribute("region"),
105 SCIMAttributeValue.createStringValue(value.region)));
106 }
107
108 if (value.postalCode != null)
109 {
110 subAttributes.add(
111 SCIMAttribute.create(
112 addressDescriptor.getSubAttribute("postalCode"),
113 SCIMAttributeValue.createStringValue(value.postalCode)));
114 }
115
116 if (value.country != null)
117 {
118 subAttributes.add(
119 SCIMAttribute.create(
120 addressDescriptor.getSubAttribute("country"),
121 SCIMAttributeValue.createStringValue(value.country)));
122 }
123
124 if (value.primary)
125 {
126 subAttributes.add(
127 SCIMAttribute.create(
128 addressDescriptor.getSubAttribute("primary"),
129 SCIMAttributeValue.createBooleanValue(value.primary)));
130 }
131
132 return SCIMAttributeValue.createComplexValue(subAttributes);
133 }
134 };
135
136
137
138 private String country;
139 private String formatted;
140 private String locality;
141 private String postalCode;
142 private String region;
143 private String streetAddress;
144 private String type;
145 private boolean primary;
146
147 /**
148 * Create an instance of the SCIM addresses attribute.
149 *
150 * @param primary Specifies whether this value is the primary value.
151 * @param streetAddress The full street address component, which may include
152 * house number, street name, PO BOX, and multi-line
153 * extended street address information.
154 * @param locality The city or locality component.
155 * @param region The state or region component.
156 * @param postalCode The zip code or postal code component.
157 * @param country The country name component.
158 * @param formatted The full mailing address, formatted for display or
159 * use with a mailing label.
160 * @param type The type of address, "work", "home" or "other".
161 */
162 public Address(final String formatted, final String streetAddress,
163 final String locality, final String region,
164 final String postalCode, final String country,
165 final String type, final boolean primary) {
166 this.country = country;
167 this.formatted = formatted;
168 this.locality = locality;
169 this.postalCode = postalCode;
170 this.primary = primary;
171 this.region = region;
172 this.streetAddress = streetAddress;
173 this.type = type;
174 }
175
176 /**
177 * Retrieves the country name component.
178 *
179 * @return The country name component.
180 */
181 public String getCountry() {
182 return country;
183 }
184
185 /**
186 * Sets the country name component.
187 *
188 * @param country The country name component.
189 */
190 public void setCountry(final String country) {
191 this.country = country;
192 }
193
194 /**
195 * Retrieves the full mailing address, formatted for display or use with a
196 * mailing label.
197 *
198 * @return The full mailing address
199 */
200 public String getFormatted() {
201 return formatted;
202 }
203
204 /**
205 * Sets the full mailing address, formatted for display or use with a
206 * mailing label.
207 *
208 * @param formatted The full mailing address.
209 */
210 public void setFormatted(final String formatted) {
211 this.formatted = formatted;
212 }
213
214 /**
215 * Retrieves the city or locality component.
216 * @return The city or locality component.
217 */
218 public String getLocality() {
219 return locality;
220 }
221
222 /**
223 * Sets the city or locality component.
224 * @param locality The city or locality component.
225 */
226 public void setLocality(final String locality) {
227 this.locality = locality;
228 }
229
230 /**
231 * Retrieves the zip code or postal code component.
232 * @return The zip code or postal code component.
233 */
234 public String getPostalCode() {
235 return postalCode;
236 }
237
238 /**
239 * Sets the zip code or postal code component.
240 * @param postalCode The zip code or postal code component.
241 */
242 public void setPostalCode(final String postalCode) {
243 this.postalCode = postalCode;
244 }
245
246 /**
247 * Whether this value is the primary value.
248 *
249 * @return <code>true</code> if this value is the primary value or
250 * <code>false</code> otherwise.
251 */
252 public boolean isPrimary() {
253 return primary;
254 }
255
256 /**
257 * Specifies whether this value is the primary value.
258 *
259 * @param primary Whether this value is the primary value.
260 */
261 public void setPrimary(final boolean primary) {
262 this.primary = primary;
263 }
264
265 /**
266 * Retrieves the state or region component.
267 *
268 * @return The state or region component.
269 */
270 public String getRegion() {
271 return region;
272 }
273
274 /**
275 * Sets the state or region component.
276 *
277 * @param region The state or region component.
278 */
279 public void setRegion(final String region) {
280 this.region = region;
281 }
282
283 /**
284 * Retrieves the full street address component, which may include house
285 * number, street name, PO BOX, and multi-line.
286 *
287 * @return The full street address component.
288 */
289 public String getStreetAddress() {
290 return streetAddress;
291 }
292
293 /**
294 * Sets The full street address component, which may include house number,
295 * street name, PO BOX, and multi-line.
296 *
297 * @param streetAddress The full street address component.
298 */
299 public void setStreetAddress(final String streetAddress) {
300 this.streetAddress = streetAddress;
301 }
302
303 /**
304 * Retrieves the type of address, "work", "home" or "other".
305 *
306 * @return The type of address.
307 */
308 public String getType() {
309 return type;
310 }
311
312 /**
313 * Sets the type of address, "work", "home" or "other".
314 *
315 * @param type he type of address.
316 */
317 public void setType(final String type) {
318 this.type = type;
319 }
320
321 /**
322 * {@inheritDoc}
323 */
324 @Override
325 public boolean equals(final Object o) {
326 if (this == o) {
327 return true;
328 }
329 if (o == null || getClass() != o.getClass()) {
330 return false;
331 }
332
333 Address address = (Address) o;
334
335 if (primary != address.primary) {
336 return false;
337 }
338 if (country != null ? !country.equals(address.country) :
339 address.country != null) {
340 return false;
341 }
342 if (formatted != null ? !formatted.equals(address.formatted) :
343 address.formatted != null) {
344 return false;
345 }
346 if (locality != null ? !locality.equals(address.locality) :
347 address.locality != null) {
348 return false;
349 }
350 if (postalCode != null ? !postalCode.equals(address.postalCode) :
351 address.postalCode != null) {
352 return false;
353 }
354 if (region != null ? !region.equals(address.region) :
355 address.region != null) {
356 return false;
357 }
358 if (streetAddress != null ? !streetAddress.equals(address.streetAddress) :
359 address.streetAddress != null) {
360 return false;
361 }
362 if (type != null ? !type.equals(address.type) : address.type != null) {
363 return false;
364 }
365
366 return true;
367 }
368
369 /**
370 * {@inheritDoc}
371 */
372 @Override
373 public int hashCode() {
374 int result = country != null ? country.hashCode() : 0;
375 result = 31 * result + (formatted != null ? formatted.hashCode() : 0);
376 result = 31 * result + (locality != null ? locality.hashCode() : 0);
377 result = 31 * result + (postalCode != null ? postalCode.hashCode() : 0);
378 result = 31 * result + (region != null ? region.hashCode() : 0);
379 result = 31 * result + (streetAddress != null ?
380 streetAddress.hashCode() : 0);
381 result = 31 * result + (type != null ? type.hashCode() : 0);
382 result = 31 * result + (primary ? 1 : 0);
383 return result;
384 }
385
386 /**
387 * {@inheritDoc}
388 */
389 @Override
390 public String toString() {
391 return "Address{" +
392 "formatted='" + formatted + '\'' +
393 ", streetAddress='" + streetAddress + '\'' +
394 ", locality='" + locality + '\'' +
395 ", region='" + region + '\'' +
396 ", postalCode='" + postalCode + '\'' +
397 ", country='" + country + '\'' +
398 ", type='" + type + '\'' +
399 ", primary=" + primary +
400 '}';
401 }
402 }