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 * @author Commons Configuration team 057 */ 058public interface Configuration extends ImmutableConfiguration, SynchronizerSupport 059{ 060 /** 061 * Return a decorator Configuration containing every key from the current 062 * Configuration that starts with the specified prefix. The prefix is 063 * removed from the keys in the subset. For example, if the configuration 064 * contains the following properties: 065 * 066 * <pre> 067 * prefix.number = 1 068 * prefix.string = Apache 069 * prefixed.foo = bar 070 * prefix = Jakarta</pre> 071 * 072 * the Configuration returned by {@code subset("prefix")} will contain 073 * the properties: 074 * 075 * <pre> 076 * number = 1 077 * string = Apache 078 * = Jakarta</pre> 079 * 080 * (The key for the value "Jakarta" is an empty string) 081 * <p> 082 * Since the subset is a decorator and not a modified copy of the initial 083 * Configuration, any change made to the subset is available to the 084 * Configuration, and reciprocally. 085 * 086 * @param prefix The prefix used to select the properties. 087 * @return a subset configuration 088 * 089 * @see SubsetConfiguration 090 */ 091 Configuration subset(String prefix); 092 093 /** 094 * Add a property to the configuration. If it already exists then the value 095 * stated here will be added to the configuration entry. For example, if 096 * the property: 097 * 098 * <pre>resource.loader = file</pre> 099 * 100 * is already present in the configuration and you call 101 * 102 * <pre>addProperty("resource.loader", "classpath")</pre> 103 * 104 * Then you will end up with a List like the following: 105 * 106 * <pre>["file", "classpath"]</pre> 107 * 108 * @param key The key to add the property to. 109 * @param value The value to add. 110 */ 111 void addProperty(String key, Object value); 112 113 /** 114 * Set a property, this will replace any previously set values. Set values 115 * is implicitly a call to clearProperty(key), addProperty(key, value). 116 * 117 * @param key The key of the property to change 118 * @param value The new value 119 */ 120 void setProperty(String key, Object value); 121 122 /** 123 * Remove a property from the configuration. 124 * 125 * @param key the key to remove along with corresponding value. 126 */ 127 void clearProperty(String key); 128 129 /** 130 * Remove all properties from the configuration. 131 */ 132 void clear(); 133 134 /** 135 * Returns the {@code ConfigurationInterpolator} object used by this 136 * {@code Configuration}. This object is responsible for variable 137 * substitution. 138 * 139 * @return the {@code ConfigurationInterpolator} (can be <b>null</b>) 140 */ 141 ConfigurationInterpolator getInterpolator(); 142 143 /** 144 * Sets the {@code ConfigurationInterpolator} object to be used by this 145 * {@code Configuration}. This object is invoked for each access of a string 146 * property in order to substitute variables which may be contained. The 147 * argument can be <b>null</b> to disable interpolation at all. 148 * 149 * @param ci the new {@code ConfigurationInterpolator} 150 */ 151 void setInterpolator(ConfigurationInterpolator ci); 152 153 /** 154 * Creates and installs a new {@code ConfigurationInterpolator} for this 155 * {@code Configuration} based on the passed in arguments. This method 156 * creates a default {@code ConfigurationInterpolator} instance and 157 * initializes it with the passed in {@code Lookup} objects. It also adds a 158 * special default {@code Lookup} object that tries to resolve variables by 159 * matching them with properties contained in this {@code Configuration}. 160 * This is also the main difference to the 161 * {@link #setInterpolator(ConfigurationInterpolator)} method 162 * which sets the passed in object as is without adding this special lookup. 163 * 164 * @param prefixLookups the map with {@code Lookup} objects associated with 165 * specific prefixes (can be <b>null</b>) 166 * @param defLookups a collection with default {@code Lookup} objects (can 167 * be <b>null</b>) 168 * @see ConfigurationInterpolator 169 */ 170 void installInterpolator(Map<String, ? extends Lookup> prefixLookups, 171 Collection<? extends Lookup> defLookups); 172}