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