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.annotation;
014
015import static org.apache.juneau.internal.ArrayUtils.*;
016import static org.apache.juneau.jsonschema.SchemaUtils.*;
017
018import java.lang.annotation.*;
019import java.util.function.*;
020
021import org.apache.juneau.collections.*;
022import org.apache.juneau.common.internal.*;
023import org.apache.juneau.parser.*;
024
025/**
026 * Utility classes and methods for the {@link ExternalDocs @ExternalDocs} annotation.
027 *
028 * <h5 class='section'>See Also:</h5><ul>
029 * </ul>
030 */
031public class ExternalDocsAnnotation {
032
033   //-----------------------------------------------------------------------------------------------------------------
034   // Static
035   //-----------------------------------------------------------------------------------------------------------------
036
037   /** Default value */
038   public static final ExternalDocs DEFAULT = create().build();
039
040   /**
041    * Instantiates a new builder for this class.
042    *
043    * @return A new builder object.
044    */
045   public static Builder create() {
046      return new Builder();
047   }
048
049   /**
050    * Returns <jk>true</jk> if the specified annotation contains all default values.
051    *
052    * @param a The annotation to check.
053    * @return <jk>true</jk> if the specified annotation contains all default values.
054    */
055   public static boolean empty(ExternalDocs a) {
056      return a == null || DEFAULT.equals(a);
057   }
058
059   /**
060    * Merges the contents of the specified annotation into the specified generic map.
061    *
062    * @param m The map to copy the contents to.
063    * @param a The annotation to apply.
064    * @return The same map with the annotation contents applied.
065    * @throws ParseException Invalid JSON found in value.
066    */
067   public static JsonMap merge(JsonMap m, ExternalDocs a) throws ParseException {
068      if (ExternalDocsAnnotation.empty(a))
069         return m;
070      Predicate<String> ne = StringUtils::isNotEmpty;
071      return m
072         .appendIf(ne, "description", joinnl(a.description()))
073         .appendIf(ne, "url", a.url())
074      ;
075   }
076
077   //-----------------------------------------------------------------------------------------------------------------
078   // Builder
079   //-----------------------------------------------------------------------------------------------------------------
080
081   /**
082    * Builder class.
083    *
084    * <h5 class='section'>See Also:</h5><ul>
085    *    <li class='jm'>{@link org.apache.juneau.BeanContext.Builder#annotations(Annotation...)}
086    * </ul>
087    */
088   public static class Builder extends AnnotationBuilder {
089
090      String url="";
091      String[] description={};
092
093      /**
094       * Constructor.
095       */
096      protected Builder() {
097         super(ExternalDocs.class);
098      }
099
100      /**
101       * Instantiates a new {@link ExternalDocs @ExternalDocs} object initialized with this builder.
102       *
103       * @return A new {@link ExternalDocs @ExternalDocs} object.
104       */
105      public ExternalDocs build() {
106         return new Impl(this);
107      }
108
109      /**
110       * Sets the {@link ExternalDocs#description} property on this annotation.
111       *
112       * @param value The new value for this property.
113       * @return This object.
114       */
115      public Builder description(String...value) {
116         this.description = value;
117         return this;
118      }
119
120      /**
121       * Sets the {@link ExternalDocs#url} property on this annotation.
122       *
123       * @param value The new value for this property.
124       * @return This object.
125       */
126      public Builder url(String value) {
127         this.url = value;
128         return this;
129      }
130
131      // <FluentSetters>
132      // </FluentSetters>
133   }
134
135   //-----------------------------------------------------------------------------------------------------------------
136   // Implementation
137   //-----------------------------------------------------------------------------------------------------------------
138
139   private static class Impl extends AnnotationImpl implements ExternalDocs {
140
141      private final String url;
142      private final String[] description;
143
144      Impl(Builder b) {
145         super(b);
146         this.description = copyOf(b.description);
147         this.url = b.url;
148         postConstruct();
149      }
150
151      @Override
152      public String[] description() {
153         return description;
154      }
155
156      @Override
157      public String url() {
158         return url;
159      }
160   }
161}