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