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.util.Set;
025
026/**
027 * License information for the exposed API.
028 *
029 * <h5 class='section'>Example:</h5>
030 * <p class='bcode'>
031 *    <jc>// Construct using SwaggerBuilder.</jc>
032 *    License x = <jsm>license</jsm>(<js>"Apache 2.0"</js>, <js>"http://www.apache.org/licenses/LICENSE-2.0.html"</js>);
033 *
034 *    <jc>// Serialize using JsonSerializer.</jc>
035 *    String json = JsonSerializer.<jsf>DEFAULT</jsf>.toString(x);
036 *
037 *    <jc>// Or just use toString() which does the same as above.</jc>
038 *    String json = x.toString();
039 * </p>
040 * <p class='bcode'>
041 *    <jc>// Output</jc>
042 *    {
043 *       <js>"name"</js>: <js>"Apache 2.0"</js>,
044 *       <js>"url"</js>: <js>"http://www.apache.org/licenses/LICENSE-2.0.html"</js>
045 *    }
046 * </p>
047 */
048@Bean(properties="name,url,*")
049@FluentSetters
050public class License extends OpenApiElement {
051
052   private String name;
053   private URI url;
054
055   /**
056    * Default constructor.
057    */
058   public License() {}
059
060   /**
061    * Copy constructor.
062    *
063    * @param copyFrom The object to copy.
064    */
065   public License(License copyFrom) {
066      super(copyFrom);
067
068      this.name = copyFrom.name;
069      this.url = copyFrom.url;
070   }
071
072   /**
073    * Make a deep copy of this object.
074    *
075    * @return A deep copy of this object.
076    */
077   public License copy() {
078      return new License(this);
079   }
080
081   /**
082    * Bean property getter:  <property>name</property>.
083    *
084    * <p>
085    * The license name used for the API.
086    *
087    * @return The property value, or <jk>null</jk> if it is not set.
088    */
089   public String getName() {
090      return name;
091   }
092
093   /**
094    * Bean property setter:  <property>name</property>.
095    *
096    * <p>
097    * The license name used for the API.
098    *
099    * @param value
100    *    The new value for this property.
101    *    <br>Property value is required.
102    * @return This object
103    */
104   public License setName(String value) {
105      name = value;
106      return this;
107   }
108
109   /**
110    * Bean property getter:  <property>url</property>.
111    *
112    * <p>
113    * A URL to the license used for the API.
114    *
115    * @return The property value, or <jk>null</jk> if it is not set.
116    */
117   public URI getUrl() {
118      return url;
119   }
120
121   /**
122    * Bean property setter:  <property>url</property>.
123    *
124    * <p>
125    * A URL to the license used for the API.
126    *
127    * @param value
128    *    The new value for this property.
129    *    <br>URIs defined by {@link UriResolver} can be used for values.
130    *    <br>Can be <jk>null</jk> to unset the property.
131    * @return This object
132    */
133   public License setUrl(URI value) {
134      url = value;
135      return this;
136   }
137
138   // <FluentSetters>
139
140   // </FluentSetters>
141
142   @Override /* OpenApiElement */
143   public <T> T get(String property, Class<T> type) {
144      if (property == null)
145         return null;
146      switch (property) {
147         case "name": return toType(getName(), type);
148         case "url": return toType(getUrl(), type);
149         default: return super.get(property, type);
150      }
151   }
152
153   @Override /* OpenApiElement */
154   public License set(String property, Object value) {
155      if (property == null)
156         return this;
157      switch (property) {
158         case "name": return setName(stringify(value));
159         case "url": return setUrl(toURI(value));
160         default:
161            super.set(property, value);
162            return this;
163      }
164   }
165
166   @Override /* OpenApiElement */
167   public Set<String> keySet() {
168      Set<String> s = setBuilder(String.class)
169         .addIf(name != null, "name")
170         .addIf(url != null, "url")
171         .build();
172      return new MultiSet<>(s, super.keySet());
173   }
174}