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.marshall;
018
019import static java.lang.annotation.ElementType.*;
020import static java.lang.annotation.RetentionPolicy.*;
021import static org.apache.juneau.commons.utils.Shorts.*;
022
023import java.lang.annotation.*;
024
025import org.apache.juneau.commons.*;
026
027/**
028 * Utility classes and methods for the {@link Swap @Swap} annotation.
029 *
030 */
031public class SwapAnnotation {
032
033   /**
034    * Prevents instantiation.
035    */
036   private SwapAnnotation() {}
037
038   /**
039    * A collection of {@link Swap @Swap annotations}.
040    */
041   @Documented
042   @Target({ TYPE, ANNOTATION_TYPE, FIELD, METHOD })
043   @Retention(RUNTIME)
044   @Inherited
045   public static @interface Array {
046
047      /**
048       * The child annotations.
049       *
050       * @return The annotation value.
051       */
052      Swap[] value();
053   }
054
055   /**
056    * Builder class.
057    *
058    * <h5 class='section'>See Also:</h5><ul>
059    *    <li class='jm'>{@link MarshallingContext.Builder#annotations(Annotation...)}
060    * </ul>
061    */
062   public static class Builder extends AnnotationObject.Builder {
063
064      private String[] description = {};
065      private Class<?> impl = void.class;
066      private Class<?> value = void.class;
067      private String template = "";
068      private String[] mediaTypes = {};
069
070      /**
071       * Constructor.
072       */
073      protected Builder() {
074         super(Swap.class);
075      }
076
077      /**
078       * Instantiates a new {@link Swap @Swap} object initialized with this builder.
079       *
080       * @return A new {@link Swap @Swap} object.
081       */
082      public Swap build() {
083         return new Object(this);
084      }
085
086      /**
087       * Sets the description property on this annotation.
088       *
089       * @param value The new value for this property.
090       * @return This object.
091       */
092      public Builder description(String...value) {
093         description = value;
094         return this;
095      }
096
097      /**
098       * Sets the {@link Swap#impl()} property on this annotation.
099       *
100       * @param value The new value for this property.
101       * @return This object.
102       */
103      public Builder impl(Class<?> value) {
104         impl = value;
105         return this;
106      }
107
108      /**
109       * Sets the {@link Swap#mediaTypes()} property on this annotation.
110       *
111       * @param value The new value for this property.
112       * @return This object.
113       */
114      public Builder mediaTypes(String...value) {
115         mediaTypes = value;
116         return this;
117      }
118
119      /**
120       * Sets the {@link Swap#template()} property on this annotation.
121       *
122       * @param value The new value for this property.
123       * @return This object.
124       */
125      public Builder template(String value) {
126         template = value;
127         return this;
128      }
129
130      /**
131       * Sets the {@link Swap#value()} property on this annotation.
132       *
133       * @param value The new value for this property.
134       * @return This object.
135       */
136      public Builder value(Class<?> value) {
137         this.value = value;
138         return this;
139      }
140
141   }
142
143   @SuppressWarnings({
144      "java:S2160" // equals not needed; annotation object identity is sufficient for usage in Sets/Maps
145   })
146   private static class Object extends AnnotationObject implements Swap {
147
148      private final String[] description;
149      private final Class<?> impl;
150      private final Class<?> value;
151      private final String template;
152      private final String[] mediaTypes;
153
154      Object(SwapAnnotation.Builder b) {
155         super(b);
156         description = cp(b.description);
157         impl = b.impl;
158         mediaTypes = cp(b.mediaTypes);
159         template = b.template;
160         value = b.value;
161      }
162
163      @Override /* Overridden from Swap */
164      public Class<?> impl() {
165         return impl;
166      }
167
168      @Override /* Overridden from Swap */
169      public String[] mediaTypes() {
170         return mediaTypes;
171      }
172
173      @Override /* Overridden from Swap */
174      public String template() {
175         return template;
176      }
177
178      @Override /* Overridden from Swap */
179      public Class<?> value() {
180         return value;
181      }
182
183      @Override /* Overridden from annotation */
184      public String[] description() {
185         return description;
186      }
187   }
188
189   /** Default value */
190   public static final Swap DEFAULT = create().build();
191
192   /**
193    * Instantiates a new builder for this class.
194    *
195    * @return A new builder object.
196    */
197   public static Builder create() {
198      return new Builder();
199   }
200}