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.http;
018
019import static org.apache.juneau.commons.utils.Shorts.*;
020
021import java.lang.annotation.*;
022
023import org.apache.juneau.commons.*;
024import org.apache.juneau.marshall.*;
025
026/**
027 * Utility classes and methods for the {@link Response @Response} annotation.
028 *
029 */
030public class ResponseAnnotation {
031
032   /**
033    * Prevents instantiation.
034    */
035   private ResponseAnnotation() {}
036
037   /**
038    * Builder class.
039    *
040    * <h5 class='section'>See Also:</h5><ul>
041    *    <li class='jm'>{@link org.apache.juneau.marshall.MarshallingContext.Builder#annotations(Annotation...) MarshallingContext.Builder#annotations(Annotation...)}
042    * </ul>
043    */
044   public static class Builder extends AnnotationObject.Builder {
045
046      private String[] description = {};
047      private Header[] headers = {};
048      private Schema schema = SchemaAnnotation.DEFAULT;
049      private String summary = "";
050      private String[] examples = {};
051
052      /**
053       * Constructor.
054       */
055      protected Builder() {
056         super(Response.class);
057      }
058
059      /**
060       * Instantiates a new {@link Response @Response} object initialized with this builder.
061       *
062       * @return A new {@link Response @Response} object.
063       */
064      public Response build() {
065         return new Instance(this);
066      }
067
068      /**
069       * Sets the description property on this annotation.
070       *
071       * @param value The new value for this property.
072       * @return This object.
073       */
074      public Builder description(String...value) {
075         description = value;
076         return this;
077      }
078
079      /**
080       * Sets the {@link Response#examples} property on this annotation.
081       *
082       * @param value The new value for this property.
083       * @return This object.
084       */
085      public Builder examples(String...value) {
086         examples = value;
087         return this;
088      }
089
090      /**
091       * Sets the {@link Response#headers} property on this annotation.
092       *
093       * @param value The new value for this property.
094       * @return This object.
095       */
096      public Builder headers(Header...value) {
097         headers = value;
098         return this;
099      }
100
101      /**
102       * Sets the {@link Response#schema} property on this annotation.
103       *
104       * @param value The new value for this property.
105       * @return This object.
106       */
107      public Builder schema(Schema value) {
108         schema = value;
109         return this;
110      }
111
112      /**
113       * Sets the {@link Response#summary()} property on this annotation.
114       *
115       * @param value The new value for this property.
116       * @return This object.
117       * @since 10.0.0
118       */
119      public Builder summary(String value) {
120         summary = value;
121         return this;
122      }
123
124   }
125
126   @SuppressWarnings({
127      "ClassExplicitlyAnnotation", // IntelliJ / SonarLint: Instance implements @Response (AnnotationObject runtime proxy pattern)
128      "all", // Eclipse JDT: AnnotationTypeUsedAsSuperInterface has no dedicated @SuppressWarnings token (JLS 9.6)
129      "java:S2160" // equals() inherited from AnnotationObject compares all annotation interface methods; subclass fields are accessed via those methods
130   })
131   private static class Instance extends AnnotationObject implements Response {
132
133      private final String[] description;
134      private final Header[] headers;
135      private final Schema schema;
136      private final String summary;
137      private final String[] examples;
138
139      Instance(ResponseAnnotation.Builder b) {
140         super(b);
141         description = cp(b.description);
142         examples = cp(b.examples);
143         headers = cp(b.headers);
144         schema = b.schema;
145         summary = b.summary;
146      }
147
148      @Override /* Overridden from Response */
149      public String[] examples() {
150         return cp(examples);
151      }
152
153      @Override /* Overridden from Response */
154      public Header[] headers() {
155         return cp(headers);
156      }
157
158      @Override /* Overridden from Response */
159      public Schema schema() {
160         return schema;
161      }
162
163      @Override /* Overridden from Response */
164      public String summary() {
165         return summary;
166      }
167
168      @Override /* Overridden from annotation */
169      public String[] description() {
170         return cp(description);
171      }
172   }
173
174   /** Default value */
175   public static final Response DEFAULT = create().build();
176
177   /**
178    * Instantiates a new builder for this class.
179    *
180    * @return A new builder object.
181    */
182   public static Builder create() {
183      return new Builder();
184   }
185
186   /**
187    * Returns <jk>true</jk> if the specified annotation contains all default values.
188    *
189    * @param a The annotation to check.
190    * @return <jk>true</jk> if the specified annotation contains all default values.
191    */
192   public static boolean empty(Response a) {
193      return a == null || DEFAULT.equals(a);
194   }
195}