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