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.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.*;
024
025import org.apache.juneau.*;
026import org.apache.juneau.reflect.*;
027import org.apache.juneau.svl.*;
028
029/**
030 * Utility classes and methods for the {@link Swap @Swap} annotation.
031 *
032 * <h5 class='section'>See Also:</h5><ul>
033 * </ul>
034 */
035public class SwapAnnotation {
036
037   //-----------------------------------------------------------------------------------------------------------------
038   // Static
039   //-----------------------------------------------------------------------------------------------------------------
040
041   /** Default value */
042   public static final Swap DEFAULT = create().build();
043
044   /**
045    * Instantiates a new builder for this class.
046    *
047    * @return A new builder object.
048    */
049   public static Builder create() {
050      return new Builder();
051   }
052
053   /**
054    * Instantiates a new builder for this class.
055    *
056    * @param on The targets this annotation applies to.
057    * @return A new builder object.
058    */
059   public static Builder create(Class<?>...on) {
060      return create().on(on);
061   }
062
063   /**
064    * Instantiates a new builder for this class.
065    *
066    * @param on The targets this annotation applies to.
067    * @return A new builder object.
068    */
069   public static Builder create(String...on) {
070      return create().on(on);
071   }
072
073   /**
074    * Creates a copy of the specified annotation.
075    *
076    * @param a The annotation to copy.s
077    * @param r The var resolver for resolving any variables.
078    * @return A copy of the specified annotation.
079    */
080   public static Swap copy(Swap a, VarResolverSession r) {
081      return
082         create()
083         .impl(a.impl())
084         .mediaTypes(r.resolve(a.mediaTypes()))
085         .on(r.resolve(a.on()))
086         .onClass(a.onClass())
087         .template(r.resolve(a.template()))
088         .value(a.value())
089         .build();
090   }
091
092   //-----------------------------------------------------------------------------------------------------------------
093   // Builder
094   //-----------------------------------------------------------------------------------------------------------------
095
096   /**
097    * Builder class.
098    *
099    * <h5 class='section'>See Also:</h5><ul>
100    *    <li class='jm'>{@link org.apache.juneau.BeanContext.Builder#annotations(Annotation...)}
101    * </ul>
102    */
103   public static class Builder extends TargetedAnnotationTMFBuilder<Builder> {
104
105      Class<?> impl=void.class, value=void.class;
106      String template="";
107      String[] mediaTypes={};
108
109      /**
110       * Constructor.
111       */
112      protected Builder() {
113         super(Swap.class);
114      }
115
116      /**
117       * Instantiates a new {@link Swap @Swap} object initialized with this builder.
118       *
119       * @return A new {@link Swap @Swap} object.
120       */
121      public Swap build() {
122         return new Impl(this);
123      }
124
125      /**
126       * Sets the {@link Swap#impl()} property on this annotation.
127       *
128       * @param value The new value for this property.
129       * @return This object.
130       */
131      public Builder impl(Class<?> value) {
132         this.impl = value;
133         return this;
134      }
135
136      /**
137       * Sets the {@link Swap#mediaTypes()} property on this annotation.
138       *
139       * @param value The new value for this property.
140       * @return This object.
141       */
142      public Builder mediaTypes(String...value) {
143         this.mediaTypes = value;
144         return this;
145      }
146
147      /**
148       * Sets the {@link Swap#template()} property on this annotation.
149       *
150       * @param value The new value for this property.
151       * @return This object.
152       */
153      public Builder template(String value) {
154         this.template = value;
155         return this;
156      }
157
158      /**
159       * Sets the {@link Swap#value()} property on this annotation.
160       *
161       * @param value The new value for this property.
162       * @return This object.
163       */
164      public Builder value(Class<?> value) {
165         this.value = value;
166         return this;
167      }
168
169   }
170
171   //-----------------------------------------------------------------------------------------------------------------
172   // Implementation
173   //-----------------------------------------------------------------------------------------------------------------
174
175   private static class Impl extends TargetedAnnotationTImpl implements Swap {
176
177      private final Class<?> impl, value;
178      private final String template;
179      private final String[] mediaTypes;
180
181      Impl(Builder b) {
182         super(b);
183         this.impl = b.impl;
184         this.mediaTypes = copyOf(b.mediaTypes);
185         this.template = b.template;
186         this.value = b.value;
187         postConstruct();
188      }
189
190      @Override /* Swap */
191      public Class<?> impl() {
192         return impl;
193      }
194
195      @Override /* Swap */
196      public String[] mediaTypes() {
197         return mediaTypes;
198      }
199
200      @Override /* Swap */
201      public String template() {
202         return template;
203      }
204
205      @Override /* Swap */
206      public Class<?> value() {
207         return value;
208      }
209   }
210
211   //-----------------------------------------------------------------------------------------------------------------
212   // Appliers
213   //-----------------------------------------------------------------------------------------------------------------
214
215   /**
216    * Applies targeted {@link Swap} annotations to a {@link org.apache.juneau.BeanContext.Builder}.
217    */
218   public static class Applier extends AnnotationApplier<Swap,BeanContext.Builder> {
219
220      /**
221       * Constructor.
222       *
223       * @param vr The resolver for resolving values in annotations.
224       */
225      public Applier(VarResolverSession vr) {
226         super(Swap.class, BeanContext.Builder.class, vr);
227      }
228
229      @Override
230      public void apply(AnnotationInfo<Swap> ai, BeanContext.Builder b) {
231         Swap a = ai.inner();
232         if (isEmptyArray(a.on(), a.onClass()))
233            return;
234         b.annotations(copy(a, vr()));
235      }
236   }
237
238   //-----------------------------------------------------------------------------------------------------------------
239   // Other
240   //-----------------------------------------------------------------------------------------------------------------
241
242   /**
243    * A collection of {@link Swap @Swap annotations}.
244    */
245   @Documented
246   @Target({METHOD,TYPE})
247   @Retention(RUNTIME)
248   @Inherited
249   public static @interface Array {
250
251      /**
252       * The child annotations.
253       *
254       * @return The annotation value.
255       */
256      Swap[] value();
257   }
258}