001package ezvcard.property;
002
003import java.util.LinkedHashMap;
004import java.util.List;
005import java.util.Map;
006
007import ezvcard.VCard;
008import ezvcard.VCardVersion;
009import ezvcard.ValidationWarning;
010import ezvcard.util.GeoUri;
011
012/*
013 Copyright (c) 2012-2023, Michael Angstadt
014 All rights reserved.
015
016 Redistribution and use in source and binary forms, with or without
017 modification, are permitted provided that the following conditions are met: 
018
019 1. Redistributions of source code must retain the above copyright notice, this
020 list of conditions and the following disclaimer. 
021 2. Redistributions in binary form must reproduce the above copyright notice,
022 this list of conditions and the following disclaimer in the documentation
023 and/or other materials provided with the distribution. 
024
025 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
026 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
027 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
028 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
029 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
030 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
031 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
032 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
033 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
034 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
035
036 The views and conclusions contained in the software and documentation are those
037 of the authors and should not be interpreted as representing official policies, 
038 either expressed or implied, of the FreeBSD Project.
039 */
040
041/**
042 * Represents the location of a physical place.
043 * @author Michael Angstadt
044 * @see <a href="http://tools.ietf.org/html/rfc6474">RFC 6474</a>
045 */
046public class PlaceProperty extends VCardProperty implements HasAltId {
047        protected GeoUri geoUri;
048        protected String uri;
049        protected String text;
050
051        /**
052         * Creates a new place property.
053         */
054        public PlaceProperty() {
055                //empty
056        }
057
058        /**
059         * Creates a new place property.
060         * @param latitude the latitude coordinate of the place
061         * @param longitude the longitude coordinate of the place
062         */
063        public PlaceProperty(double latitude, double longitude) {
064                setCoordinates(latitude, longitude);
065        }
066
067        /**
068         * Creates a new place property.
069         * @param text a text value representing the place
070         */
071        public PlaceProperty(String text) {
072                setText(text);
073        }
074
075        /**
076         * Copy constructor.
077         * @param original the property to make a copy of
078         */
079        public PlaceProperty(PlaceProperty original) {
080                super(original);
081                geoUri = original.geoUri;
082                uri = original.uri;
083                text = original.text;
084        }
085
086        /**
087         * Gets the latitude of the location.
088         * @return the latitude or null if there is no geo information
089         */
090        public Double getLatitude() {
091                return (geoUri == null) ? null : geoUri.getCoordA();
092        }
093
094        /**
095         * Gets the longitude of the location.
096         * @return the longitude or null if there is no geo information
097         */
098        public Double getLongitude() {
099                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}