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.*;
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 Content @Content} annotation.
032 *
033 * <h5 class='section'>See Also:</h5><ul>
034 * </ul>
035 */
036public class ContentAnnotation {
037
038   //-----------------------------------------------------------------------------------------------------------------
039   // Static
040   //-----------------------------------------------------------------------------------------------------------------
041
042   /** Default value */
043   public static final Content 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    * Returns <jk>true</jk> if the specified annotation contains all default values.
076    *
077    * @param a The annotation to check.
078    * @return <jk>true</jk> if the specified annotation contains all default values.
079    */
080   public static boolean empty(Content a) {
081      return a == null || DEFAULT.equals(a);
082   }
083
084   //-----------------------------------------------------------------------------------------------------------------
085   // Builder
086   //-----------------------------------------------------------------------------------------------------------------
087
088   /**
089    * Builder class.
090    *
091    * <h5 class='section'>See Also:</h5><ul>
092    *    <li class='jm'>{@link org.apache.juneau.BeanContext.Builder#annotations(Annotation...)}
093    * </ul>
094    */
095   public static class Builder extends TargetedAnnotationTMBuilder<Builder> {
096
097      Schema schema = SchemaAnnotation.DEFAULT;
098
099      /**
100       * Constructor.
101       */
102      protected Builder() {
103         super(Content.class);
104      }
105
106      /**
107       * Instantiates a new {@link Content @Content} object initialized with this builder.
108       *
109       * @return A new {@link Content @Content} object.
110       */
111      public Content build() {
112         return new Impl(this);
113      }
114
115      /**
116       * Sets the {@link Content#schema} property on this annotation.
117       *
118       * @param value The new value for this property.
119       * @return This object.
120       */
121      public Builder schema(Schema value) {
122         this.schema = value;
123         return this;
124      }
125
126   }
127
128   //-----------------------------------------------------------------------------------------------------------------
129   // Implementation
130   //-----------------------------------------------------------------------------------------------------------------
131
132   private static class Impl extends TargetedAnnotationTImpl implements Content {
133
134      private final Schema schema;
135
136      Impl(Builder b) {
137         super(b);
138         this.schema = b.schema;
139         postConstruct();
140      }
141
142      @Override /* Body */
143      public Schema schema() {
144         return schema;
145      }
146   }
147
148   //-----------------------------------------------------------------------------------------------------------------
149   // Appliers
150   //-----------------------------------------------------------------------------------------------------------------
151
152   /**
153    * Applies targeted {@link Content} annotations to a {@link org.apache.juneau.BeanContext.Builder}.
154    */
155   public static class Applier extends AnnotationApplier<Content,BeanContext.Builder> {
156
157      /**
158       * Constructor.
159       *
160       * @param vr The resolver for resolving values in annotations.
161       */
162      public Applier(VarResolverSession vr) {
163         super(Content.class, BeanContext.Builder.class, vr);
164      }
165
166      @Override
167      public void apply(AnnotationInfo<Content> ai, BeanContext.Builder b) {
168         Content a = ai.inner();
169         if (isEmptyArray(a.on(), a.onClass()))
170            return;
171         b.annotations(a);
172      }
173   }
174
175   //-----------------------------------------------------------------------------------------------------------------
176   // Other
177   //-----------------------------------------------------------------------------------------------------------------
178
179   /**
180    * A collection of {@link Content @Content annotations}.
181    */
182   @Documented
183   @Target({METHOD,TYPE})
184   @Retention(RUNTIME)
185   @Inherited
186   public static @interface Array {
187
188      /**
189       * The child annotations.
190       *
191       * @return The annotation value.
192       */
193      Content[] value();
194   }
195}