001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.juneau.cp;
018
019import static org.apache.juneau.internal.CollectionUtils.*;
020
021import java.util.*;
022
023import org.apache.juneau.common.utils.*;
024
025/**
026 * A list of default settings.
027 *
028 * <p>
029 * Consists of a simple string-keyed map of arbitrary objects.
030 *
031 * <h5 class='section'>Notes:</h5><ul>
032 *    <li class='warn'>This class is not thread safe.
033 * </ul>
034 *
035 * <h5 class='section'>See Also:</h5><ul>
036 * </ul>
037 */
038public class DefaultSettingsMap {
039
040   //-----------------------------------------------------------------------------------------------------------------
041   // Static
042   //-----------------------------------------------------------------------------------------------------------------
043
044   /**
045    * Static creator.
046    *
047    * @return A new object.
048    */
049   public static DefaultSettingsMap create() {
050      return new DefaultSettingsMap();
051   }
052
053   //-----------------------------------------------------------------------------------------------------------------
054   // Instance
055   //-----------------------------------------------------------------------------------------------------------------
056
057   private final Map<String,Object> entries;
058
059   /**
060    * Constructor.
061    */
062   protected DefaultSettingsMap() {
063      entries = map();
064   }
065
066   /**
067    * Copy constructor
068    *
069    * @param value The object to copy.
070    */
071   public DefaultSettingsMap(DefaultSettingsMap value) {
072      entries = copyOf(value.entries);
073   }
074
075   /**
076    * Sets the specified setting value.
077    *
078    * @param name The setting name.
079    * @param value The setting value.
080    * @return This object.
081    */
082   public DefaultSettingsMap set(String name, Object value) {
083      entries.put(name, value);
084      return this;
085   }
086
087   /**
088    * Returns the value of the specified setting if it exists.
089    *
090    * @param <T> The return type.
091    * @param type The setting type.
092    * @param name The setting name.
093    * @return The setting value.
094    */
095   @SuppressWarnings("unchecked")
096   public <T> Optional<T> get(Class<T> type, String name) {
097      return Utils.opt((T)entries.get(name));
098   }
099
100   /**
101    * Creates a copy of this map.
102    *
103    * @return A copy of this map.
104    */
105   public DefaultSettingsMap copy() {
106      return new DefaultSettingsMap(this);
107   }
108}