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.annotation;
018
019import static org.apache.juneau.commons.utils.CollectionUtils.*;
020
021import java.lang.annotation.*;
022
023import org.apache.juneau.annotation.*;
024import org.apache.juneau.commons.annotation.*;
025
026/**
027 * Utility classes and methods for the {@link OpSwagger @OpSwagger} annotation.
028 *
029 * <h5 class='section'>See Also:</h5><ul>
030 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauBeanSwagger2">juneau-bean-swagger-v2</a>
031 * </ul>
032 */
033public class OpSwaggerAnnotation {
034   /**
035    * Builder class.
036    *
037    * <h5 class='section'>See Also:</h5><ul>
038    *    <li class='jm'>{@link org.apache.juneau.BeanContext.Builder#annotations(Annotation...)}
039    * </ul>
040    */
041   public static class Builder extends AnnotationObject.Builder {
042
043      private String[] description = {};
044      private ExternalDocs externalDocs = ExternalDocsAnnotation.DEFAULT;
045      private String deprecated = "", operationId = "";
046      private String[] consumes = {}, parameters = {}, produces = {}, responses = {}, schemes = {}, summary = {}, tags = {}, value = {};
047
048      /**
049       * Constructor.
050       */
051      protected Builder() {
052         super(OpSwagger.class);
053      }
054
055      /**
056       * Instantiates a new {@link OpSwagger @OpSwagger} object initialized with this builder.
057       *
058       * @return A new {@link OpSwagger @OpSwagger} object.
059       */
060      public OpSwagger build() {
061         return new Object(this);
062      }
063
064      /**
065       * Sets the description property on this annotation.
066       *
067       * @param value The new value for this property.
068       * @return This object.
069       */
070      public Builder description(String...value) {
071         description = value;
072         return this;
073      }
074
075      /**
076       * Sets the {@link OpSwagger#consumes()} property on this annotation.
077       *
078       * @param value The new value for this property.
079       * @return This object.
080       */
081      public Builder consumes(String...value) {
082         consumes = value;
083         return this;
084      }
085
086      /**
087       * Sets the {@link OpSwagger#deprecated()} property on this annotation.
088       *
089       * @param value The new value for this property.
090       * @return This object.
091       */
092      public Builder deprecated(String value) {
093         deprecated = value;
094         return this;
095      }
096
097      /**
098       * Sets the {@link OpSwagger#externalDocs()} property on this annotation.
099       *
100       * @param value The new value for this property.
101       * @return This object.
102       */
103      public Builder externalDocs(ExternalDocs value) {
104         externalDocs = value;
105         return this;
106      }
107
108      /**
109       * Sets the {@link OpSwagger#operationId()} property on this annotation.
110       *
111       * @param value The new value for this property.
112       * @return This object.
113       */
114      public Builder operationId(String value) {
115         operationId = value;
116         return this;
117      }
118
119      /**
120       * Sets the {@link OpSwagger#parameters()} property on this annotation.
121       *
122       * @param value The new value for this property.
123       * @return This object.
124       */
125      public Builder parameters(String...value) {
126         parameters = value;
127         return this;
128      }
129
130      /**
131       * Sets the {@link OpSwagger#produces()} property on this annotation.
132       *
133       * @param value The new value for this property.
134       * @return This object.
135       */
136      public Builder produces(String...value) {
137         produces = value;
138         return this;
139      }
140
141      /**
142       * Sets the {@link OpSwagger#responses()} property on this annotation.
143       *
144       * @param value The new value for this property.
145       * @return This object.
146       */
147      public Builder responses(String...value) {
148         responses = value;
149         return this;
150      }
151
152      /**
153       * Sets the {@link OpSwagger#schemes()} property on this annotation.
154       *
155       * @param value The new value for this property.
156       * @return This object.
157       */
158      public Builder schemes(String...value) {
159         schemes = value;
160         return this;
161      }
162
163      /**
164       * Sets the {@link OpSwagger#summary()} property on this annotation.
165       *
166       * @param value The new value for this property.
167       * @return This object.
168       */
169      public Builder summary(String...value) {
170         summary = value;
171         return this;
172      }
173
174      /**
175       * Sets the {@link OpSwagger#tags()} property on this annotation.
176       *
177       * @param value The new value for this property.
178       * @return This object.
179       */
180      public Builder tags(String...value) {
181         tags = value;
182         return this;
183      }
184
185      /**
186       * Sets the {@link OpSwagger#value()} property on this annotation.
187       *
188       * @param value The new value for this property.
189       * @return This object.
190       */
191      public Builder value(String...value) {
192         this.value = value;
193         return this;
194      }
195
196   }
197
198   private static class Object extends AnnotationObject implements OpSwagger {
199
200      private final String[] description;
201      private final ExternalDocs externalDocs;
202      private final String deprecated, operationId;
203      private final String[] consumes, parameters, produces, responses, schemes, summary, tags, value;
204
205      Object(OpSwaggerAnnotation.Builder b) {
206         super(b);
207         description = copyOf(b.description);
208         consumes = copyOf(b.consumes);
209         deprecated = b.deprecated;
210         externalDocs = b.externalDocs;
211         operationId = b.operationId;
212         parameters = copyOf(b.parameters);
213         produces = copyOf(b.produces);
214         responses = copyOf(b.responses);
215         schemes = copyOf(b.schemes);
216         summary = copyOf(b.summary);
217         tags = copyOf(b.tags);
218         value = copyOf(b.value);
219      }
220
221      @Override /* Overridden from OpSwagger */
222      public String[] consumes() {
223         return consumes;
224      }
225
226      @Override /* Overridden from OpSwagger */
227      public String deprecated() {
228         return deprecated;
229      }
230
231      @Override /* Overridden from OpSwagger */
232      public ExternalDocs externalDocs() {
233         return externalDocs;
234      }
235
236      @Override /* Overridden from OpSwagger */
237      public String operationId() {
238         return operationId;
239      }
240
241      @Override /* Overridden from OpSwagger */
242      public String[] parameters() {
243         return parameters;
244      }
245
246      @Override /* Overridden from OpSwagger */
247      public String[] produces() {
248         return produces;
249      }
250
251      @Override /* Overridden from OpSwagger */
252      public String[] responses() {
253         return responses;
254      }
255
256      @Override /* Overridden from OpSwagger */
257      public String[] schemes() {
258         return schemes;
259      }
260
261      @Override /* Overridden from OpSwagger */
262      public String[] summary() {
263         return summary;
264      }
265
266      @Override /* Overridden from OpSwagger */
267      public String[] tags() {
268         return tags;
269      }
270
271      @Override /* Overridden from OpSwagger */
272      public String[] value() {
273         return value;
274      }
275
276      @Override /* Overridden from annotation */
277      public String[] description() {
278         return description;
279      }
280   }
281
282   /** Default value */
283   public static final OpSwagger DEFAULT = create().build();
284
285   /**
286    * Instantiates a new builder for this class.
287    *
288    * @return A new builder object.
289    */
290   public static Builder create() {
291      return new Builder();
292   }
293
294   /**
295    * Returns <jk>true</jk> if the specified annotation contains all default values.
296    *
297    * @param a The annotation to check.
298    * @return <jk>true</jk> if the specified annotation contains all default values.
299    */
300   public static boolean empty(OpSwagger a) {
301      return a == null || DEFAULT.equals(a);
302   }
303
304   /**
305    * Returns <jk>false</jk> if the specified annotation contains all default values.
306    *
307    * @param a The annotation to check.
308    * @return <jk>false</jk> if the specified annotation contains all default values.
309    */
310   public static boolean notEmpty(OpSwagger a) {
311      return ! empty(a);
312   }
313}