001// ***************************************************************************************************************************
002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
003// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
005// * with the License.  You may obtain a copy of the License at                                                              *
006// *                                                                                                                         *
007// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
008// *                                                                                                                         *
009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
011// * specific language governing permissions and limitations under the License.                                              *
012// ***************************************************************************************************************************
013package org.apache.juneau.dto.swagger;
014
015import static org.apache.juneau.internal.BeanPropertyUtils.*;
016import static org.apache.juneau.internal.StringUtils.*;
017
018import java.net.*;
019import java.net.URI;
020import java.util.*;
021
022import org.apache.juneau.*;
023import org.apache.juneau.annotation.*;
024import org.apache.juneau.internal.*;
025import org.apache.juneau.utils.*;
026
027/**
028 * Contact information for the exposed API.
029 *
030 * <h5 class='section'>Example:</h5>
031 * <p class='bcode w800'>
032 *    <jc>// Construct using SwaggerBuilder.</jc>
033 *    Contact x = <jsm>contact</jsm>(<js>"API Support"</js>, <js>"http://www.swagger.io/support"</js>, <js>"support@swagger.io"</js>);
034 *
035 *    <jc>// Serialize using JsonSerializer.</jc>
036 *    String json = JsonSerializer.<jsf>DEFAULT</jsf>.toString(x);
037 *
038 *    <jc>// Or just use toString() which does the same as above.</jc>
039 *    String json = x.toString();
040 * </p>
041 * <p class='bcode w800'>
042 *    <jc>// Output</jc>
043 *    {
044 *       <js>"name"</js>: <js>"API Support"</js>,
045 *       <js>"url"</js>: <js>"http://www.swagger.io/support"</js>,
046 *       <js>"email"</js>: <js>"support@swagger.io"</js>
047 *    }
048 * </p>
049 *
050 * <h5 class='section'>See Also:</h5>
051 * <ul class='doctree'>
052 *    <li class='link'>{@doc juneau-dto.Swagger}
053 * </ul>
054 */
055@Bean(properties="name,url,email,*")
056public class Contact extends SwaggerElement {
057
058   private String name;
059   private URI url;
060   private String email;
061
062   /**
063    * Default constructor.
064    */
065   public Contact() {}
066
067   /**
068    * Copy constructor.
069    *
070    * @param copyFrom The object to copy.
071    */
072   public Contact(Contact copyFrom) {
073      super(copyFrom);
074
075      this.name = copyFrom.name;
076      this.url = copyFrom.url;
077      this.email = copyFrom.email;
078   }
079
080   /**
081    * Make a deep copy of this object.
082    *
083    * @return A deep copy of this object.
084    */
085   public Contact copy() {
086      return new Contact(this);
087   }
088
089   /**
090    * Bean property getter:  <property>name</property>.
091    *
092    * <p>
093    * The identifying name of the contact person/organization.
094    *
095    * @return The property value, or <jk>null</jk> if it is not set.
096    */
097   public String getName() {
098      return name;
099   }
100
101   /**
102    * Bean property setter:  <property>name</property>.
103    *
104    * <p>
105    * The identifying name of the contact person/organization.
106    *
107    * @param value
108    *    The new value for this property.
109    *    <br>Can be <jk>null</jk> to unset the property.
110    * @return This object (for method chaining).
111    */
112   public Contact setName(String value) {
113      name = value;
114      return this;
115   }
116
117   /**
118    * Same as {@link #setName(String)}.
119    *
120    * @param value
121    *    The new value for this property.
122    *    <br>Non-String values will be converted to String using <code>toString()</code>.
123    *    <br>Can be <jk>null</jk> to unset the property.
124    * @return This object (for method chaining).
125    */
126   public Contact name(Object value) {
127      return setName(toStringVal(value));
128   }
129
130   /**
131    * Bean property getter:  <property>url</property>.
132    *
133    * <p>
134    * The URL pointing to the contact information.
135    *
136    * @return The property value, or <jk>null</jk> if it is not set.
137    */
138   public URI getUrl() {
139      return url;
140   }
141
142   /**
143    * Bean property setter:  <property>url</property>.
144    *
145    * <p>
146    * The value can be of any of the following types: {@link URI}, {@link URL}, {@link String}.
147    * <br>Strings must be valid URIs.
148    *
149    * <p>
150    * URIs defined by {@link UriResolver} can be used for values.
151    *
152    * @param value
153    *    The new value for this property.
154    *    <br>Can be <jk>null</jk> to unset the property.
155    * @return This object (for method chaining).
156    */
157   public Contact setUrl(URI value) {
158      url = value;
159      return this;
160   }
161
162   /**
163    * Same as {@link #setUrl(URI)}.
164    *
165    * @param value
166    *    The new value for this property.
167    *    <br>Non-URI values will be converted to URI using <code><jk>new</jk> URI(value.toString())</code>.
168    *    <br>Can be <jk>null</jk> to unset the property.
169    * @return This object (for method chaining).
170    */
171   public Contact url(Object value) {
172      return setUrl(StringUtils.toURI(value));
173   }
174
175   /**
176    * Bean property getter:  <property>email</property>.
177    *
178    * <p>
179    * The email address of the contact person/organization.
180    *
181    * @return The property value, or <jk>null</jk> if it is not set.
182    */
183   public String getEmail() {
184      return email;
185   }
186
187   /**
188    * Bean property setter:  <property>email</property>.
189    *
190    * <p>
191    * The email address of the contact person/organization.
192    *
193    * @param value
194    *    The new value for this property.
195    *    <br>MUST be in the format of an email address.
196    *    <br>Can be <jk>null</jk> to unset the property.
197    * @return This object (for method chaining).
198    */
199   public Contact setEmail(String value) {
200      email = value;
201      return this;
202   }
203
204   /**
205    * Same as {@link #setEmail(String)}.
206    *
207    * @param value
208    *    The new value for this property.
209    *    <br>Non-String values will be converted to String using <code>toString()</code>.
210    *    <br>MUST be in the format of an email address.
211    *    <br>Can be <jk>null</jk> to unset the property.
212    * @return This object (for method chaining).
213    */
214   public Contact email(Object value) {
215      return setEmail(toStringVal(value));
216   }
217
218   /**
219    * Returns <jk>true</jk> if the name property is not null or empty.
220    *
221    * @return <jk>true</jk> if the name property is not null or empty.
222    */
223   public boolean hasName() {
224      return isNotEmpty(name);
225   }
226
227   /**
228    * Returns <jk>true</jk> if the URL property is not null.
229    *
230    * @return <jk>true</jk> if the URL property is not null.
231    */
232   public boolean hasUrl() {
233      return url != null;
234   }
235
236   /**
237    * Returns <jk>true</jk> if the email property is not null or empty.
238    *
239    * @return <jk>true</jk> if the email property is not null or empty.
240    */
241   public boolean hasEmail() {
242      return isNotEmpty(email);
243   }
244
245   @Override /* SwaggerElement */
246   public <T> T get(String property, Class<T> type) {
247      if (property == null)
248         return null;
249      switch (property) {
250         case "name": return toType(getName(), type);
251         case "url": return toType(getUrl(), type);
252         case "email": return toType(getEmail(), type);
253         default: return super.get(property, type);
254      }
255   }
256
257   @Override /* SwaggerElement */
258   public Contact set(String property, Object value) {
259      if (property == null)
260         return this;
261      switch (property) {
262         case "name": return name(value);
263         case "url": return url(value);
264         case "email": return email(value);
265         default:
266            super.set(property, value);
267            return this;
268      }
269   }
270
271   @Override /* SwaggerElement */
272   public Set<String> keySet() {
273      ASet<String> s = new ASet<String>()
274         .appendIf(name != null, "name")
275         .appendIf(url != null, "url")
276         .appendIf(email != null, "email");
277      return new MultiSet<>(s, super.keySet());
278   }
279}