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