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.internal.CollectionUtils.*;
016import static org.apache.juneau.internal.ConverterUtils.*;
017
018import org.apache.juneau.annotation.*;
019import org.apache.juneau.internal.*;
020
021import java.util.*;
022
023/**
024 * TODO
025 */
026@Bean(properties="schema,example,examples,encoding,*")
027@FluentSetters
028public class MediaType extends OpenApiElement{
029   private SchemaInfo schema;
030   private Object example;
031   private Map<String,Example> examples;
032   private Map<String,Encoding> encoding;
033
034   /**
035    * Default constructor.
036    */
037   public MediaType() { }
038
039   /**
040    * Copy constructor.
041    *
042    * @param copyFrom The object to copy.
043    */
044   public MediaType(MediaType copyFrom) {
045      super(copyFrom);
046
047      this.schema = copyFrom.schema;
048      this.example = copyFrom.example;
049      if (copyFrom.examples == null)
050         this.examples = null;
051      else
052         this.examples = new LinkedHashMap<>();
053      for (Map.Entry<String,Example> e : copyFrom.examples.entrySet())
054         this.examples.put(e.getKey(), e.getValue().copy());
055
056      if (copyFrom.encoding == null)
057         this.encoding = null;
058      else
059         this.encoding = new LinkedHashMap<>();
060      for (Map.Entry<String,Encoding> e : copyFrom.encoding.entrySet())
061         this.encoding.put(e.getKey(), e.getValue().copy());
062   }
063
064   /**
065    * Make a deep copy of this object.
066    *
067    * @return A deep copy of this object.
068    */
069   public MediaType copy() {
070      return new MediaType(this);
071   }
072
073   @Override /* OpenApiElement */
074   protected MediaType strict() {
075      super.strict();
076      return this;
077   }
078
079   /**
080    * Bean property getter:  <property>schema</property>.
081    *
082    * @return The property value, or <jk>null</jk> if it is not set.
083    */
084   public SchemaInfo getSchema() {
085      return schema;
086   }
087
088   /**
089    * Bean property setter:  <property>schema</property>.
090    *
091    * @param value
092    *    The new value for this property.
093    *    <br>Can be <jk>null</jk> to unset the property.
094    * @return This object
095    */
096   public MediaType setSchema(SchemaInfo value) {
097      schema = value;
098      return this;
099   }
100
101   /**
102    * Bean property getter:  <property>x-example</property>.
103    *
104    * @return The property value, or <jk>null</jk> if it is not set.
105    */
106   @Beanp("x-example")
107   public Object getExample() {
108      return example;
109   }
110
111   /**
112    * Bean property setter:  <property>examples</property>.
113    *
114    * @param value
115    *    The new value for this property.
116    *    <br>Can be <jk>null</jk> to unset the property.
117    * @return This object
118    */
119   @Beanp("x-example")
120   public MediaType setExample(Object value) {
121      example = value;
122      return this;
123   }
124
125   /**
126    * Bean property getter:  <property>variables</property>.
127    *
128    * @return The property value, or <jk>null</jk> if it is not set.
129    */
130   public Map<String, Encoding> getEncoding() {
131      return encoding;
132   }
133
134   /**
135    * Bean property setter:  <property>variables</property>.
136    *
137    * @param value
138    *    The new value for this property.
139    * @return This object
140    */
141   public MediaType setEncoding(Map<String, Encoding> value) {
142      encoding = copyOf(value);
143      return this;
144   }
145
146   /**
147    * Adds one or more values to the <property>variables</property> property.
148    *
149    * @param key The mapping key.
150    * @param value
151    *    The values to add to this property.
152    *    <br>Ignored if <jk>null</jk>.
153    * @return This object
154    */
155   public MediaType addEncoding(String key, Encoding value) {
156      encoding = mapBuilder(encoding).sparse().add(key, value).build();
157      return this;
158   }
159
160   /**
161    * Bean property getter:  <property>examples</property>.
162    *
163    * <p>
164    * The list of possible responses as they are returned from executing this operation.
165    *
166    * @return The property value, or <jk>null</jk> if it is not set.
167    */
168   public Map<String,Example> getExamples() {
169      return examples;
170   }
171
172   /**
173    * Bean property setter:  <property>headers</property>.
174    *
175    * <p>
176    * A list of examples that are sent with the response.
177    *
178    * @param value
179    *    The new value for this property.
180    *    <br>Can be <jk>null</jk> to unset the property.
181    * @return This object
182    */
183   public MediaType setExamples(Map<String,Example> value) {
184      examples = copyOf(value);
185      return this;
186   }
187
188   /**
189    * Adds a single value to the <property>examples</property> property.
190    *
191    * @param name The example name.
192    * @param example The example.
193    * @return This object
194    */
195   public MediaType addExample(String name, Example example) {
196      examples = mapBuilder(examples).add(name, example).build();
197      return this;
198   }
199
200   // <FluentSetters>
201
202   // </FluentSetters>
203
204   @Override /* OpenApiElement */
205   public <T> T get(String property, Class<T> type) {
206      if (property == null)
207         return null;
208      switch (property) {
209         case "schema": return toType(getSchema(), type);
210         case "example": return toType(getExample(), type);
211         case "examples": return toType(getExamples(), type);
212         case "encoding": return toType(getEncoding(), type);
213         default: return super.get(property, type);
214      }
215   }
216
217   @Override /* OpenApiElement */
218   public MediaType set(String property, Object value) {
219      if (property == null)
220         return this;
221      switch (property) {
222         case "schema": return setSchema(toType(value, SchemaInfo.class));
223         case "example": return setExample(value);
224         case "examples": return setExamples(mapBuilder(String.class,Example.class).sparse().addAny(value).build());
225         case "encoding": return setEncoding(mapBuilder(String.class,Encoding.class).sparse().addAny(value).build());
226         default:
227            super.set(property, value);
228            return this;
229      }
230   }
231
232   @Override /* OpenApiElement */
233   public Set<String> keySet() {
234      Set<String> s = setBuilder(String.class)
235            .addIf(schema != null, "schema")
236            .addIf(example != null, "example")
237            .addIf(encoding != null, "encoding")
238            .addIf(examples != null, "examples")
239            .build();
240      return new MultiSet<>(s, super.keySet());
241   }
242}