001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.juneau.rest.server;
018
019import static org.apache.juneau.commons.utils.Shorts.*;
020
021import java.lang.annotation.*;
022
023import org.apache.juneau.commons.*;
024import org.apache.juneau.http.*;
025import org.apache.juneau.marshall.*;
026
027/**
028 * Utility classes and methods for the {@link Swagger @Swagger} annotation.
029 *
030 * <h5 class='section'>See Also:</h5><ul>
031 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauBeanSwagger2">juneau-bean-swagger-v2</a>
032 * </ul>
033 */
034public class SwaggerAnnotation {
035
036   /**
037    * Prevents instantiation.
038    */
039   private SwaggerAnnotation() {}
040
041   /**
042    * Builder class.
043    *
044    * <h5 class='section'>See Also:</h5><ul>
045    *    <li class='jm'>{@link org.apache.juneau.marshall.MarshallingContext.Builder#annotations(Annotation...) MarshallingContext.Builder#annotations(Annotation...)}
046    * </ul>
047    */
048   public static class Builder extends AnnotationObject.Builder {
049
050      private String[] description = {};
051      private Contact contact = ContactAnnotation.DEFAULT;
052      private ExternalDocs externalDocs = ExternalDocsAnnotation.DEFAULT;
053      private License license = LicenseAnnotation.DEFAULT;
054      private String version = "";
055      private String[] termsOfService = {};
056      private String[] title = {};
057      private String[] value = {};
058      private Tag[] tags = {};
059
060      /**
061       * Constructor.
062       */
063      protected Builder() {
064         super(Swagger.class);
065      }
066
067      /**
068       * Instantiates a new {@link Swagger @Swagger} object initialized with this builder.
069       *
070       * @return A new {@link Swagger @Swagger} object.
071       */
072      public Swagger build() {
073         return new Object(this);
074      }
075
076      /**
077       * Sets the description property on this annotation.
078       *
079       * @param value The new value for this property.
080       * @return This object.
081       */
082      public Builder description(String...value) {
083         description = value;
084         return this;
085      }
086
087      /**
088       * Sets the {@link Swagger#contact()} property on this annotation.
089       *
090       * @param value The new value for this property.
091       * @return This object.
092       */
093      public Builder contact(Contact value) {
094         contact = value;
095         return this;
096      }
097
098      /**
099       * Sets the {@link Swagger#externalDocs()} property on this annotation.
100       *
101       * @param value The new value for this property.
102       * @return This object.
103       */
104      public Builder externalDocs(ExternalDocs value) {
105         externalDocs = value;
106         return this;
107      }
108
109      /**
110       * Sets the {@link Swagger#license()} property on this annotation.
111       *
112       * @param value The new value for this property.
113       * @return This object.
114       */
115      public Builder license(License value) {
116         license = value;
117         return this;
118      }
119
120      /**
121       * Sets the {@link Swagger#tags()} property on this annotation.
122       *
123       * @param value The new value for this property.
124       * @return This object.
125       */
126      public Builder tags(Tag...value) {
127         tags = value;
128         return this;
129      }
130
131      /**
132       * Sets the {@link Swagger#termsOfService()} property on this annotation.
133       *
134       * @param value The new value for this property.
135       * @return This object.
136       */
137      public Builder termsOfService(String...value) {
138         termsOfService = value;
139         return this;
140      }
141
142      /**
143       * Sets the {@link Swagger#title()} property on this annotation.
144       *
145       * @param value The new value for this property.
146       * @return This object.
147       */
148      public Builder title(String...value) {
149         title = value;
150         return this;
151      }
152
153      /**
154       * Sets the {@link Swagger#value()} property on this annotation.
155       *
156       * @param value The new value for this property.
157       * @return This object.
158       */
159      public Builder value(String...value) {
160         this.value = value;
161         return this;
162      }
163
164      /**
165       * Sets the {@link Swagger#version()} property on this annotation.
166       *
167       * @param value The new value for this property.
168       * @return This object.
169       */
170      public Builder version(String value) {
171         version = value;
172         return this;
173      }
174
175   }
176
177   @SuppressWarnings({
178      "java:S2160" // equals() inherited from AnnotationObject compares all annotation interface methods; subclass fields are accessed via those methods
179   })
180   private static class Object extends AnnotationObject implements Swagger {
181
182      private final String[] description;
183      private final Contact contact;
184      private final ExternalDocs externalDocs;
185      private final License license;
186      private final String version;
187      private final String[] termsOfService;
188      private final String[] title;
189      private final String[] value;
190      private final Tag[] tags;
191
192      Object(SwaggerAnnotation.Builder b) {
193         super(b);
194         description = cp(b.description);
195         contact = b.contact;
196         externalDocs = b.externalDocs;
197         license = b.license;
198         tags = cp(b.tags);
199         termsOfService = cp(b.termsOfService);
200         title = cp(b.title);
201         value = cp(b.value);
202         version = b.version;
203      }
204
205      @Override /* Overridden from Swagger */
206      public Contact contact() {
207         return contact;
208      }
209
210      @Override /* Overridden from Swagger */
211      public ExternalDocs externalDocs() {
212         return externalDocs;
213      }
214
215      @Override /* Overridden from Swagger */
216      public License license() {
217         return license;
218      }
219
220      @Override /* Overridden from Swagger */
221      public Tag[] tags() {
222         return tags;
223      }
224
225      @Override /* Overridden from Swagger */
226      public String[] termsOfService() {
227         return termsOfService;
228      }
229
230      @Override /* Overridden from Swagger */
231      public String[] title() {
232         return title;
233      }
234
235      @Override /* Overridden from Swagger */
236      public String[] value() {
237         return value;
238      }
239
240      @Override /* Overridden from Swagger */
241      public String version() {
242         return version;
243      }
244
245      @Override /* Overridden from annotation */
246      public String[] description() {
247         return description;
248      }
249   }
250
251   /** Default value */
252   public static final Swagger DEFAULT = create().build();
253
254   /**
255    * Instantiates a new builder for this class.
256    *
257    * @return A new builder object.
258    */
259   public static Builder create() {
260      return new Builder();
261   }
262
263   /**
264    * Returns <jk>true</jk> if the specified annotation contains all default values.
265    *
266    * @param a The annotation to check.
267    * @return <jk>true</jk> if the specified annotation contains all default values.
268    */
269   public static boolean empty(Swagger a) {
270      return a == null || DEFAULT.equals(a);
271   }
272}