001package ezvcard.property;
002
003import java.util.LinkedHashMap;
004import java.util.List;
005import java.util.Map;
006import java.util.Objects;
007
008import ezvcard.VCard;
009import ezvcard.VCardVersion;
010import ezvcard.ValidationWarning;
011import ezvcard.util.GeoUri;
012
013/*
014 Copyright (c) 2012-2026, Michael Angstadt
015 All rights reserved.
016
017 Redistribution and use in source and binary forms, with or without
018 modification, are permitted provided that the following conditions are met: 
019
020 1. Redistributions of source code must retain the above copyright notice, this
021 list of conditions and the following disclaimer. 
022 2. Redistributions in binary form must reproduce the above copyright notice,
023 this list of conditions and the following disclaimer in the documentation
024 and/or other materials provided with the distribution. 
025
026 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
027 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
028 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
029 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
030 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
031 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
032 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
033 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
034 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
035 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
036
037 The views and conclusions contained in the software and documentation are those
038 of the authors and should not be interpreted as representing official policies, 
039 either expressed or implied, of the FreeBSD Project.
040 */
041
042/**
043 * Represents the location of a physical place.
044 * @author Michael Angstadt
045 * @see <a href="http://tools.ietf.org/html/rfc6474">RFC 6474</a>
046 */
047public class PlaceProperty extends VCardProperty implements HasAltId {
048        protected GeoUri geoUri;
049        protected String uri;
050        protected String text;
051
052        /**
053         * Creates a new place property.
054         */
055        public PlaceProperty() {
056                //empty
057        }
058
059        /**
060         * Creates a new place property.
061         * @param latitude the latitude coordinate of the place
062         * @param longitude the longitude coordinate of the place
063         */
064        public PlaceProperty(double latitude, double longitude) {
065                setCoordinates(latitude, longitude);
066        }
067
068        /**
069         * Creates a new place property.
070         * @param text a text value representing the place
071         */
072        public PlaceProperty(String text) {
073                setText(text);
074        }
075
076        /**
077         * Copy constructor.
078         * @param original the property to make a copy of
079         */
080        public PlaceProperty(PlaceProperty original) {
081                super(original);
082                geoUri = original.geoUri;
083                uri = original.uri;
084                text = original.text;
085        }
086
087        /**
088         * Gets the latitude of the location.
089         * @return the latitude or null if there is no geo information
090         */
091        public Double getLatitude() {
092                return (geoUri == null) ? null : geoUri.getCoordA();
093        }
094
095        /**
096         * Gets the longitude of the location.
097         * @return the longitude or null if there is no geo information
098         */
099        public Double getLongitude() {
100                return (geoUri == null) ? null : geoUri.getCoordB();
101        }
102
103        /**
104         * Gets the location's geo position.
105         * @return the geo position or null if there is no geo information
106         */
107        public GeoUri getGeoUri() {
108                return geoUri;
109        }
110
111        /**
112         * Sets the property's value to a set of geo coordinates.
113         * @param latitude the latitude
114         * @param longitude the longitude
115         */
116        public void setCoordinates(double latitude, double longitude) {
117                setGeoUri(new GeoUri.Builder(latitude, longitude).build());
118        }
119
120        /**
121         * Sets the property's value to a set of geo coordinates.
122         * @param geoUri the geo URI
123         */
124        public void setGeoUri(GeoUri geoUri) {
125                this.geoUri = geoUri;
126                uri = null;
127                text = null;
128        }
129
130        /**
131         * Gets the URI representing the location.
132         * @return the URI or null if no URI is set
133         */
134        public String getUri() {
135                return uri;
136        }
137
138        /**
139         * Sets the property's value to a URI.
140         * @param uri the URI
141         */
142        public void setUri(String uri) {
143                this.uri = uri;
144                geoUri = null;
145                text = null;
146        }
147
148        /**
149         * Gets the text value representing the location.
150         * @return the text value or null if no text value is set
151         */
152        public String getText() {
153                return text;
154        }
155
156        /**
157         * Sets the property's value to a text value.
158         * @param text the text
159         */
160        public void setText(String text) {
161                this.text = text;
162                geoUri = null;
163                uri = null;
164        }
165
166        //@Override
167        public String getAltId() {
168                return parameters.getAltId();
169        }
170
171        //@Override
172        public void setAltId(String altId) {
173                parameters.setAltId(altId);
174        }
175
176        @Override
177        public String getLanguage() {
178                return super.getLanguage();
179        }
180
181        @Override
182        public void setLanguage(String language) {
183                super.setLanguage(language);
184        }
185
186        @Override
187        protected void _validate(List<ValidationWarning> warnings, VCardVersion version, VCard vcard) {
188                if (uri == null && text == null && geoUri == null) {
189                        warnings.add(new ValidationWarning(8));
190                }
191        }
192
193        @Override
194        protected Map<String, Object> toStringValues() {
195                Map<String, Object> values = new LinkedHashMap<>();
196                values.put("geoUri", geoUri);
197                values.put("uri", uri);
198                values.put("text", text);
199                return values;
200        }
201
202        @Override
203        public int hashCode() {
204                final int prime = 31;
205                int result = super.hashCode();
206                result = prime * result + Objects.hash(geoUri, text, uri);
207                return result;
208        }
209
210        @Override
211        public boolean equals(Object obj) {
212                if (this == obj) return true;
213                if (!super.equals(obj)) return false;
214                if (getClass() != obj.getClass()) return false;
215                PlaceProperty other = (PlaceProperty) obj;
216                return Objects.equals(geoUri, other.geoUri) && Objects.equals(text, other.text) && Objects.equals(uri, other.uri);
217        }
218}