View Javadoc
1   package nl.mineleni.openls.databinding.openls;
2   
3   import java.util.HashMap;
4   import java.util.Map;
5   import java.util.Vector;
6   
7   import nl.mineleni.openls.XmlNamespaceConstants;
8   
9   /**
10   * OLS Address.
11   * 
12   * http://schemas.opengis.net/ols/1.2.0/ADT.xsd
13   * 
14   * <pre>
15   * 
16   * &lt;complexType name="AddressType"&gt;
17   *   &lt;annotation&gt;
18   *     &lt;documentation&gt;Defines an address&lt;/documentation&gt;
19   *   &lt;/annotation&gt;
20   *   &lt;complexContent&gt;
21   *     &lt;extension base="xls:AbstractAddressType"&gt;
22   *       &lt;choice&gt;
23   *         &lt;element name="freeFormAddress" type="string"&gt;
24   *           &lt;annotation&gt;
25   *             &lt;documentation&gt;An unstructured free form address.&lt;/documentation&gt;
26   *           &lt;/annotation&gt;
27   *         &lt;/element&gt;
28   *         &lt;sequence&gt;
29   *           &lt;element ref="xls:StreetAddress" /&gt;
30   *           &lt;element ref="xls:Place" minOccurs="0" maxOccurs="unbounded" /&gt;
31   *           &lt;element ref="xls:PostalCode" minOccurs="0" /&gt;
32   *         &lt;/sequence&gt;
33   *       &lt;/choice&gt;
34   *     &lt;/extension&gt;
35   *   &lt;/complexContent&gt;
36   * &lt;/complexType&gt;
37   * 
38   * </pre>
39   * 
40   * @author mprins
41   * @since 1.7
42   */
43  public class Address implements XmlNamespaceConstants {
44  	/**
45  	 * serialization id.
46  	 */
47  	private static final long serialVersionUID = -4508692290538906979L;
48  
49  	/** The country code. */
50  	private String countryCode;
51  
52  	/** The street address. */
53  	private StreetAddress streetAddress;
54  
55  	/** The postal code. */
56  	private PostalCode postalCode;
57  
58  	/** The place. */
59  	private final Vector<Place> place = new Vector<>();
60  
61  	/** The place map. */
62  	private final Map<String, String> placeMap = new HashMap<>();
63  
64  	/** The has country code. */
65  	private boolean hasCountryCode;
66  
67  	/** The has street address. */
68  	private boolean hasStreetAddress;
69  
70  	/** The has postal code. */
71  	private boolean hasPostalCode;
72  
73  	/**
74  	 * Instantiates a new address.
75  	 */
76  	public Address() {
77  		this.hasCountryCode = false;
78  		this.hasStreetAddress = false;
79  		this.hasPostalCode = false;
80  	}
81  
82  	/**
83  	 * Instantiates a new address.
84  	 * 
85  	 * @param countryCode
86  	 *            the country code
87  	 * @param streetAddress
88  	 *            the street address
89  	 * @param postalCode
90  	 *            the postal code
91  	 */
92  	public Address(final String countryCode, final StreetAddress streetAddress,
93  			final PostalCode postalCode) {
94  		this.hasCountryCode = true;
95  		this.countryCode = countryCode;
96  		this.hasStreetAddress = true;
97  		this.streetAddress = streetAddress;
98  		this.hasPostalCode = true;
99  		this.postalCode = postalCode;
100 	}
101 
102 	/**
103 	 * Sets the country code.
104 	 * 
105 	 * @param countryCode
106 	 *            the new country code
107 	 */
108 	public void setCountryCode(final String countryCode) {
109 		this.hasCountryCode = true;
110 		this.countryCode = countryCode;
111 	}
112 
113 	/**
114 	 * Gets the country code.
115 	 * 
116 	 * @return the country code
117 	 */
118 	public String getCountryCode() {
119 		return this.countryCode;
120 	}
121 
122 	/**
123 	 * Checks for country code.
124 	 * 
125 	 * @return true, if successful
126 	 */
127 	public boolean hasCountryCode() {
128 		return this.hasCountryCode;
129 	}
130 
131 	/**
132 	 * Sets the street address.
133 	 * 
134 	 * @param streetAddress
135 	 *            the new street address
136 	 */
137 	public void setStreetAddress(final StreetAddress streetAddress) {
138 		this.hasStreetAddress = true;
139 		this.streetAddress = streetAddress;
140 	}
141 
142 	/**
143 	 * Gets the street address.
144 	 * 
145 	 * @return the street address
146 	 */
147 	public StreetAddress getStreetAddress() {
148 		return this.streetAddress;
149 	}
150 
151 	/**
152 	 * Checks for street address.
153 	 * 
154 	 * @return true, if successful
155 	 */
156 	public boolean hasStreetAddress() {
157 		return this.hasStreetAddress;
158 	}
159 
160 	/**
161 	 * Sets the postal code.
162 	 * 
163 	 * @param postalCode
164 	 *            the new postal code
165 	 */
166 	public void setPostalCode(final PostalCode postalCode) {
167 		this.hasPostalCode = true;
168 		this.postalCode = postalCode;
169 	}
170 
171 	/**
172 	 * Gets the postal code.
173 	 * 
174 	 * @return the postal code
175 	 */
176 	public PostalCode getPostalCode() {
177 		return this.postalCode;
178 	}
179 
180 	/**
181 	 * Checks for postal code.
182 	 * 
183 	 * @return true, if successful
184 	 */
185 	public boolean hasPostalCode() {
186 		return this.hasPostalCode;
187 	}
188 
189 	/**
190 	 * Adds the place.
191 	 * 
192 	 * @param val
193 	 *            the val
194 	 */
195 	public void addPlace(final Place val) {
196 		this.place.add(val);
197 		if (!val.getType().equals("")) {
198 			this.placeMap.put(val.getType(), val.getPlace());
199 		}
200 	}
201 
202 	/**
203 	 * Gets the place at.
204 	 * 
205 	 * @param i
206 	 *            the i
207 	 * @return the place at
208 	 */
209 	public Place getPlaceAt(final int i) {
210 		return this.place.get(i);
211 	}
212 
213 	/**
214 	 * Gets the place size.
215 	 * 
216 	 * @return the place size
217 	 */
218 	public int getPlaceSize() {
219 		return this.place.size();
220 	}
221 
222 	/**
223 	 * Gets the place by type.
224 	 * 
225 	 * @param type
226 	 *            the type
227 	 * @return the place by type
228 	 */
229 	public String getPlaceByType(final String type) {
230 		return this.placeMap.get(type);
231 	}
232 
233 	/**
234 	 * Checks for place type.
235 	 * 
236 	 * @param type
237 	 *            the type
238 	 * @return true, if successful
239 	 */
240 	public boolean hasPlaceType(final String type) {
241 		return (this.placeMap.get(type) != null);
242 	}
243 
244 	/*
245 	 * (non-Javadoc)
246 	 * 
247 	 * @see nl.mineleni.openls.databinding.common.XmlNamespaceConstants#toXML()
248 	 */
249 	@Override
250 	public String toXML() {
251 		final StringBuilder sb = new StringBuilder("<"
252 				+ XmlNamespaceConstants.OPENLS_NAMESPACE_PREFIX + ":Address");
253 		if (this.hasCountryCode()) {
254 			sb.append(" countryCode=\"" + this.getCountryCode() + "\"");
255 		}
256 		sb.append(">");
257 		if (this.hasStreetAddress()) {
258 			sb.append(this.streetAddress.toXML());
259 		}
260 		for (final Place pla : this.place) {
261 			sb.append(pla.toXML());
262 		}
263 		if (this.hasPostalCode()) {
264 			sb.append(this.postalCode.toXML());
265 		}
266 		sb.append("</" + XmlNamespaceConstants.OPENLS_NAMESPACE_PREFIX
267 				+ ":Address>");
268 		return sb.toString();
269 	}
270 }