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 */ 017 018package org.apache.commons.configuration2; 019 020import java.util.Collection; 021import java.util.Map; 022 023import org.apache.commons.configuration2.interpol.ConfigurationInterpolator; 024import org.apache.commons.configuration2.interpol.Lookup; 025import org.apache.commons.configuration2.sync.SynchronizerSupport; 026 027 028/** 029 * <p>The main Configuration interface.</p> 030 * <p>This interface allows accessing and manipulating a configuration object. 031 * The major part of the methods defined in this interface deals with accessing 032 * properties of various data types. There is a generic {@code getProperty()} 033 * method, which returns the value of the queried property in its raw data 034 * type. Other getter methods try to convert this raw data type into a specific 035 * data type. If this fails, a {@code ConversionException} will be thrown.</p> 036 * <p>For most of the property getter methods an overloaded version exists that 037 * allows to specify a default value, which will be returned if the queried 038 * property cannot be found in the configuration. The behavior of the methods 039 * that do not take a default value in case of a missing property is not defined 040 * by this interface and depends on a concrete implementation. E.g. the 041 * {@link AbstractConfiguration} class, which is the base class 042 * of most configuration implementations provided by this package, per default 043 * returns <b>null</b> if a property is not found, but provides the 044 * {@link AbstractConfiguration#setThrowExceptionOnMissing(boolean) 045 * setThrowExceptionOnMissing()} 046 * method, with which it can be configured to throw a {@code NoSuchElementException} 047 * exception in that case. (Note that getter methods for primitive types in 048 * {@code AbstractConfiguration} always throw an exception for missing 049 * properties because there is no way of overloading the return value.)</p> 050 * <p>With the {@code addProperty()} and {@code setProperty()} methods 051 * new properties can be added to a configuration or the values of properties 052 * can be changed. With {@code clearProperty()} a property can be removed. 053 * Other methods allow to iterate over the contained properties or to create 054 * a subset configuration.</p> 055 * 056 */ 057public interface Configuration extends ImmutableConfiguration, SynchronizerSupport 058{ 059 /** 060 * Return a decorator Configuration containing every key from the current 061 * Configuration that starts with the specified prefix. The prefix is 062 * removed from the keys in the subset. For example, if the configuration 063 * contains the following properties: 064 * 065 * <pre> 066 * prefix.number = 1 067 * prefix.string = Apache 068 * prefixed.foo = bar 069 * prefix = Jakarta</pre> 070 * 071 * the Configuration returned by {@code subset("prefix")} will contain 072 * the properties: 073 * 074 * <pre> 075 * number = 1 076 * string = Apache 077 * = Jakarta</pre> 078 * 079 * (The key for the value "Jakarta" is an empty string) 080 * <p> 081 * Since the subset is a decorator and not a modified copy of the initial 082 * Configuration, any change made to the subset is available to the 083 * Configuration, and reciprocally. 084 * 085 * @param prefix The prefix used to select the properties. 086 * @return a subset configuration 087 * 088 * @see SubsetConfiguration 089 */ 090 Configuration subset(String prefix); 091 092 /** 093 * Add a property to the configuration. If it already exists then the value 094 * stated here will be added to the configuration entry. For example, if 095 * the property: 096 * 097 * <pre>resource.loader = file</pre> 098 * 099 * is already present in the configuration and you call 100 * 101 * <pre>addProperty("resource.loader", "classpath")</pre> 102 * 103 * Then you will end up with a List like the following: 104 * 105 * <pre>["file", "classpath"]</pre> 106 * 107 * @param key The key to add the property to. 108 * @param value The value to add. 109 */ 110 void addProperty(String key, Object value); 111 112 /** 113 * Set a property, this will replace any previously set values. Set values 114 * is implicitly a call to clearProperty(key), addProperty(key, value). 115 * 116 * @param key The key of the property to change 117 * @param value The new value 118 */ 119 void setProperty(String key, Object value); 120 121 /** 122 * Remove a property from the configuration. 123 * 124 * @param key the key to remove along with corresponding value. 125 */ 126 void clearProperty(String key); 127 128 /** 129 * Remove all properties from the configuration. 130 */ 131 void clear(); 132 133 /** 134 * Returns the {@code ConfigurationInterpolator} object used by this 135 * {@code Configuration}. This object is responsible for variable 136 * substitution. 137 * 138 * @return the {@code ConfigurationInterpolator} (can be <b>null</b>) 139 */ 140 ConfigurationInterpolator getInterpolator(); 141 142 /** 143 * Sets the {@code ConfigurationInterpolator} object to be used by this 144 * {@code Configuration}. This object is invoked for each access of a string 145 * property in order to substitute variables which may be contained. The 146 * argument can be <b>null</b> to disable interpolation at all. 147 * 148 * @param ci the new {@code ConfigurationInterpolator} 149 */ 150 void setInterpolator(ConfigurationInterpolator ci); 151 152 /** 153 * Creates and installs a new {@code ConfigurationInterpolator} for this 154 * {@code Configuration} based on the passed in arguments. This method 155 * creates a default {@code ConfigurationInterpolator} instance and 156 * initializes it with the passed in {@code Lookup} objects. It also adds a 157 * special default {@code Lookup} object that tries to resolve variables by 158 * matching them with properties contained in this {@code Configuration}. 159 * This is also the main difference to the 160 * {@link #setInterpolator(ConfigurationInterpolator)} method 161 * which sets the passed in object as is without adding this special lookup. 162 * 163 * @param prefixLookups the map with {@code Lookup} objects associated with 164 * specific prefixes (can be <b>null</b>) 165 * @param defLookups a collection with default {@code Lookup} objects (can 166 * be <b>null</b>) 167 * @see ConfigurationInterpolator 168 */ 169 void installInterpolator(Map<String, ? extends Lookup> prefixLookups, 170 Collection<? extends Lookup> defLookups); 171}