001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.juneau.bean.openapi3;
018
019import static org.apache.juneau.commons.utils.AssertionUtils.*;
020import static org.apache.juneau.commons.utils.CollectionUtils.*;
021import static org.apache.juneau.commons.utils.StringUtils.*;
022import static org.apache.juneau.commons.utils.Utils.*;
023import static org.apache.juneau.internal.ConverterUtils.*;
024
025import java.net.*;
026import java.util.*;
027
028import org.apache.juneau.*;
029import org.apache.juneau.commons.collections.*;
030
031/**
032 * Contact information for the exposed API.
033 *
034 * <p>
035 * The Contact Object provides contact information for the exposed API. This information can be used by clients
036 * to get in touch with the API maintainers for support, questions, or other inquiries.
037 *
038 * <h5 class='section'>OpenAPI Specification:</h5>
039 * <p>
040 * The Contact Object is composed of the following fields:
041 * <ul class='spaced-list'>
042 *    <li><c>name</c> (string) - The identifying name of the contact person/organization
043 *    <li><c>url</c> (string) - The URL pointing to the contact information
044 *    <li><c>email</c> (string) - The email address of the contact person/organization
045 * </ul>
046 *
047 * <h5 class='section'>Example:</h5>
048 * <p class='bcode'>
049 *    <jc>// Construct using SwaggerBuilder.</jc>
050 *    Contact <jv>x</jv> = <jsm>contact</jsm>(<js>"API Support"</js>, <js>"http://www.swagger.io/support"</js>, <js>"support@swagger.io"</js>);
051 *
052 *    <jc>// Serialize using JsonSerializer.</jc>
053 *    String <jv>json</jv> = Json.<jsm>from</jsm>(<jv>x</jv>);
054 *
055 *    <jc>// Or just use toString() which does the same as above.</jc>
056 *    String <jv>json</jv> = <jv>x</jv>.toString();
057 * </p>
058 * <p class='bcode'>
059 *    <jc>// Output</jc>
060 *    {
061 *       <js>"name"</js>: <js>"API Support"</js>,
062 *       <js>"url"</js>: <js>"http://www.swagger.io/support"</js>,
063 *       <js>"email"</js>: <js>"support@swagger.io"</js>
064 *    }
065 * </p>
066 *
067 * <h5 class='section'>See Also:</h5><ul>
068 *    <li class='link'><a class="doclink" href="https://spec.openapis.org/oas/v3.0.0#contact-object">OpenAPI Specification &gt; Contact Object</a>
069 *    <li class='link'><a class="doclink" href="https://swagger.io/docs/specification/api-general-info/">OpenAPI API General Info</a>
070 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauBeanOpenApi3">juneau-bean-openapi-v3</a>
071 * </ul>
072 */
073public class Contact extends OpenApiElement {
074
075   private String name;
076   private URI url;
077   private String email;
078
079   /**
080    * Default constructor.
081    */
082   public Contact() {}
083
084   /**
085    * Copy constructor.
086    *
087    * @param copyFrom The object to copy.
088    */
089   public Contact(Contact copyFrom) {
090      super(copyFrom);
091
092      this.name = copyFrom.name;
093      this.url = copyFrom.url;
094      this.email = copyFrom.email;
095   }
096
097   /**
098    * Make a deep copy of this object.
099    *
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}