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.StringUtils.*; 016import static org.apache.juneau.internal.ObjectUtils.*; 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 * <ul class='seealso'> 051 * <li class='link'>{@doc juneau-dto.Swagger} 052 * </ul> 053 */ 054@Bean(bpi="name,url,email,*") 055public class Contact extends SwaggerElement { 056 057 private String name; 058 private URI url; 059 private String email; 060 061 /** 062 * Default constructor. 063 */ 064 public Contact() {} 065 066 /** 067 * Copy constructor. 068 * 069 * @param copyFrom The object to copy. 070 */ 071 public Contact(Contact copyFrom) { 072 super(copyFrom); 073 074 this.name = copyFrom.name; 075 this.url = copyFrom.url; 076 this.email = copyFrom.email; 077 } 078 079 /** 080 * Make a deep copy of this object. 081 * 082 * @return A deep copy of this object. 083 */ 084 public Contact copy() { 085 return new Contact(this); 086 } 087 088 /** 089 * Bean property getter: <property>name</property>. 090 * 091 * <p> 092 * The identifying name of the contact person/organization. 093 * 094 * @return The property value, or <jk>null</jk> if it is not set. 095 */ 096 public String getName() { 097 return name; 098 } 099 100 /** 101 * Bean property setter: <property>name</property>. 102 * 103 * <p> 104 * The identifying name of the contact person/organization. 105 * 106 * @param value 107 * The new value for this property. 108 * <br>Can be <jk>null</jk> to unset the property. 109 * @return This object (for method chaining). 110 */ 111 public Contact setName(String value) { 112 name = value; 113 return this; 114 } 115 116 /** 117 * Same as {@link #setName(String)}. 118 * 119 * @param value 120 * The new value for this property. 121 * <br>Non-String values will be converted to String using <c>toString()</c>. 122 * <br>Can be <jk>null</jk> to unset the property. 123 * @return This object (for method chaining). 124 */ 125 public Contact name(Object value) { 126 return setName(stringify(value)); 127 } 128 129 /** 130 * Bean property getter: <property>url</property>. 131 * 132 * <p> 133 * The URL pointing to the contact information. 134 * 135 * @return The property value, or <jk>null</jk> if it is not set. 136 */ 137 public URI getUrl() { 138 return url; 139 } 140 141 /** 142 * Bean property setter: <property>url</property>. 143 * 144 * <p> 145 * The value can be of any of the following types: {@link URI}, {@link URL}, {@link String}. 146 * <br>Strings must be valid URIs. 147 * 148 * <p> 149 * URIs defined by {@link UriResolver} can be used for values. 150 * 151 * @param value 152 * The new value for this property. 153 * <br>Can be <jk>null</jk> to unset the property. 154 * @return This object (for method chaining). 155 */ 156 public Contact setUrl(URI value) { 157 url = value; 158 return this; 159 } 160 161 /** 162 * Same as {@link #setUrl(URI)}. 163 * 164 * @param value 165 * The new value for this property. 166 * <br>Non-URI values will be converted to URI using <code><jk>new</jk> URI(value.toString())</code>. 167 * <br>Can be <jk>null</jk> to unset the property. 168 * @return This object (for method chaining). 169 */ 170 public Contact url(Object value) { 171 return setUrl(StringUtils.toURI(value)); 172 } 173 174 /** 175 * Bean property getter: <property>email</property>. 176 * 177 * <p> 178 * The email address of the contact person/organization. 179 * 180 * @return The property value, or <jk>null</jk> if it is not set. 181 */ 182 public String getEmail() { 183 return email; 184 } 185 186 /** 187 * Bean property setter: <property>email</property>. 188 * 189 * <p> 190 * The email address of the contact person/organization. 191 * 192 * @param value 193 * The new value for this property. 194 * <br>MUST be in the format of an email address. 195 * <br>Can be <jk>null</jk> to unset the property. 196 * @return This object (for method chaining). 197 */ 198 public Contact setEmail(String value) { 199 email = value; 200 return this; 201 } 202 203 /** 204 * Same as {@link #setEmail(String)}. 205 * 206 * @param value 207 * The new value for this property. 208 * <br>Non-String values will be converted to String using <c>toString()</c>. 209 * <br>MUST be in the format of an email address. 210 * <br>Can be <jk>null</jk> to unset the property. 211 * @return This object (for method chaining). 212 */ 213 public Contact email(Object value) { 214 return setEmail(stringify(value)); 215 } 216 217 /** 218 * Returns <jk>true</jk> if the name property is not null or empty. 219 * 220 * @return <jk>true</jk> if the name property is not null or empty. 221 */ 222 public boolean hasName() { 223 return isNotEmpty(name); 224 } 225 226 /** 227 * Returns <jk>true</jk> if the URL property is not null. 228 * 229 * @return <jk>true</jk> if the URL property is not null. 230 */ 231 public boolean hasUrl() { 232 return url != null; 233 } 234 235 /** 236 * Returns <jk>true</jk> if the email property is not null or empty. 237 * 238 * @return <jk>true</jk> if the email property is not null or empty. 239 */ 240 public boolean hasEmail() { 241 return isNotEmpty(email); 242 } 243 244 @Override /* SwaggerElement */ 245 public <T> T get(String property, Class<T> type) { 246 if (property == null) 247 return null; 248 switch (property) { 249 case "name": return toType(getName(), type); 250 case "url": return toType(getUrl(), type); 251 case "email": return toType(getEmail(), type); 252 default: return super.get(property, type); 253 } 254 } 255 256 @Override /* SwaggerElement */ 257 public Contact set(String property, Object value) { 258 if (property == null) 259 return this; 260 switch (property) { 261 case "name": return name(value); 262 case "url": return url(value); 263 case "email": return email(value); 264 default: 265 super.set(property, value); 266 return this; 267 } 268 } 269 270 @Override /* SwaggerElement */ 271 public Set<String> keySet() { 272 ASet<String> s = new ASet<String>() 273 .appendIf(name != null, "name") 274 .appendIf(url != null, "url") 275 .appendIf(email != null, "email"); 276 return new MultiSet<>(s, super.keySet()); 277 } 278}