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