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