Debian Codegraph @

src/main/java/ezvcard/property/DateOrTimeProperty.java

raw symbols SCIP
1 package ezvcard.property;
2
3 import java.time.Instant;
4 import java.time.LocalDate;
5 import java.time.LocalDateTime;
6 import java.time.OffsetDateTime;
7 import java.time.temporal.Temporal;
8 import java.util.LinkedHashMap;
9 import java.util.List;
10 import java.util.Map;
11 import java.util.Objects;
12
13 import ezvcard.VCard;
14 import ezvcard.VCardVersion;
15 import ezvcard.ValidationWarning;
16 import ezvcard.parameter.Calscale;
17 import ezvcard.parameter.VCardParameters;
18 import ezvcard.util.PartialDate;
19
20 /*
21 Copyright (c) 2012-2023, Michael Angstadt
22 All rights reserved.
23
24 Redistribution and use in source and binary forms, with or without
25 modification, are permitted provided that the following conditions are met:
26
27 1. Redistributions of source code must retain the above copyright notice, this
28 list of conditions and the following disclaimer.
29 2. Redistributions in binary form must reproduce the above copyright notice,
30 this list of conditions and the following disclaimer in the documentation
31 and/or other materials provided with the distribution.
32
33 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
34 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
35 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
36 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
37 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
38 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
39 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
40 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
41 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
42 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43
44 The views and conclusions contained in the software and documentation are those
45 of the authors and should not be interpreted as representing official policies,
46 either expressed or implied, of the FreeBSD Project.
47 */
48
49 /**
50 * Represents a property that typically contains a date/time value, but can also
51 * contain a partial date or free-text value.
52 * @author Michael Angstadt
53 */
54 public class DateOrTimeProperty extends VCardProperty implements HasAltId {
55 private String text;
56 private Temporal date;
57 private PartialDate partialDate;
58
59 /**
60 * Creates a date-and-or-time property.
61 * @param date the date value (should be one of the following:
62 * {@link LocalDate}, {@link LocalDateTime}, {@link OffsetDateTime},
63 * {@link Instant})
64 */
65 public DateOrTimeProperty(Temporal date) {
66 this.date = date;
67 }
68
69 /**
70 * Creates a date-and-or-time property.
71 * @param partialDate the partial date value (vCard 4.0 only)
72 */
73 public DateOrTimeProperty(PartialDate partialDate) {
74 setPartialDate(partialDate);
75 }
76
77 /**
78 * Creates a date-and-or-time property.
79 * @param text the text value (vCard 4.0 only)
80 */
81 public DateOrTimeProperty(String text) {
82 setText(text);
83 }
84
85 /**
86 * Copy constructor.
87 * @param original the property to make a copy of
88 */
89 public DateOrTimeProperty(DateOrTimeProperty original) {
90 super(original);
91 text = original.text;
92 date = original.date;
93 partialDate = original.partialDate;
94 }
95
96 /**
97 * Gets the date value. It should be an instance of one of the following
98 * classes: {@link LocalDate}, {@link LocalDateTime},
99 * {@link OffsetDateTime}, {@link Instant}.
100 * @return the date value or null if not set
101 */
102 public Temporal getDate() {
103 return date;
104 }
105
106 /**
107 * Sets the value of this property to a date. It should be an instance of
108 * one of the following classes: {@link LocalDate}, {@link LocalDateTime},
109 * {@link OffsetDateTime}, {@link Instant}.
110 * @param date the date
111 */
112 public void setDate(Temporal date) {
113 this.date = date;
114 text = null;
115 partialDate = null;
116 }
117
118 /**
119 * Gets the reduced accuracy or truncated date. This is only supported by
120 * vCard 4.0.
121 * @return the reduced accuracy or truncated date or null if not set
122 * @see <a href="http://tools.ietf.org/html/rfc6350">RFC 6350 p.12-14</a>
123 */
124 public PartialDate getPartialDate() {
125 return partialDate;
126 }
127
128 /**
129 * <p>
130 * Sets the value of this property to a reduced accuracy or truncated date.
131 * This is only supported by vCard 4.0.
132 * </p>
133 *
134 * <pre class="brush:java">
135 * Birthday bday = new Birthday();
136 * bday.setPartialDate(PartialDate.date(null, 4, 20)); //April 20
137 * </pre>
138 *
139 * @param partialDate the reduced accuracy or truncated date
140 * @see <a href="http://tools.ietf.org/html/rfc6350">RFC 6350 p.12-14</a>
141 */
142 public void setPartialDate(PartialDate partialDate) {
143 this.partialDate = partialDate;
144 text = null;
145 date = null;
146 }
147
148 /**
149 * Gets the text value of this type. This is only supported by vCard 4.0.
150 * @return the text value or null if not set
151 */
152 public String getText() {
153 return text;
154 }
155
156 /**
157 * Sets the value of this property to a text string. This is only supported
158 * by vCard 4.0.
159 * @param text the text value or null if not set
160 */
161 public void setText(String text) {
162 this.text = text;
163 date = null;
164 partialDate = null;
165 }
166
167 /**
168 * <p>
169 * Gets the type of calendar that is used for a date or date-time property
170 * value.
171 * </p>
172 * <p>
173 * <b>Supported versions:</b> {@code 4.0}
174 * </p>
175 * @return the type of calendar or null if not found
176 * @see VCardParameters#getCalscale
177 */
178 public Calscale getCalscale() {
179 return parameters.getCalscale();
180 }
181
182 /**
183 * <p>
184 * Sets the type of calendar that is used for a date or date-time property
185 * value.
186 * </p>
187 * <p>
188 * <b>Supported versions:</b> {@code 4.0}
189 * </p>
190 * @param calscale the type of calendar or null to remove
191 * @see VCardParameters#setCalscale
192 */
193 public void setCalscale(Calscale calscale) {
194 parameters.setCalscale(calscale);
195 }
196
197 @Override
198 public String getLanguage() {
199 return super.getLanguage();
200 }
201
202 @Override
203 public void setLanguage(String language) {
204 super.setLanguage(language);
205 }
206
207 //@Override
208 public String getAltId() {
209 return parameters.getAltId();
210 }
211
212 //@Override
213 public void setAltId(String altId) {
214 parameters.setAltId(altId);
215 }
216
217 @Override
218 protected void _validate(List<ValidationWarning> warnings, VCardVersion version, VCard vcard) {
219 if (date == null && partialDate == null && text == null) {
220 warnings.add(new ValidationWarning(8));
221 }
222
223 if (version == VCardVersion.V2_1 || version == VCardVersion.V3_0) {
224 if (text != null) {
225 warnings.add(new ValidationWarning(11));
226 }
227 if (partialDate != null) {
228 warnings.add(new ValidationWarning(12));
229 }
230 }
231 }
232
233 @Override
234 protected Map<String, Object> toStringValues() {
235 Map<String, Object> values = new LinkedHashMap<>();
236 values.put("text", text);
237 values.put("date", date);
238 values.put("partialDate", partialDate);
239 return values;
240 }
241
242 @Override
243 public int hashCode() {
244 final int prime = 31;
245 int result = super.hashCode();
246 result = prime * result + Objects.hash(date, partialDate, text);
247 return result;
248 }
249
250 @Override
251 public boolean equals(Object obj) {
252 if (this == obj) return true;
253 if (!super.equals(obj)) return false;
254 if (getClass() != obj.getClass()) return false;
255 DateOrTimeProperty other = (DateOrTimeProperty) obj;
256 return Objects.equals(date, other.date) && Objects.equals(partialDate, other.partialDate) && Objects.equals(text, other.text);
257 }
258 }