1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.apache.juneau.bean.openapi3;
18
19 import static org.apache.juneau.commons.utils.AssertionUtils.*;
20 import static org.apache.juneau.commons.utils.CollectionUtils.*;
21 import static org.apache.juneau.commons.utils.StringUtils.*;
22 import static org.apache.juneau.commons.utils.Utils.*;
23 import static org.apache.juneau.internal.ConverterUtils.*;
24
25 import java.net.*;
26 import java.util.*;
27
28 import org.apache.juneau.*;
29 import org.apache.juneau.commons.collections.*;
30
31 /**
32 * License information for the exposed API.
33 *
34 * <p>
35 * The License Object provides license information for the exposed API. This information helps clients understand
36 * the terms under which the API can be used, including any restrictions or requirements.
37 *
38 * <h5 class='section'>OpenAPI Specification:</h5>
39 * <p>
40 * The License Object is composed of the following fields:
41 * <ul class='spaced-list'>
42 * <li><c>name</c> (string, REQUIRED) - The license name used for the API
43 * <li><c>url</c> (string) - A URL to the license used for the API
44 * </ul>
45 *
46 * <h5 class='section'>Example:</h5>
47 * <p class='bcode'>
48 * <jc>// Construct using SwaggerBuilder.</jc>
49 * License <jv>x</jv> = <jsm>license</jsm>(<js>"Apache 2.0"</js>, <js>"http://www.apache.org/licenses/LICENSE-2.0.html"</js>);
50 *
51 * <jc>// Serialize using JsonSerializer.</jc>
52 * String <jv>json</jv> = Json.<jsm>from</jsm>(<jv>x</jv>);
53 *
54 * <jc>// Or just use toString() which does the same as above.</jc>
55 * String <jv>json</jv> = <jv>x</jv>.toString();
56 * </p>
57 * <p class='bcode'>
58 * <jc>// Output</jc>
59 * {
60 * <js>"name"</js>: <js>"Apache 2.0"</js>,
61 * <js>"url"</js>: <js>"http://www.apache.org/licenses/LICENSE-2.0.html"</js>
62 * }
63 * </p>
64 *
65 * <h5 class='section'>See Also:</h5><ul>
66 * <li class='link'><a class="doclink" href="https://spec.openapis.org/oas/v3.0.0#license-object">OpenAPI Specification > License Object</a>
67 * <li class='link'><a class="doclink" href="https://swagger.io/docs/specification/api-general-info/">OpenAPI API General Info</a>
68 * <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauBeanOpenApi3">juneau-bean-openapi-v3</a>
69 * </ul>
70 */
71 public class License extends OpenApiElement {
72
73 private String name;
74 private URI url;
75
76 /**
77 * Default constructor.
78 */
79 public License() {}
80
81 /**
82 * Copy constructor.
83 *
84 * @param copyFrom The object to copy.
85 */
86 public License(License copyFrom) {
87 super(copyFrom);
88
89 this.name = copyFrom.name;
90 this.url = copyFrom.url;
91 }
92
93 /**
94 * Make a deep copy of this object.
95 *
96 * @return A deep copy of this object.
97 */
98 public License copy() {
99 return new License(this);
100 }
101
102 @Override /* Overridden from OpenApiElement */
103 public <T> T get(String property, Class<T> type) {
104 assertArgNotNull("property", property);
105 return switch (property) {
106 case "name" -> toType(getName(), type);
107 case "url" -> toType(getUrl(), type);
108 default -> super.get(property, type);
109 };
110 }
111
112 /**
113 * Bean property getter: <property>name</property>.
114 *
115 * <p>
116 * The license name used for the API.
117 *
118 * @return The property value, or <jk>null</jk> if it is not set.
119 */
120 public String getName() { return name; }
121
122 /**
123 * Bean property getter: <property>url</property>.
124 *
125 * <p>
126 * A URL to the license used for the API.
127 *
128 * @return The property value, or <jk>null</jk> if it is not set.
129 */
130 public URI getUrl() { return url; }
131
132 @Override /* Overridden from OpenApiElement */
133 public Set<String> keySet() {
134 // @formatter:off
135 var s = setb(String.class)
136 .addIf(nn(name), "name")
137 .addIf(nn(url), "url")
138 .build();
139 // @formatter:on
140 return new MultiSet<>(s, super.keySet());
141 }
142
143 @Override /* Overridden from OpenApiElement */
144 public License set(String property, Object value) {
145 assertArgNotNull("property", property);
146 return switch (property) {
147 case "name" -> setName(s(value));
148 case "url" -> setUrl(toUri(value));
149 default -> {
150 super.set(property, value);
151 yield this;
152 }
153 };
154 }
155
156 /**
157 * Bean property setter: <property>name</property>.
158 *
159 * <p>
160 * The license name used for the API.
161 *
162 * @param value
163 * The new value for this property.
164 * <br>Property value is required.
165 * <br>Can be <jk>null</jk> to unset the property.
166 * @return This object
167 */
168 public License setName(String value) {
169 name = value;
170 return this;
171 }
172
173 /**
174 * Bean property setter: <property>url</property>.
175 *
176 * <p>
177 * A URL to the license used for the API.
178 *
179 * @param value
180 * The new value for this property.
181 * <br>URIs defined by {@link UriResolver} can be used for values.
182 * <br>Can be <jk>null</jk> to unset the property.
183 * @return This object
184 */
185 public License setUrl(URI value) {
186 url = value;
187 return this;
188 }
189
190 @Override /* Overridden from OpenApiElement */
191 public License strict() {
192 super.strict();
193 return this;
194 }
195
196 @Override /* Overridden from OpenApiElement */
197 public License strict(Object value) {
198 super.strict(value);
199 return this;
200 }
201 }