View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.juneau.bean.openapi3;
18  
19  import static org.apache.juneau.commons.utils.AssertionUtils.*;
20  import static org.apache.juneau.commons.utils.CollectionUtils.*;
21  import static org.apache.juneau.commons.utils.StringUtils.*;
22  import static org.apache.juneau.commons.utils.Utils.*;
23  import static org.apache.juneau.internal.ConverterUtils.*;
24  
25  import java.net.*;
26  import java.util.*;
27  
28  import org.apache.juneau.*;
29  import org.apache.juneau.commons.collections.*;
30  
31  /**
32   * Contact information for the exposed API.
33   *
34   * <p>
35   * The Contact Object provides contact information for the exposed API. This information can be used by clients
36   * to get in touch with the API maintainers for support, questions, or other inquiries.
37   *
38   * <h5 class='section'>OpenAPI Specification:</h5>
39   * <p>
40   * The Contact Object is composed of the following fields:
41   * <ul class='spaced-list'>
42   * 	<li><c>name</c> (string) - The identifying name of the contact person/organization
43   * 	<li><c>url</c> (string) - The URL pointing to the contact information
44   * 	<li><c>email</c> (string) - The email address of the contact person/organization
45   * </ul>
46   *
47   * <h5 class='section'>Example:</h5>
48   * <p class='bcode'>
49   * 	<jc>// Construct using SwaggerBuilder.</jc>
50   * 	Contact <jv>x</jv> = <jsm>contact</jsm>(<js>"API Support"</js>, <js>"http://www.swagger.io/support"</js>, <js>"support@swagger.io"</js>);
51   *
52   * 	<jc>// Serialize using JsonSerializer.</jc>
53   * 	String <jv>json</jv> = Json.<jsm>from</jsm>(<jv>x</jv>);
54   *
55   * 	<jc>// Or just use toString() which does the same as above.</jc>
56   * 	String <jv>json</jv> = <jv>x</jv>.toString();
57   * </p>
58   * <p class='bcode'>
59   * 	<jc>// Output</jc>
60   * 	{
61   * 		<js>"name"</js>: <js>"API Support"</js>,
62   * 		<js>"url"</js>: <js>"http://www.swagger.io/support"</js>,
63   * 		<js>"email"</js>: <js>"support@swagger.io"</js>
64   * 	}
65   * </p>
66   *
67   * <h5 class='section'>See Also:</h5><ul>
68   * 	<li class='link'><a class="doclink" href="https://spec.openapis.org/oas/v3.0.0#contact-object">OpenAPI Specification &gt; Contact Object</a>
69   * 	<li class='link'><a class="doclink" href="https://swagger.io/docs/specification/api-general-info/">OpenAPI API General Info</a>
70   * 	<li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauBeanOpenApi3">juneau-bean-openapi-v3</a>
71   * </ul>
72   */
73  public class Contact extends OpenApiElement {
74  
75  	private String name;
76  	private URI url;
77  	private String email;
78  
79  	/**
80  	 * Default constructor.
81  	 */
82  	public Contact() {}
83  
84  	/**
85  	 * Copy constructor.
86  	 *
87  	 * @param copyFrom The object to copy.
88  	 */
89  	public Contact(Contact copyFrom) {
90  		super(copyFrom);
91  
92  		this.name = copyFrom.name;
93  		this.url = copyFrom.url;
94  		this.email = copyFrom.email;
95  	}
96  
97  	/**
98  	 * Make a deep copy of this object.
99  	 *
100 	 * @return A deep copy of this object.
101 	 */
102 	public Contact copy() {
103 		return new Contact(this);
104 	}
105 
106 	@Override /* Overridden from OpenApiElement */
107 	public <T> T get(String property, Class<T> type) {
108 		assertArgNotNull("property", property);
109 		return switch (property) {
110 			case "name" -> toType(getName(), type);
111 			case "url" -> toType(getUrl(), type);
112 			case "email" -> toType(getEmail(), type);
113 			default -> super.get(property, type);
114 		};
115 	}
116 
117 	/**
118 	 * Bean property getter:  <property>email</property>.
119 	 *
120 	 * <p>
121 	 * The email address of the contact person/organization.
122 	 *
123 	 * @return The property value, or <jk>null</jk> if it is not set.
124 	 */
125 	public String getEmail() { return email; }
126 
127 	/**
128 	 * Bean property getter:  <property>name</property>.
129 	 *
130 	 * <p>
131 	 * The identifying name of the contact person/organization.
132 	 *
133 	 * @return The property value, or <jk>null</jk> if it is not set.
134 	 */
135 	public String getName() { return name; }
136 
137 	/**
138 	 * Bean property getter:  <property>url</property>.
139 	 *
140 	 * <p>
141 	 * The URL pointing to the contact information.
142 	 *
143 	 * @return The property value, or <jk>null</jk> if it is not set.
144 	 */
145 	public URI getUrl() { return url; }
146 
147 	@Override /* Overridden from OpenApiElement */
148 	public Set<String> keySet() {
149 		// @formatter:off
150 		var s = setb(String.class)
151 			.addIf(nn(email), "email")
152 			.addIf(nn(name), "name")
153 			.addIf(nn(url), "url")
154 			.build();
155 		// @formatter:on
156 		return new MultiSet<>(s, super.keySet());
157 	}
158 
159 	@Override /* Overridden from OpenApiElement */
160 	public Contact set(String property, Object value) {
161 		assertArgNotNull("property", property);
162 		return switch (property) {
163 			case "email" -> setEmail(s(value));
164 			case "name" -> setName(s(value));
165 			case "url" -> setUrl(toUri(value));
166 			default -> {
167 				super.set(property, value);
168 				yield this;
169 			}
170 		};
171 	}
172 
173 	/**
174 	 * Bean property setter:  <property>email</property>.
175 	 *
176 	 * <p>
177 	 * The email address of the contact person/organization.
178 	 *
179 	 * @param value
180 	 * 	The new value for this property.
181 	 * 	<br>MUST be in the format of an email address.
182 	 * 	<br>Can be <jk>null</jk> to unset the property.
183 	 * @return This object
184 	 */
185 	public Contact setEmail(String value) {
186 		email = value;
187 		return this;
188 	}
189 
190 	/**
191 	 * Bean property setter:  <property>name</property>.
192 	 *
193 	 * <p>
194 	 * The identifying name of the contact person/organization.
195 	 *
196 	 * @param value
197 	 * 	The new value for this property.
198 	 * 	<br>Can be <jk>null</jk> to unset the property.
199 	 * @return This object
200 	 */
201 	public Contact setName(String value) {
202 		name = value;
203 		return this;
204 	}
205 
206 	/**
207 	 * Bean property setter:  <property>url</property>.
208 	 *
209 	 * <p>
210 	 * The value can be of any of the following types: {@link URI}, {@link URL}, {@link String}.
211 	 * <br>Strings must be valid URIs.
212 	 *
213 	 * <p>
214 	 * URIs defined by {@link UriResolver} can be used for values.
215 	 *
216 	 * @param value
217 	 * 	The new value for this property.
218 	 * 	<br>Can be <jk>null</jk> to unset the property.
219 	 * @return This object
220 	 */
221 	public Contact setUrl(URI value) {
222 		url = value;
223 		return this;
224 	}
225 
226 	@Override /* Overridden from OpenApiElement */
227 	public Contact strict() {
228 		super.strict();
229 		return this;
230 	}
231 
232 	@Override /* Overridden from OpenApiElement */
233 	public Contact strict(Object value) {
234 		super.strict(value);
235 		return this;
236 	}
237 }