View Javadoc
1   /* ***************************************************************************
2    * Copyright (c) 2008 Brabenetz Harald, Austria.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   * 
16   *****************************************************************************/
17  package org.settings4j.settings;
18  
19  import org.settings4j.Settings4jFactory;
20  import org.settings4j.Settings4jInstance;
21  import org.settings4j.Settings4jRepository;
22  
23  /**
24   * This class is specialized in retrieving settings by name and also maintaining the settings hierarchy.
25   * <p>
26   * <em>The casual user does not have to deal with this class
27   * directly.</em>
28   * <p>
29   * The structure of the settings hierarchy is maintained by the {@link #getSettings} method. The hierarchy is such that
30   * children link to their parent but parents do not have any pointers to their children. Moreover, settings can be
31   * instantiated in any order, in particular descendant before ancestor.
32   * <p>
33   * In case a descendant is created before a particular ancestor, then it creates a provision node for the ancestor and
34   * adds itself to the provision node. Other descendants of the same ancestor add themselves to the previously created
35   * provision node.
36   * 
37   * @author hbrabenetz
38   */
39  public class DefaultSettingsRepository implements Settings4jRepository {
40  
41      private static final Settings4jFactory DEFAULT_FACTORY = new DefaultSettingsFactory();
42  
43      private Settings4jInstance settings;
44  
45      public Settings4jInstance getSettings() {
46          return getSettings(DEFAULT_FACTORY);
47      }
48  
49      /** {@inheritDoc} */
50      public Settings4jInstance getSettings(final Settings4jFactory factory) {
51          if (this.settings == null) {
52              this.settings = factory.makeNewSettingsInstance();
53          }
54          return this.settings;
55      }
56  
57      /** {@inheritDoc} */
58      public int getConnectorCount() {
59          if (this.settings == null) {
60              return 0;
61          }
62          // else
63          return this.settings.getConnectors().size();
64      }
65  
66      /** {@inheritDoc} */
67      public void resetConfiguration() {
68          this.settings.removeAllConnectors();
69      }
70  }