View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.configuration2;
19  
20  import java.util.Collection;
21  import java.util.Map;
22  
23  import org.apache.commons.configuration2.interpol.ConfigurationInterpolator;
24  import org.apache.commons.configuration2.interpol.Lookup;
25  import org.apache.commons.configuration2.sync.SynchronizerSupport;
26  
27  /**
28   * <p>
29   * The main Configuration interface.
30   * </p>
31   * <p>
32   * This interface allows accessing and manipulating a configuration object. The major part of the methods defined in
33   * this interface deals with accessing properties of various data types. There is a generic {@code getProperty()}
34   * method, which returns the value of the queried property in its raw data type. Other getter methods try to convert
35   * this raw data type into a specific data type. If this fails, a {@code ConversionException} will be thrown.
36   * </p>
37   * <p>
38   * For most of the property getter methods an overloaded version exists that allows to specify a default value, which
39   * will be returned if the queried property cannot be found in the configuration. The behavior of the methods that do
40   * not take a default value in case of a missing property is not defined by this interface and depends on a concrete
41   * implementation. E.g. the {@link AbstractConfiguration} class, which is the base class of most configuration
42   * implementations provided by this package, per default returns <strong>null</strong> if a property is not found, but provides
43   * the {@link AbstractConfiguration#setThrowExceptionOnMissing(boolean) setThrowExceptionOnMissing()} method, with which
44   * it can be configured to throw a {@code NoSuchElementException} exception in that case. (Note that getter methods for
45   * primitive types in {@code AbstractConfiguration} always throw an exception for missing properties because there is no
46   * way of overloading the return value.)
47   * </p>
48   * <p>
49   * With the {@code addProperty()} and {@code setProperty()} methods new properties can be added to a configuration or
50   * the values of properties can be changed. With {@code clearProperty()} a property can be removed. Other methods allow
51   * to iterate over the contained properties or to create a subset configuration.
52   * </p>
53   */
54  public interface Configuration extends ImmutableConfiguration, SynchronizerSupport {
55  
56      /**
57       * Add a property to the configuration. If it already exists then the value stated here will be added to the
58       * configuration entry. For example, if the property:
59       *
60       * <pre>
61       * resource.loader = file
62       * </pre>
63       *
64       * is already present in the configuration and you call
65       *
66       * <pre>
67       * addProperty("resource.loader", "classpath")
68       * </pre>
69       *
70       * Then you will end up with a List like the following:
71       *
72       * <pre>
73       * ["file", "classpath"]
74       * </pre>
75       *
76       * @param key The key to add the property to.
77       * @param value The value to add.
78       */
79      void addProperty(String key, Object value);
80  
81      /**
82       * Remove all properties from the configuration.
83       */
84      void clear();
85  
86      /**
87       * Remove a property from the configuration.
88       *
89       * @param key the key to remove along with corresponding value.
90       */
91      void clearProperty(String key);
92  
93      /**
94       * Gets the {@code ConfigurationInterpolator} object used by this {@code Configuration}. This object is responsible
95       * for variable substitution.
96       *
97       * @return the {@code ConfigurationInterpolator} (can be <strong>null</strong>)
98       */
99      ConfigurationInterpolator getInterpolator();
100 
101     /**
102      * Creates and installs a new {@code ConfigurationInterpolator} for this {@code Configuration} based on the passed in
103      * arguments. This method creates a default {@code ConfigurationInterpolator} instance and initializes it with the
104      * passed in {@code Lookup} objects. It also adds a special default {@code Lookup} object that tries to resolve
105      * variables by matching them with properties contained in this {@code Configuration}. This is also the main difference
106      * to the {@link #setInterpolator(ConfigurationInterpolator)} method which sets the passed in object as is without
107      * adding this special lookup.
108      *
109      * @param prefixLookups the map with {@code Lookup} objects associated with specific prefixes (can be <strong>null</strong>)
110      * @param defLookups a collection with default {@code Lookup} objects (can be <strong>null</strong>)
111      * @see ConfigurationInterpolator
112      */
113     void installInterpolator(Map<String, ? extends Lookup> prefixLookups, Collection<? extends Lookup> defLookups);
114 
115     /**
116      * Sets the {@code ConfigurationInterpolator} object to be used by this {@code Configuration}. This object is invoked
117      * for each access of a string property in order to substitute variables which may be contained. The argument can be
118      * <strong>null</strong> to disable interpolation at all.
119      *
120      * @param ci the new {@code ConfigurationInterpolator}
121      */
122     void setInterpolator(ConfigurationInterpolator ci);
123 
124     /**
125      * Sets a property, this will replace any previously set values. Set values is implicitly a call to clearProperty(key),
126      * addProperty(key, value).
127      *
128      * @param key The key of the property to change
129      * @param value The new value
130      */
131     void setProperty(String key, Object value);
132 
133     /**
134      * Return a decorator Configuration containing every key from the current Configuration that starts with the specified
135      * prefix. The prefix is removed from the keys in the subset. For example, if the configuration contains the following
136      * properties:
137      *
138      * <pre>
139      *    prefix.number = 1
140      *    prefix.string = Apache
141      *    prefixed.foo = bar
142      *    prefix = Jakarta
143      * </pre>
144      *
145      * the Configuration returned by {@code subset("prefix")} will contain the properties:
146      *
147      * <pre>
148      *    number = 1
149      *    string = Apache
150      *    = Jakarta
151      * </pre>
152      *
153      * (The key for the value "Jakarta" is an empty string)
154      * <p>
155      * Since the subset is a decorator and not a modified copy of the initial Configuration, any change made to the subset
156      * is available to the Configuration, and reciprocally.
157      *
158      * @param prefix The prefix used to select the properties.
159      * @return a subset configuration
160      * @see SubsetConfiguration
161      */
162     Configuration subset(String prefix);
163 }