Debian Codegraph @

src/main/java/ezvcard/property/PlaceProperty.java

raw symbols SCIP
1 package ezvcard.property;
2
3 import java.util.LinkedHashMap;
4 import java.util.List;
5 import java.util.Map;
6
7 import ezvcard.VCard;
8 import ezvcard.VCardVersion;
9 import ezvcard.ValidationWarning;
10 import ezvcard.util.GeoUri;
11
12 /*
13 Copyright (c) 2012-2023, Michael Angstadt
14 All rights reserved.
15
16 Redistribution and use in source and binary forms, with or without
17 modification, are permitted provided that the following conditions are met:
18
19 1. Redistributions of source code must retain the above copyright notice, this
20 list of conditions and the following disclaimer.
21 2. Redistributions in binary form must reproduce the above copyright notice,
22 this list of conditions and the following disclaimer in the documentation
23 and/or other materials provided with the distribution.
24
25 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
26 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
27 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
29 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
30 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
32 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35
36 The views and conclusions contained in the software and documentation are those
37 of the authors and should not be interpreted as representing official policies,
38 either expressed or implied, of the FreeBSD Project.
39 */
40
41 /**
42 * Represents the location of a physical place.
43 * @author Michael Angstadt
44 * @see <a href="http://tools.ietf.org/html/rfc6474">RFC 6474</a>
45 */
46 public class PlaceProperty extends VCardProperty implements HasAltId {
47 protected GeoUri geoUri;
48 protected String uri;
49 protected String text;
50
51 /**
52 * Creates a new place property.
53 */
54 public PlaceProperty() {
55 //empty
56 }
57
58 /**
59 * Creates a new place property.
60 * @param latitude the latitude coordinate of the place
61 * @param longitude the longitude coordinate of the place
62 */
63 public PlaceProperty(double latitude, double longitude) {
64 setCoordinates(latitude, longitude);
65 }
66
67 /**
68 * Creates a new place property.
69 * @param text a text value representing the place
70 */
71 public PlaceProperty(String text) {
72 setText(text);
73 }
74
75 /**
76 * Copy constructor.
77 * @param original the property to make a copy of
78 */
79 public PlaceProperty(PlaceProperty original) {
80 super(original);
81 geoUri = original.geoUri;
82 uri = original.uri;
83 text = original.text;
84 }
85
86 /**
87 * Gets the latitude of the location.
88 * @return the latitude or null if there is no geo information
89 */
90 public Double getLatitude() {
91 return (geoUri == null) ? null : geoUri.getCoordA();
92 }
93
94 /**
95 * Gets the longitude of the location.
96 * @return the longitude or null if there is no geo information
97 */
98 public Double getLongitude() {
99 return (geoUri == null) ? null : geoUri.getCoordB();
100 }
101
102 /**
103 * Gets the location's geo position.
104 * @return the geo position or null if there is no geo information
105 */
106 public GeoUri getGeoUri() {
107 return geoUri;
108 }
109
110 /**
111 * Sets the property's value to a set of geo coordinates.
112 * @param latitude the latitude
113 * @param longitude the longitude
114 */
115 public void setCoordinates(double latitude, double longitude) {
116 setGeoUri(new GeoUri.Builder(latitude, longitude).build());
117 }
118
119 /**
120 * Sets the property's value to a set of geo coordinates.
121 * @param geoUri the geo URI
122 */
123 public void setGeoUri(GeoUri geoUri) {
124 this.geoUri = geoUri;
125 uri = null;
126 text = null;
127 }
128
129 /**
130 * Gets the URI representing the location.
131 * @return the URI or null if no URI is set
132 */
133 public String getUri() {
134 return uri;
135 }
136
137 /**
138 * Sets the property's value to a URI.
139 * @param uri the URI
140 */
141 public void setUri(String uri) {
142 this.uri = uri;
143 geoUri = null;
144 text = null;
145 }
146
147 /**
148 * Gets the text value representing the location.
149 * @return the text value or null if no text value is set
150 */
151 public String getText() {
152 return text;
153 }
154
155 /**
156 * Sets the property's value to a text value.
157 * @param text the text
158 */
159 public void setText(String text) {
160 this.text = text;
161 geoUri = null;
162 uri = null;
163 }
164
165 //@Override
166 public String getAltId() {
167 return parameters.getAltId();
168 }
169
170 //@Override
171 public void setAltId(String altId) {
172 parameters.setAltId(altId);
173 }
174
175 @Override
176 public String getLanguage() {
177 return super.getLanguage();
178 }
179
180 @Override
181 public void setLanguage(String language) {
182 super.setLanguage(language);
183 }
184
185 @Override
186 protected void _validate(List<ValidationWarning> warnings, VCardVersion version, VCard vcard) {
187 if (uri == null && text == null && geoUri == null) {
188 warnings.add(new ValidationWarning(8));
189 }
190 }
191
192 @Override
193 protected Map<String, Object> toStringValues() {
194 Map<String, Object> values = new LinkedHashMap<>();
195 values.put("geoUri", geoUri);
196 values.put("uri", uri);
197 values.put("text", text);
198 return values;
199 }
200
201 @Override
202 public int hashCode() {
203 final int prime = 31;
204 int result = super.hashCode();
205 result = prime * result + ((geoUri == null) ? 0 : geoUri.hashCode());
206 result = prime * result + ((text == null) ? 0 : text.hashCode());
207 result = prime * result + ((uri == null) ? 0 : uri.hashCode());
208 return result;
209 }
210
211 @Override
212 public boolean equals(Object obj) {
213 if (this == obj) return true;
214 if (!super.equals(obj)) return false;
215 PlaceProperty other = (PlaceProperty) obj;
216 if (geoUri == null) {
217 if (other.geoUri != null) return false;
218 } else if (!geoUri.equals(other.geoUri)) return false;
219 if (text == null) {
220 if (other.text != null) return false;
221 } else if (!text.equals(other.text)) return false;
222 if (uri == null) {
223 if (other.uri != null) return false;
224 } else if (!uri.equals(other.uri)) return false;
225 return true;
226 }
227 }