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.annotation.Bean;
020import org.apache.juneau.internal.*;
021
022import java.util.Collection;
023import java.util.List;
024import java.util.Set;
025
026/**
027 * TODO
028 */
029@Bean(properties="enum,default,description,*")
030@FluentSetters
031public class ServerVariable extends OpenApiElement {
032
033   private List<Object> _enum;
034   private String _default;
035   private String description;
036
037   /**
038    * Default constructor.
039    */
040   public ServerVariable() {}
041
042   /**
043    * Copy constructor.
044    *
045    * @param copyFrom The object to copy.
046    */
047   public ServerVariable(ServerVariable copyFrom) {
048      super(copyFrom);
049
050      this._enum = copyOf(copyFrom._enum);
051      this._default = copyFrom._default;
052      this.description = copyFrom.description;
053   }
054
055   /**
056    * Make a deep copy of this object.
057    * @return A deep copy of this object.
058    */
059   public ServerVariable copy() {
060      return new ServerVariable(this);
061   }
062
063   @Override /* OpenApiElement */
064   protected ServerVariable strict() {
065      super.strict();
066      return this;
067   }
068
069   /**
070    * Bean property getter:  <property>enum</property>.
071    *
072    * @return The property value, or <jk>null</jk> if it is not set.
073    */
074   public List<Object> getEnum() {
075      return _enum;
076   }
077
078   /**
079    * Bean property setter:  <property>enum</property>.
080    *
081    * @param value
082    *    The new value for this property.
083    *    <br>Can be <jk>null</jk> to unset the property.
084    * @return This object
085    */
086   public ServerVariable setEnum(Collection<Object> value) {
087      _enum = listFrom(value);
088      return this;
089   }
090
091   /**
092    * Adds one or more values to the <property>enum</property> property.
093    *
094    * @param values
095    *    The values to add to this property.
096    *    <br>Valid types:
097    *    <ul>
098    *       <li><code>Object</code>
099    *       <li><code>Collection&lt;Object&gt;</code>
100    *       <li><code>String</code> - JSON array representation of <code>Collection&lt;Object&gt;</code>
101    *          <h5 class='figure'>Example:</h5>
102    *          <p class='bcode'>
103    *    _enum(<js>"['foo','bar']"</js>);
104    *          </p>
105    *       <li><code>String</code> - Individual values
106    *          <h5 class='figure'>Example:</h5>
107    *          <p class='bcode'>
108    *    _enum(<js>"foo"</js>, <js>"bar"</js>);
109    *          </p>
110    *    </ul>
111    *    <br>Ignored if <jk>null</jk>.
112    * @return This object
113    */
114   public ServerVariable addEnum(Object...values) {
115      _enum = listBuilder(_enum).sparse().addAny(values).build();
116      return this;
117   }
118
119   /**
120    * Bean property getter:  <property>default</property>.
121    *
122    * <p>
123    * Declares the value of the item that the server will use if none is provided.
124    *
125    * <h5 class='section'>Notes:</h5>
126    * <ul class='spaced-list'>
127    *    <li>
128    *       <js>"default"</js> has no meaning for required items.
129    *    <li>
130    *       Unlike JSON Schema this value MUST conform to the defined <code>type</code> for the data type.
131    * </ul>
132    *
133    * @return The property value, or <jk>null</jk> if it is not set.
134    */
135   public String getDefault() {
136      return _default;
137   }
138
139   /**
140    * Bean property setter:  <property>default</property>.
141    *
142    * <p>
143    * Declares the value of the item that the server will use if none is provided.
144    *
145    * <h5 class='section'>Notes:</h5>
146    * <ul class='spaced-list'>
147    *    <li>
148    *       <js>"default"</js> has no meaning for required items.
149    *    <li>
150    *       Unlike JSON Schema this value MUST conform to the defined <code>type</code> for the data type.
151    * </ul>
152    *
153    * @param value
154    *    The new value for this property.
155    *    <br>Can be <jk>null</jk> to unset the property.
156    * @return This object
157    */
158   public ServerVariable setDefault(String value) {
159      _default = value;
160      return this;
161   }
162
163   /**
164    * Bean property getter:  <property>description</property>.
165    *
166    * <p>
167    * Declares the value of the item that the server will use if none is provided.
168    *
169    * <h5 class='section'>Notes:</h5>
170    * <ul class='spaced-list'>
171    *    <li>
172    *       <js>"description"</js> has no meaning for required items.
173    *    <li>
174    *       Unlike JSON Schema this value MUST conform to the defined <code>type</code> for the data type.
175    * </ul>
176    *
177    * @return The property value, or <jk>null</jk> if it is not set.
178    */
179   public String getDescription() {
180      return description;
181   }
182
183   /**
184    * Bean property setter:  <property>description</property>.
185    *
186    * <p>
187    * Declares the value of the item that the server will use if none is provided.
188    *
189    * <h5 class='section'>Notes:</h5>
190    * <ul class='spaced-list'>
191    *    <li>
192    *       <js>"description"</js> has no meaning for required items.
193    *    <li>
194    *       Unlike JSON Schema this value MUST conform to the defined <code>type</code> for the data type.
195    * </ul>
196    *
197    * @param value
198    *    The new value for this property.
199    * @return This object
200    */
201   public ServerVariable setDescription(String value) {
202      description = value;
203      return this;
204   }
205
206   // <FluentSetters>
207
208   // </FluentSetters>
209
210   @Override /* OpenApiElement */
211   public <T> T get(String property, Class<T> type) {
212      if (property == null)
213         return null;
214      switch (property) {
215         case "enum": return toType(getEnum(), type);
216         case "default": return toType(getDefault(), type);
217         case "description": return toType(getDescription(), type);
218         default: return super.get(property, type);
219      }
220   }
221
222   @Override /* OpenApiElement */
223   public ServerVariable set(String property, Object value) {
224      if (property == null)
225         return this;
226      switch (property) {
227         case "default": return setDefault(stringify(value));
228         case "enum": return setEnum(listBuilder(Object.class).sparse().addAny(value).build());
229         case "description": return setDescription(stringify(value));
230         default:
231            super.set(property, value);
232            return this;
233      }
234   }
235
236   @Override /* OpenApiElement */
237   public Set<String> keySet() {
238      Set<String> s = setBuilder(String.class)
239            .addIf(_enum != null, "enum")
240            .addIf(_default != null,"default" )
241            .addIf(description != null, "description")
242            .build();
243      return new MultiSet<>(s, super.keySet());
244
245
246   }
247}