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.openapi3;
014
015import static org.apache.juneau.common.internal.StringUtils.*;
016import static org.apache.juneau.internal.CollectionUtils.*;
017import static org.apache.juneau.internal.ConverterUtils.*;
018
019import org.apache.juneau.UriResolver;
020import org.apache.juneau.annotation.Bean;
021import org.apache.juneau.internal.*;
022
023import java.net.URI;
024import java.net.URL;
025import java.util.Set;
026
027/**
028 * Contact information for the exposed API.
029 *
030 * <h5 class='section'>Example:</h5>
031 * <p class='bcode'>
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'>
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@Bean(properties="name,url,email,*")
051@FluentSetters
052public class Contact extends OpenApiElement {
053
054   private String name;
055   private URI url;
056   private String email;
057
058   /**
059    * Default constructor.
060    */
061   public Contact() {}
062
063   /**
064    * Copy constructor.
065    *
066    * @param copyFrom The object to copy.
067    */
068   public Contact(Contact copyFrom) {
069      super(copyFrom);
070
071      this.name = copyFrom.name;
072      this.url = copyFrom.url;
073      this.email = copyFrom.email;
074   }
075
076   /**
077    * Make a deep copy of this object.
078    *
079    * @return A deep copy of this object.
080    */
081   public Contact copy() {
082      return new Contact(this);
083   }
084
085   /**
086    * Bean property getter:  <property>name</property>.
087    *
088    * <p>
089    * The identifying name of the contact person/organization.
090    *
091    * @return The property value, or <jk>null</jk> if it is not set.
092    */
093   public String getName() {
094      return name;
095   }
096
097   /**
098    * Bean property setter:  <property>name</property>.
099    *
100    * <p>
101    * The identifying name of the contact person/organization.
102    *
103    * @param value
104    *    The new value for this property.
105    *    <br>Can be <jk>null</jk> to unset the property.
106    * @return This object
107    */
108   public Contact setName(String value) {
109      name = value;
110      return this;
111   }
112
113   /**
114    * Bean property getter:  <property>url</property>.
115    *
116    * <p>
117    * The URL pointing to the contact information.
118    *
119    * @return The property value, or <jk>null</jk> if it is not set.
120    */
121   public URI getUrl() {
122      return url;
123   }
124
125   /**
126    * Bean property setter:  <property>url</property>.
127    *
128    * <p>
129    * The value can be of any of the following types: {@link URI}, {@link URL}, {@link String}.
130    * <br>Strings must be valid URIs.
131    *
132    * <p>
133    * URIs defined by {@link UriResolver} can be used for values.
134    *
135    * @param value
136    *    The new value for this property.
137    *    <br>Can be <jk>null</jk> to unset the property.
138    * @return This object
139    */
140   public Contact setUrl(URI value) {
141      url = value;
142      return this;
143   }
144
145   /**
146    * Bean property getter:  <property>email</property>.
147    *
148    * <p>
149    * The email address of the contact person/organization.
150    *
151    * @return The property value, or <jk>null</jk> if it is not set.
152    */
153   public String getEmail() {
154      return email;
155   }
156
157   /**
158    * Bean property setter:  <property>email</property>.
159    *
160    * <p>
161    * The email address of the contact person/organization.
162    *
163    * @param value
164    *    The new value for this property.
165    *    <br>MUST be in the format of an email address.
166    *    <br>Can be <jk>null</jk> to unset the property.
167    * @return This object
168    */
169   public Contact setEmail(String value) {
170      email = value;
171      return this;
172   }
173
174   // <FluentSetters>
175
176   // </FluentSetters>
177
178   @Override /* OpenApiElement */
179   public <T> T get(String property, Class<T> type) {
180      if (property == null)
181         return null;
182      switch (property) {
183         case "name": return toType(getName(), type);
184         case "url": return toType(getUrl(), type);
185         case "email": return toType(getEmail(), type);
186         default: return super.get(property, type);
187      }
188   }
189
190   @Override /* OpenApiElement */
191   public Contact set(String property, Object value) {
192      if (property == null)
193         return this;
194      switch (property) {
195         case "name": return setName(stringify(value));
196         case "url": return setUrl(toURI(value));
197         case "email": return setEmail(stringify(value));
198         default:
199            super.set(property, value);
200            return this;
201      }
202   }
203
204   @Override /* OpenApiElement */
205   public Set<String> keySet() {
206      Set<String> s = setBuilder(String.class)
207         .addIf(name != null, "name")
208         .addIf(url != null, "url")
209         .addIf(email != null, "email")
210         .build();
211      return new MultiSet<>(s, super.keySet());
212   }
213}