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.cp;
014
015import static org.apache.juneau.internal.CollectionUtils.*;
016
017import java.util.*;
018
019/**
020 * A list of default settings.
021 *
022 * <p>
023 * Consists of a simple string-keyed map of arbitrary objects.
024 *
025 * <h5 class='section'>Notes:</h5><ul>
026 *    <li class='warn'>This class is not thread safe.
027 * </ul>
028 *
029 * <h5 class='section'>See Also:</h5><ul>
030 * </ul>
031 */
032public class DefaultSettingsMap {
033
034   //-----------------------------------------------------------------------------------------------------------------
035   // Static
036   //-----------------------------------------------------------------------------------------------------------------
037
038   /**
039    * Static creator.
040    *
041    * @return A new object.
042    */
043   public static DefaultSettingsMap create() {
044      return new DefaultSettingsMap();
045   }
046
047   //-----------------------------------------------------------------------------------------------------------------
048   // Instance
049   //-----------------------------------------------------------------------------------------------------------------
050
051   private final Map<String,Object> entries;
052
053   /**
054    * Constructor.
055    */
056   protected DefaultSettingsMap() {
057      entries = map();
058   }
059
060   /**
061    * Copy constructor
062    *
063    * @param value The object to copy.
064    */
065   public DefaultSettingsMap(DefaultSettingsMap value) {
066      entries = copyOf(value.entries);
067   }
068
069   /**
070    * Sets the specified setting value.
071    *
072    * @param name The setting name.
073    * @param value The setting value.
074    * @return This object.
075    */
076   public DefaultSettingsMap set(String name, Object value) {
077      entries.put(name, value);
078      return this;
079   }
080
081   /**
082    * Returns the value of the specified setting if it exists.
083    *
084    * @param <T> The return type.
085    * @param type The setting type.
086    * @param name The setting name.
087    * @return The setting value.
088    */
089   @SuppressWarnings("unchecked")
090   public <T> Optional<T> get(Class<T> type, String name) {
091      return optional((T)entries.get(name));
092   }
093
094   /**
095    * Creates a copy of this map.
096    *
097    * @return A copy of this map.
098    */
099   public DefaultSettingsMap copy() {
100      return new DefaultSettingsMap(this);
101   }
102}