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