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