Debian Codegraph @

src/main/java/ezvcard/property/Agent.java

raw symbols SCIP
1 package ezvcard.property;
2
3 import java.text.NumberFormat;
4 import java.util.LinkedHashMap;
5 import java.util.List;
6 import java.util.Locale;
7 import java.util.Map;
8
9 import ezvcard.SupportedVersions;
10 import ezvcard.VCard;
11 import ezvcard.VCardVersion;
12 import ezvcard.ValidationWarnings;
13 import ezvcard.ValidationWarning;
14
15 /*
16 Copyright (c) 2012-2023, Michael Angstadt
17 All rights reserved.
18
19 Redistribution and use in source and binary forms, with or without
20 modification, are permitted provided that the following conditions are met:
21
22 1. Redistributions of source code must retain the above copyright notice, this
23 list of conditions and the following disclaimer.
24 2. Redistributions in binary form must reproduce the above copyright notice,
25 this list of conditions and the following disclaimer in the documentation
26 and/or other materials provided with the distribution.
27
28 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
29 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
30 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
31 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
32 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
33 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
34 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
35 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
37 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38
39 The views and conclusions contained in the software and documentation are those
40 of the authors and should not be interpreted as representing official policies,
41 either expressed or implied, of the FreeBSD Project.
42 */
43
44 /**
45 * <p>
46 * Defines information about the person's agent.
47 * </p>
48 *
49 * <p>
50 * <b>Code sample (creating)</b>
51 * </p>
52 *
53 * <pre class="brush:java">
54 * VCard vcard = new VCard();
55 *
56 * //URL
57 * Agent agent = new Agent("http://www.linkedin.com/BobSmith");
58 * vcard.setAgent(agent);
59 *
60 * //vCard
61 * VCard agentVCard = new VCard();
62 * agentVCard.setFormattedName("Bob Smith");
63 * agentVCard.addTelephoneNumber("(555) 123-4566");
64 * agentVCard.addUrl("http://www.linkedin.com/BobSmith");
65 * agent = new Agent(agentVCard);
66 * vcard.setAgent(agent);
67 * </pre>
68 *
69 * <p>
70 * <b>Code sample (retrieving)</b>
71 * </p>
72 *
73 * <pre class="brush:java">
74 * VCard vcard = ...
75 * Agent agent = vcard.getAgent();
76 *
77 * String url = agent.getUrl();
78 * if (url != null) {
79 * //property value is a URL
80 * }
81 *
82 * VCard agentVCard = agent.getVCard();
83 * if (agentVCard != null) {
84 * //property value is a vCard
85 * }
86 * </pre>
87 *
88 * <p>
89 * <b>Property name:</b> {@code AGENT}
90 * </p>
91 * <p>
92 * <b>Supported versions:</b> {@code 2.1, 3.0}
93 * </p>
94 *
95 * @author Michael Angstadt
96 * @see <a href="http://tools.ietf.org/html/rfc2426#page-19">RFC 2426 p.19</a>
97 * @see <a href="http://www.imc.org/pdi/vcard-21.doc">vCard 2.1 p.18</a>
98 */
99 @SupportedVersions({ VCardVersion.V2_1, VCardVersion.V3_0 })
100 public class Agent extends VCardProperty {
101 private String url;
102 private VCard vcard;
103
104 /**
105 * Creates an empty agent property.
106 */
107 public Agent() {
108 //empty
109 }
110
111 /**
112 * Creates an agent property.
113 * @param url a URL pointing to the agent's information
114 */
115 public Agent(String url) {
116 setUrl(url);
117 }
118
119 /**
120 * Creates an agent property.
121 * @param vcard a vCard containing the agent's information
122 */
123 public Agent(VCard vcard) {
124 setVCard(vcard);
125 }
126
127 /**
128 * Copy constructor.
129 * @param original the property to make a copy of
130 */
131 public Agent(Agent original) {
132 super(original);
133 url = original.url;
134 vcard = (original.vcard == null) ? null : new VCard(original.vcard);
135 }
136
137 /**
138 * Gets the URL to the agent's information.
139 * @return the URL or null if not set
140 */
141 public String getUrl() {
142 return url;
143 }
144
145 /**
146 * Sets the URL to the agent's information.
147 * @param url the URL
148 */
149 public void setUrl(String url) {
150 this.url = url;
151 vcard = null;
152 }
153
154 /**
155 * Gets an embedded vCard with the agent's information.
156 * @return the vCard or null if not set
157 */
158 public VCard getVCard() {
159 return vcard;
160 }
161
162 /**
163 * Sets an embedded vCard with the agent's information.
164 * @param vcard the vCard
165 */
166 public void setVCard(VCard vcard) {
167 this.vcard = vcard;
168 url = null;
169 }
170
171 @Override
172 protected void _validate(List<ValidationWarning> warnings, VCardVersion version, VCard vcard) {
173 if (url == null && this.vcard == null) {
174 warnings.add(new ValidationWarning(8));
175 }
176
177 if (this.vcard != null) {
178 NumberFormat nf = NumberFormat.getIntegerInstance(Locale.ROOT);
179 nf.setMinimumIntegerDigits(2);
180
181 ValidationWarnings validationWarnings = this.vcard.validate(version);
182 for (Map.Entry<VCardProperty, List<ValidationWarning>> entry : validationWarnings) {
183 VCardProperty property = entry.getKey();
184 List<ValidationWarning> propViolations = entry.getValue();
185
186 for (ValidationWarning propViolation : propViolations) {
187 String className = (property == null) ? "" : property.getClass().getSimpleName();
188
189 int code = propViolation.getCode();
190 String codeStr = (code >= 0) ? "W" + nf.format(code) : "";
191 String message = propViolation.getMessage();
192 warnings.add(new ValidationWarning(10, className, codeStr, message));
193 }
194 }
195 }
196 }
197
198 @Override
199 protected Map<String, Object> toStringValues() {
200 Map<String, Object> values = new LinkedHashMap<>();
201 values.put("url", url);
202 values.put("vcard", vcard);
203 return values;
204 }
205
206 @Override
207 public Agent copy() {
208 return new Agent(this);
209 }
210
211 @Override
212 public int hashCode() {
213 final int prime = 31;
214 int result = super.hashCode();
215 result = prime * result + ((url == null) ? 0 : url.hashCode());
216 result = prime * result + ((vcard == null) ? 0 : vcard.hashCode());
217 return result;
218 }
219
220 @Override
221 public boolean equals(Object obj) {
222 if (this == obj) return true;
223 if (!super.equals(obj)) return false;
224 Agent other = (Agent) obj;
225 if (url == null) {
226 if (other.url != null) return false;
227 } else if (!url.equals(other.url)) return false;
228 if (vcard == null) {
229 if (other.vcard != null) return false;
230 } else if (!vcard.equals(other.vcard)) return false;
231 return true;
232 }
233 }