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