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.annotation;
018
019import static java.lang.annotation.ElementType.*;
020import static java.lang.annotation.RetentionPolicy.*;
021import static org.apache.juneau.commons.utils.CollectionUtils.*;
022
023import java.lang.annotation.*;
024import java.lang.reflect.*;
025import java.util.*;
026
027import org.apache.juneau.*;
028import org.apache.juneau.commons.annotation.*;
029import org.apache.juneau.commons.reflect.*;
030import org.apache.juneau.svl.*;
031
032/**
033 * Utility classes and methods for the {@link StatusCode @StatusCode} annotation.
034 *
035 */
036public class StatusCodeAnnotation {
037   /**
038    * Applies targeted {@link StatusCode} annotations to a {@link org.apache.juneau.BeanContext.Builder}.
039    */
040   public static class Applier extends AnnotationApplier<StatusCode,BeanContext.Builder> {
041
042      /**
043       * Constructor.
044       *
045       * @param vr The resolver for resolving values in annotations.
046       */
047      public Applier(VarResolverSession vr) {
048         super(StatusCode.class, BeanContext.Builder.class, vr);
049      }
050
051      @Override
052      public void apply(AnnotationInfo<StatusCode> ai, BeanContext.Builder b) {
053         StatusCode a = ai.inner();
054         if (isEmptyArray(a.on()) && isEmptyArray(a.onClass()))
055            return;
056         b.annotations(a);
057      }
058   }
059
060   /**
061    * A collection of {@link StatusCode @StatusCode annotations}.
062    */
063   @Documented
064   @Target({ METHOD, TYPE })
065   @Retention(RUNTIME)
066   @Inherited
067   public static @interface Array {
068
069      /**
070       * The child annotations.
071       *
072       * @return The annotation value.
073       */
074      StatusCode[] value();
075   }
076
077   /**
078    * Builder class.
079    *
080    * <h5 class='section'>See Also:</h5><ul>
081    *    <li class='jm'>{@link org.apache.juneau.BeanContext.Builder#annotations(Annotation...)}
082    * </ul>
083    */
084   public static class Builder extends AppliedAnnotationObject.BuilderTM {
085
086      private String[] description = {};
087      private int value[] = {};
088
089      /**
090       * Constructor.
091       */
092      protected Builder() {
093         super(StatusCode.class);
094      }
095
096      /**
097       * Instantiates a new {@link StatusCode @StatusCode} object initialized with this builder.
098       *
099       * @return A new {@link StatusCode @StatusCode} object.
100       */
101      public StatusCode build() {
102         return new Object(this);
103      }
104
105      /**
106       * Sets the description property on this annotation.
107       *
108       * @param value The new value for this property.
109       * @return This object.
110       */
111      public Builder description(String...value) {
112         description = value;
113         return this;
114      }
115
116      /**
117       * Sets the {@link StatusCode#value} property on this annotation.
118       *
119       * @param value The new value for this property.
120       * @return This object.
121       */
122      public Builder value(int...value) {
123         this.value = value;
124         return this;
125      }
126
127      @Override /* Overridden from AppliedAnnotationObject.Builder */
128      public Builder on(String...value) {
129         super.on(value);
130         return this;
131      }
132
133      @Override /* Overridden from AppliedAnnotationObject.BuilderT */
134      public Builder on(Class<?>...value) {
135         super.on(value);
136         return this;
137      }
138
139      @Override /* Overridden from AppliedOnClassAnnotationObject.Builder */
140      public Builder onClass(Class<?>...value) {
141         super.onClass(value);
142         return this;
143      }
144
145      @Override /* Overridden from AppliedAnnotationObject.BuilderM */
146      public Builder on(Method...value) {
147         super.on(value);
148         return this;
149      }
150
151      @Override /* Overridden from AppliedAnnotationObject.BuilderT */
152      public Builder on(ClassInfo...value) {
153         super.on(value);
154         return this;
155      }
156
157      @Override /* Overridden from AppliedAnnotationObject.BuilderT */
158      public Builder onClass(ClassInfo...value) {
159         super.onClass(value);
160         return this;
161      }
162
163      @Override /* Overridden from AppliedAnnotationObject.BuilderTM */
164      public Builder on(MethodInfo...value) {
165         super.on(value);
166         return this;
167      }
168
169   }
170
171   private static class Object extends AppliedOnClassAnnotationObject implements StatusCode {
172
173      private final String[] description;
174      private final int[] value;
175
176      Object(StatusCodeAnnotation.Builder b) {
177         super(b);
178         this.description = copyOf(b.description);
179         this.value = Arrays.copyOf(b.value, b.value.length);
180      }
181
182      @Override /* Overridden from Response */
183      public int[] value() {
184         return value;
185      }
186
187      @Override /* Overridden from annotation */
188      public String[] description() {
189         return description;
190      }
191   }
192
193   /** Default value */
194   public static final StatusCode DEFAULT = create().build();
195
196   /**
197    * Instantiates a new builder for this class.
198    *
199    * @return A new builder object.
200    */
201   public static Builder create() {
202      return new Builder();
203   }
204
205   /**
206    * Instantiates a new builder for this class.
207    *
208    * @param on The targets this annotation applies to.
209    * @return A new builder object.
210    */
211   public static Builder create(Class<?>...on) {
212      return create().on(on);
213   }
214
215   /**
216    * Instantiates a new builder for this class.
217    *
218    * @param on The targets this annotation applies to.
219    * @return A new builder object.
220    */
221   public static Builder create(String...on) {
222      return create().on(on);
223   }
224}