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.swagger;
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.commons.collections.*;
029
030/**
031 * Contact information for the exposed API.
032 *
033 * <p>
034 * The Contact Object provides contact information for the exposed API in Swagger 2.0. This information can be used
035 * by clients to get in touch with the API maintainers for support, questions, or other inquiries.
036 *
037 * <h5 class='section'>Swagger Specification:</h5>
038 * <p>
039 * The Contact Object is composed of the following fields:
040 * <ul class='spaced-list'>
041 *    <li><c>name</c> (string) - The identifying name of the contact person/organization
042 *    <li><c>url</c> (string) - The URL pointing to the contact information
043 *    <li><c>email</c> (string) - The email address of the contact person/organization
044 * </ul>
045 *
046 * <h5 class='section'>Example:</h5>
047 * <p class='bjava'>
048 *    <jc>// Construct using SwaggerBuilder.</jc>
049 *    Contact <jv>contact</jv> = <jsm>contact</jsm>(<js>"API Support"</js>, <js>"http://www.swagger.io/support"</js>, <js>"support@swagger.io"</js>);
050 *
051 *    <jc>// Serialize using JsonSerializer.</jc>
052 *    String <jv>json</jv> = Json.<jsm>from</jsm>(<jv>contact</jv>);
053 *
054 *    <jc>// Or just use toString() which does the same as above.</jc>
055 *    <jv>json</jv> = <jv>contact</jv>.toString();
056 * </p>
057 * <p class='bjson'>
058 *    <jc>// Output</jc>
059 *    {
060 *       <js>"name"</js>: <js>"API Support"</js>,
061 *       <js>"url"</js>: <js>"http://www.swagger.io/support"</js>,
062 *       <js>"email"</js>: <js>"support@swagger.io"</js>
063 *    }
064 * </p>
065 *
066 * <h5 class='section'>See Also:</h5><ul>
067 *    <li class='link'><a class="doclink" href="https://swagger.io/specification/v2/#contact-object">Swagger 2.0 Specification &gt; Contact Object</a>
068 *    <li class='link'><a class="doclink" href="https://swagger.io/docs/specification/2-0/api-general-info/">Swagger API General Info</a>
069 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauBeanSwagger2">juneau-bean-swagger-v2</a>
070 * </ul>
071 */
072public class Contact extends SwaggerElement {
073
074   private String name;
075   private URI url;
076   private String email;
077
078   /**
079    * Default constructor.
080    */
081   public Contact() {}
082
083   /**
084    * Copy constructor.
085    *
086    * @param copyFrom The object to copy.
087    */
088   public Contact(Contact copyFrom) {
089      super(copyFrom);
090
091      this.email = copyFrom.email;
092      this.name = copyFrom.name;
093      this.url = copyFrom.url;
094   }
095
096   /**
097    * Make a deep copy of this object.
098    *
099    * @return A deep copy of this object.
100    */
101   public Contact copy() {
102      return new Contact(this);
103   }
104
105   @Override /* Overridden from SwaggerElement */
106   public <T> T get(String property, Class<T> type) {
107      assertArgNotNull("property", property);
108      return switch (property) {
109         case "email" -> toType(getEmail(), type);
110         case "name" -> toType(getName(), type);
111         case "url" -> toType(getUrl(), type);
112         default -> super.get(property, type);
113      };
114   }
115
116   /**
117    * Bean property getter:  <property>email</property>.
118    *
119    * <p>
120    * The email address of the contact person/organization.
121    *
122    * @return The property value, or <jk>null</jk> if it is not set.
123    */
124   public String getEmail() { return email; }
125
126   /**
127    * Bean property getter:  <property>name</property>.
128    *
129    * <p>
130    * The identifying name of the contact person/organization.
131    *
132    * @return The property value, or <jk>null</jk> if it is not set.
133    */
134   public String getName() { return name; }
135
136   /**
137    * Bean property getter:  <property>url</property>.
138    *
139    * <p>
140    * The URL pointing to the contact information.
141    *
142    * @return The property value, or <jk>null</jk> if it is not set.
143    */
144   public URI getUrl() { return url; }
145
146   @Override /* Overridden from SwaggerElement */
147   public Set<String> keySet() {
148      // @formatter:off
149      var s = setb(String.class)
150         .addIf(nn(email), "email")
151         .addIf(nn(name), "name")
152         .addIf(nn(url), "url")
153         .build();
154      // @formatter:on
155      return new MultiSet<>(s, super.keySet());
156   }
157
158   @Override /* Overridden from SwaggerElement */
159   public Contact set(String property, Object value) {
160      assertArgNotNull("property", property);
161      return switch (property) {
162         case "email" -> setEmail(s(value));
163         case "name" -> setName(s(value));
164         case "url" -> setUrl(toUri(value));
165         default -> {
166            super.set(property, value);
167            yield this;
168         }
169      };
170   }
171
172   /**
173    * Bean property setter:  <property>email</property>.
174    *
175    * <p>
176    * The email address of the contact person/organization.
177    *
178    * @param value
179    *    The new value for this property.
180    *    <br>MUST be in the format of an email address.
181    *    <br>Can be <jk>null</jk> to unset the property.
182    * @return This object.
183    */
184   public Contact setEmail(String value) {
185      email = value;
186      return this;
187   }
188
189   /**
190    * Bean property setter:  <property>name</property>.
191    *
192    * <p>
193    * The identifying name of the contact person/organization.
194    *
195    * @param value
196    *    The new value for this property.
197    *    <br>Can be <jk>null</jk> to unset the property.
198    * @return This object.
199    */
200   public Contact setName(String value) {
201      name = value;
202      return this;
203   }
204
205   /**
206    * Bean property setter:  <property>url</property>.
207    *
208    * <p>
209    * The URL pointing to the contact information.
210    *
211    * @param value
212    *    The new value for this property.
213    *    <br>Can be <jk>null</jk> to unset the property.
214    * @return This object.
215    */
216   public Contact setUrl(URI value) {
217      url = value;
218      return this;
219   }
220
221   /**
222    * Sets strict mode on this bean.
223    *
224    * @return This object.
225    */
226   @Override
227   public Contact strict() {
228      super.strict();
229      return this;
230   }
231
232   /**
233    * Sets strict mode on this bean.
234    *
235    * @param value
236    *    The new value for this property.
237    *    <br>Non-boolean values will be converted to boolean using <code>Boolean.<jsm>valueOf</jsm>(value.toString())</code>.
238    *    <br>Can be <jk>null</jk> (interpreted as <jk>false</jk>).
239    * @return This object.
240    */
241   @Override
242   public Contact strict(Object value) {
243      super.strict(value);
244      return this;
245   }
246}