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.dto.openapi3;
014
015import static org.apache.juneau.common.internal.StringUtils.*;
016import static org.apache.juneau.internal.CollectionUtils.*;
017import static org.apache.juneau.internal.ConverterUtils.*;
018
019import org.apache.juneau.UriResolver;
020import org.apache.juneau.annotation.Bean;
021import org.apache.juneau.internal.*;
022
023import java.net.URI;
024import java.net.URL;
025import java.util.*;
026
027/**
028 * TODO
029 */
030@Bean(properties="url,description,variables,*")
031@FluentSetters
032public class Server extends OpenApiElement{
033   private URI url;
034   private String description;
035   private Map<String,ServerVariable> variables;
036
037   /**
038    * Default constructor.
039    */
040   public Server() { }
041
042   /**
043    * Copy constructor.
044    *
045    * @param copyFrom The object to copy.
046    */
047   public Server(Server copyFrom) {
048      super(copyFrom);
049
050      this.url = copyFrom.url;
051      this.description = copyFrom.description;
052      if (copyFrom.variables == null) {
053         this.variables = null;
054      } else {
055         this.variables = new LinkedHashMap<>();
056         for (Map.Entry<String,ServerVariable> e : copyFrom.variables.entrySet())
057            this.variables.put(e.getKey(),   e.getValue().copy());
058      }
059   }
060
061   /**
062    * Make a deep copy of this object.
063    *
064    * @return A deep copy of this object.
065    */
066   public Server copy() {
067      return new Server(this);
068   }
069
070   @Override /* OpenApiElement */
071   protected Server strict() {
072      super.strict();
073      return this;
074   }
075
076   /**
077    * Bean property getter:  <property>url</property>.
078    *
079    * <p>
080    * The URL pointing to the contact information.
081    *
082    * @return The property value, or <jk>null</jk> if it is not set.
083    */
084   public URI getUrl() {
085      return url;
086   }
087
088   /**
089    * Bean property setter:  <property>url</property>.
090    *
091    * <p>
092    * The value can be of any of the following types: {@link URI}, {@link URL}, {@link String}.
093    * <br>Strings must be valid URIs.
094    *
095    * <p>
096    * URIs defined by {@link UriResolver} can be used for values.
097    *
098    * @param value
099    *    The new value for this property.
100    *    <br>Can be <jk>null</jk> to unset the property.
101    * @return This object
102    */
103   public Server setUrl(URI value) {
104      url = value;
105      return this;
106   }
107
108   /**
109    * Bean property getter:  <property>description</property>.
110    *
111    * @return The property value, or <jk>null</jk> if it is not set.
112    */
113   public String getDescription() {
114      return description;
115   }
116
117   /**
118    * Bean property setter:  <property>description</property>.
119    *
120    * @param value
121    *    The new value for this property.
122    * @return This object
123    */
124   public Server setDescription(String value) {
125      description = value;
126      return this;
127   }
128
129   /**
130    * Bean property getter:  <property>variables</property>.
131    *
132    * @return The property value, or <jk>null</jk> if it is not set.
133    */
134   public Map<String, ServerVariable> getVariables() {
135      return variables;
136   }
137
138   /**
139    * Bean property setter:  <property>variables</property>.
140    *
141    * @param value
142    *    The new value for this property.
143    * @return This object
144    */
145   public Server setVariables(Map<String, ServerVariable> value) {
146      variables = copyOf(value);
147      return this;
148   }
149
150   /**
151    * Adds one or more values to the <property>variables</property> property.
152    *
153    * @param key The mapping key.
154    * @param value
155    *    The values to add to this property.
156    *    <br>Ignored if <jk>null</jk>.
157    * @return This object
158    */
159   public Server addVariable(String key, ServerVariable value) {
160      variables = mapBuilder(variables).sparse().add(key, value).build();
161      return this;
162   }
163
164   // <FluentSetters>
165
166   // </FluentSetters>
167
168   @Override /* OpenApiElement */
169   public <T> T get(String property, Class<T> type) {
170      if (property == null)
171         return null;
172      switch (property) {
173         case "url": return toType(getUrl(), type);
174         case "description": return toType(getDescription(), type);
175         case "variables": return toType(getVariables(), type);
176         default: return super.get(property, type);
177      }
178   }
179
180   @Override /* OpenApiElement */
181   public Server set(String property, Object value) {
182      if (property == null)
183         return this;
184      switch (property) {
185         case "url": return setUrl(toURI(value));
186         case "description": return setDescription(stringify(value));
187         case "variables": return setVariables(mapBuilder(String.class,ServerVariable.class).sparse().addAny(value).build());
188         default:
189            super.set(property, value);
190            return this;
191      }
192   }
193
194   @Override /* OpenApiElement */
195   public Set<String> keySet() {
196      Set<String> s = setBuilder(String.class)
197            .addIf(url != null, "url")
198            .addIf(description != null, "description")
199            .addIf(variables != null, "variables")
200            .build();
201      return new MultiSet<>(s, super.keySet());
202   }
203}