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.urlencoding.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.*;
021
022import org.apache.juneau.*;
023import org.apache.juneau.annotation.*;
024import org.apache.juneau.reflect.*;
025import org.apache.juneau.svl.*;
026
027/**
028 * Utility classes and methods for the {@link UrlEncoding @UrlEncoding} annotation.
029 *
030 * <h5 class='section'>See Also:</h5><ul>
031 *    <li class='link'><a class="doclink" href="../../../../../index.html#jm.UrlEncodingDetails">URL-Encoding Details</a>
032 * </ul>
033 */
034public class UrlEncodingAnnotation {
035
036   //-----------------------------------------------------------------------------------------------------------------
037   // Static
038   //-----------------------------------------------------------------------------------------------------------------
039
040   /** Default value */
041   public static final UrlEncoding 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    * Creates a copy of the specified annotation.
074    *
075    * @param a The annotation to copy.s
076    * @param r The var resolver for resolving any variables.
077    * @return A copy of the specified annotation.
078    */
079   public static UrlEncoding copy(UrlEncoding a, VarResolverSession r) {
080      return
081         create()
082         .expandedParams(a.expandedParams())
083         .on(r.resolve(a.on()))
084         .onClass(a.onClass())
085         .build();
086   }
087
088   //-----------------------------------------------------------------------------------------------------------------
089   // Builder
090   //-----------------------------------------------------------------------------------------------------------------
091
092   /**
093    * Builder class.
094    *
095    * <h5 class='section'>See Also:</h5><ul>
096    *    <li class='jm'>{@link org.apache.juneau.BeanContext.Builder#annotations(Annotation...)}
097    * </ul>
098    */
099   public static class Builder extends TargetedAnnotationTMFBuilder {
100
101      boolean expandedParams;
102
103      /**
104       * Constructor.
105       */
106      protected Builder() {
107         super(UrlEncoding.class);
108      }
109
110      /**
111       * Instantiates a new {@link UrlEncoding @UrlEncoding} object initialized with this builder.
112       *
113       * @return A new {@link UrlEncoding @UrlEncoding} object.
114       */
115      public UrlEncoding build() {
116         return new Impl(this);
117      }
118
119
120      /**
121       * Sets the {@link UrlEncoding#expandedParams} property on this annotation.
122       *
123       * @param value The new value for this property.
124       * @return This object.
125       */
126      public Builder expandedParams(boolean value) {
127         this.expandedParams = value;
128         return this;
129      }
130
131      // <FluentSetters>
132
133      @Override /* GENERATED - TargetedAnnotationBuilder */
134      public Builder on(String...values) {
135         super.on(values);
136         return this;
137      }
138
139      @Override /* GENERATED - TargetedAnnotationTBuilder */
140      public Builder on(java.lang.Class<?>...value) {
141         super.on(value);
142         return this;
143      }
144
145      @Override /* GENERATED - TargetedAnnotationTBuilder */
146      public Builder onClass(java.lang.Class<?>...value) {
147         super.onClass(value);
148         return this;
149      }
150
151      @Override /* GENERATED - TargetedAnnotationTMFBuilder */
152      public Builder on(Field...value) {
153         super.on(value);
154         return this;
155      }
156
157      @Override /* GENERATED - TargetedAnnotationTMFBuilder */
158      public Builder on(Method...value) {
159         super.on(value);
160         return this;
161      }
162
163      // </FluentSetters>
164   }
165
166   //-----------------------------------------------------------------------------------------------------------------
167   // Implementation
168   //-----------------------------------------------------------------------------------------------------------------
169
170   private static class Impl extends TargetedAnnotationTImpl implements UrlEncoding {
171
172      private final boolean expandedParams;
173
174      Impl(Builder b) {
175         super(b);
176         this.expandedParams = b.expandedParams;
177         postConstruct();
178      }
179
180
181      @Override /* UrlEncoding */
182      public boolean expandedParams() {
183         return expandedParams;
184      }
185   }
186
187   //-----------------------------------------------------------------------------------------------------------------
188   // Appliers
189   //-----------------------------------------------------------------------------------------------------------------
190
191   /**
192    * Applies targeted {@link UrlEncoding} annotations to a {@link org.apache.juneau.Context.Builder}.
193    */
194   public static class Apply extends AnnotationApplier<UrlEncoding,Context.Builder> {
195
196      /**
197       * Constructor.
198       *
199       * @param vr The resolver for resolving values in annotations.
200       */
201      public Apply(VarResolverSession vr) {
202         super(UrlEncoding.class, Context.Builder.class, vr);
203      }
204
205      @Override
206      public void apply(AnnotationInfo<UrlEncoding> ai, Context.Builder b) {
207         UrlEncoding a = ai.inner();
208         if (isEmptyArray(a.on(), a.onClass()))
209            return;
210         b.annotations(copy(a, vr()));
211      }
212   }
213
214   //-----------------------------------------------------------------------------------------------------------------
215   // Other
216   //-----------------------------------------------------------------------------------------------------------------
217
218   /**
219    * A collection of {@link UrlEncoding @UrlEncoding annotations}.
220    */
221   @Documented
222   @Target({METHOD,TYPE})
223   @Retention(RUNTIME)
224   @Inherited
225   public static @interface Array {
226
227      /**
228       * The child annotations.
229       *
230       * @return The annotation value.
231       */
232      UrlEncoding[] value();
233   }
234}