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;
014
015import org.apache.juneau.json.*;
016
017/**
018 * Runtime arguments common to all bean, serializer, and parser sessions.
019 */
020public class SessionArgs {
021
022   /**
023    * Default empty session arguments.
024    */
025   public static final SessionArgs DEFAULT = new SessionArgs();
026
027   ObjectMap properties;
028
029   /**
030    * Constructor.
031    */
032   public SessionArgs() {}
033
034   //-----------------------------------------------------------------------------------------------------------------
035   // Properties.
036   //-----------------------------------------------------------------------------------------------------------------
037
038   /**
039    * Session-level properties.
040    *
041    * <p>
042    * Overrides context-level properties.
043    *
044    * @param value
045    *    The new value for this property.
046    *    <br>Can be <jk>null</jk>.
047    * @return This object (for method chaining).
048    */
049   public SessionArgs properties(ObjectMap value) {
050      this.properties = value;
051      return this;
052   }
053
054   /**
055    * Adds a property to this session.
056    *
057    * @param key The property key.
058    * @param value The property value.
059    * @return This object (for method chaining).
060    */
061   public SessionArgs property(String key, Object value) {
062      if (value == null) {
063         if (properties != null)
064            properties.remove(key);
065      } else {
066         if (properties == null)
067            properties = new ObjectMap();
068         properties.put(key, value);
069      }
070      return this;
071   }
072
073   /**
074    * Returns a property on this session.
075    *
076    * @param key The property key.
077    * @return The property value, or <jk>null</jk> if not set.
078    */
079   public Object getProperty(String key) {
080      if (properties != null)
081         return properties.get(key);
082      return null;
083   }
084
085   //-----------------------------------------------------------------------------------------------------------------
086   // Other methods
087   //-----------------------------------------------------------------------------------------------------------------
088
089   /**
090    * Returns the properties defined on this object as a simple map for debugging purposes.
091    *
092    * @return A new map containing the properties defined on this object.
093    */
094   public ObjectMap toMap() {
095      return new DefaultFilteringObjectMap()
096         .append("SessionArgs", new DefaultFilteringObjectMap()
097            .append("properties", properties)
098         );
099   }
100
101   @Override /* Object */
102   public String toString() {
103      return SimpleJsonSerializer.DEFAULT_READABLE.toString(toMap());
104   }
105}