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