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.ConverterUtils.*;
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.collections.*;
025import org.apache.juneau.internal.*;
026
027/**
028 * License information for the exposed API.
029 *
030 * <h5 class='section'>Example:</h5>
031 * <p class='bcode w800'>
032 *    <jc>// Construct using SwaggerBuilder.</jc>
033 *    License x = <jsm>license</jsm>(<js>"Apache 2.0"</js>, <js>"http://www.apache.org/licenses/LICENSE-2.0.html"</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>"Apache 2.0"</js>,
045 *       <js>"url"</js>: <js>"http://www.apache.org/licenses/LICENSE-2.0.html"</js>
046 *    }
047 * </p>
048 *
049 * <ul class='seealso'>
050 *    <li class='link'>{@doc DtoSwagger}
051 * </ul>
052 */
053@Bean(bpi="name,url,*")
054public class License extends SwaggerElement {
055
056   private String name;
057   private URI url;
058
059   /**
060    * Default constructor.
061    */
062   public License() {}
063
064   /**
065    * Copy constructor.
066    *
067    * @param copyFrom The object to copy.
068    */
069   public License(License copyFrom) {
070      super(copyFrom);
071
072      this.name = copyFrom.name;
073      this.url = copyFrom.url;
074   }
075
076   /**
077    * Make a deep copy of this object.
078    *
079    * @return A deep copy of this object.
080    */
081   public License copy() {
082      return new License(this);
083   }
084
085   /**
086    * Bean property getter:  <property>name</property>.
087    *
088    * <p>
089    * The license name used for the API.
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 license name used for the API.
102    *
103    * @param value
104    *    The new value for this property.
105    *    <br>Property value is required.
106    * @return This object (for method chaining).
107    */
108   public License setName(String value) {
109      name = value;
110      return this;
111   }
112
113   /**
114    * Same as {@link #setName(String)}.
115    *
116    * @param value
117    *    The new value for this property.
118    *    <br>Non-String values will be converted to String using <c>toString()</c>.
119    *    <br>Can be <jk>null</jk> to unset the property.
120    * @return This object (for method chaining).
121    */
122   public License name(Object value) {
123      return setName(stringify(value));
124   }
125
126   /**
127    * Bean property getter:  <property>url</property>.
128    *
129    * <p>
130    * A URL to the license used for the API.
131    *
132    * @return The property value, or <jk>null</jk> if it is not set.
133    */
134   public URI getUrl() {
135      return url;
136   }
137
138   /**
139    * Bean property setter:  <property>url</property>.
140    *
141    * <p>
142    * A URL to the license used for the API.
143    *
144    * @param value
145    *    The new value for this property.
146    *    <br>URIs defined by {@link UriResolver} can be used for values.
147    *    <br>Can be <jk>null</jk> to unset the property.
148    * @return This object (for method chaining).
149    */
150   public License setUrl(URI value) {
151      url = value;
152      return this;
153   }
154
155   /**
156    * Same as {@link #setUrl(URI)}.
157    *
158    * @param value
159    *    The new value for this property.
160    *    <br>Non-URI values will be converted to URI using <code><jk>new</jk> URI(value.toString())</code>.
161    *    <br>Valid types:
162    *    <ul>
163    *       <li>{@link URI}
164    *       <li>{@link URL}
165    *       <li>{@link String}
166    *          <br>Converted to a URI using <code>URI.<jsm>create</jsm>(value.toString())</code>
167    *    </ul>
168    *    <br>Can be <jk>null</jk> to unset the property.
169    * @return This object (for method chaining).
170    */
171   public License url(Object value) {
172      return setUrl(StringUtils.toURI(value));
173   }
174
175   /**
176    * Returns <jk>true</jk> if the name property is not null or empty.
177    *
178    * @return <jk>true</jk> if the name property is not null or empty.
179    */
180   public boolean hasName() {
181      return isNotEmpty(name);
182   }
183
184   /**
185    * Returns <jk>true</jk> if the url property is not null.
186    *
187    * @return <jk>true</jk> if the url property is not null.
188    */
189   public boolean hasUrl() {
190      return url != null;
191   }
192
193   @Override /* SwaggerElement */
194   public <T> T get(String property, Class<T> type) {
195      if (property == null)
196         return null;
197      switch (property) {
198         case "name": return toType(getName(), type);
199         case "url": return toType(getUrl(), type);
200         default: return super.get(property, type);
201      }
202   }
203
204   @Override /* SwaggerElement */
205   public License set(String property, Object value) {
206      if (property == null)
207         return this;
208      switch (property) {
209         case "name": return name(value);
210         case "url": return url(value);
211         default:
212            super.set(property, value);
213            return this;
214      }
215   }
216
217   @Override /* SwaggerElement */
218   public Set<String> keySet() {
219      ASet<String> s = ASet.<String>of()
220         .aif(name != null, "name")
221         .aif(url != null, "url");
222      return new MultiSet<>(s, super.keySet());
223   }
224}