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.util.LinkedHashMap;
024import java.util.Map;
025import java.util.Set;
026
027/**
028 * Used to aid in serialization, deserialization, and validation.
029 */
030@Bean(properties="propertyName,mapping,*")
031@FluentSetters
032public class Discriminator extends OpenApiElement {
033
034   private String propertyName;
035   private Map<String,String> mapping;
036
037   /**
038    * Default constructor.
039    */
040   public Discriminator() {}
041
042   /**
043    * Copy constructor.
044    *
045    * @param copyFrom The object to copy.
046    */
047   public Discriminator(Discriminator copyFrom) {
048      super(copyFrom);
049
050      this.propertyName = copyFrom.propertyName;
051      if (copyFrom.mapping == null)
052         this.mapping = null;
053      else
054         this.mapping = new LinkedHashMap<>(copyFrom.mapping);
055   }
056
057   /**
058    * Make a deep copy of this object.
059    *
060    * @return A deep copy of this object.
061    */
062   public Discriminator copy() {
063      return new Discriminator(this);
064   }
065
066   /**
067    * Bean property getter:  <property>propertyName</property>.
068    *
069    * <p>
070    * A short description of the target documentation.
071    *
072    * @return The property value, or <jk>null</jk> if it is not set.
073    */
074   public String getPropertyName() {
075      return propertyName;
076   }
077
078   /**
079    * Bean property setter:  <property>propertyName</property>.
080    *
081    * <p>
082    * A short description of the target documentation.
083    *
084    * @param value
085    *    The new value for this property.
086    *    <br>Can be <jk>null</jk> to unset the property.
087    * @return This object
088    */
089   public Discriminator setPropertyName(String value) {
090      propertyName = value;
091      return this;
092   }
093
094   /**
095    * Bean property getter:  <property>mapping</property>.
096    *
097    * <p>
098    * The URL for the target documentation.
099    *
100    * @return The property value, or <jk>null</jk> if it is not set.
101    */
102   public Map<String,String> getMapping() {
103      return mapping;
104   }
105
106   /**
107    * Bean property setter:  <property>mapping</property>.
108    *
109    * <p>
110    * The URL for the target documentation.
111    *
112    * @param value
113    *    The new value for this property.
114    *    <br>Property value is required.
115    *    <br>URIs defined by {@link UriResolver} can be used for values.
116    * @return This object
117    */
118   public Discriminator setMapping(Map<String,String> value) {
119      mapping = copyOf(value);
120      return this;
121   }
122
123   /**
124    * Adds one or more values to the <property>mapping</property> property.
125    *
126    * @param key The key.
127    * @param value The value.
128    * @return This object
129    */
130   public Discriminator addMapping(String key, String value) {
131      mapping = mapBuilder(mapping).sparse().add(key, value).build();
132      return this;
133   }
134
135   // <FluentSetters>
136
137   // </FluentSetters>
138
139   @Override /* OpenApiElement */
140   public <T> T get(String property, Class<T> type) {
141      if (property == null)
142         return null;
143      switch (property) {
144         case "propertyName": return toType(getPropertyName(), type);
145         case "mapping": return toType(getMapping(), type);
146         default: return super.get(property, type);
147      }
148   }
149
150   @Override /* OpenApiElement */
151   public Discriminator set(String property, Object value) {
152      if (property == null)
153         return this;
154      switch (property) {
155         case "propertyName": return setPropertyName(stringify(value));
156         case "mapping": return setMapping(mapBuilder(String.class,String.class).sparse().addAny(value).build());
157         default:
158            super.set(property, value);
159            return this;
160      }
161   }
162
163   @Override /* OpenApiElement */
164   public Set<String> keySet() {
165      Set<String> s = setBuilder(String.class)
166         .addIf(propertyName != null, "propertyName")
167         .addIf(mapping != null, "mapping")
168         .build();
169      return new MultiSet<>(s, super.keySet());
170   }
171}