DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
Configuration.cs
Go to the documentation of this file.
1 //
2 // Copyright 2010-2015 Deveel
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 using System;
18 using System.Collections;
19 using System.Collections.Generic;
20 using System.Linq;
21 
22 using Deveel.Data;
23 
24 namespace Deveel.Data.Configuration {
25  public class Configuration : IConfiguration {
26  private readonly bool isRoot;
27  private readonly Dictionary<string, object> values;
28 
32  private Configuration(bool isRoot) {
33  Parent = null;
34  this.isRoot = isRoot;
35  values = new Dictionary<string, object>();
36  }
37 
45  : this(parent == null) {
46  Parent = parent;
47  Source = source;
48 
49  if (source != null)
50  this.Load(source);
51  }
52 
54  : this(null, source) {
55  }
56 
57  public Configuration()
58  : this(true) {
59  }
60 
62  : this(parent, null) {
63  }
64 
66  public IConfigSource Source { get; set; }
67 
69  public IConfiguration Parent { get; set; }
70 
72  public IEnumerable<string> GetKeys(ConfigurationLevel level) {
73  var returnKeys = new Dictionary<string, string>();
74  if (!isRoot && Parent != null && level == ConfigurationLevel.Deep) {
75  var configKeys = Parent.GetKeys(level);
76  foreach (var pair in configKeys) {
77  returnKeys[pair] = pair;
78  }
79  }
80 
81  foreach (var configKey in values.Keys) {
82  returnKeys[configKey] = configKey;
83  }
84 
85  return returnKeys.Values.AsEnumerable();
86  }
87 
88  public IEnumerator<KeyValuePair<string, object>> GetEnumerator() {
89  var keys = GetKeys(ConfigurationLevel.Deep);
90  return keys.Select(key => new KeyValuePair<string, object>(key, GetValue(key))).GetEnumerator();
91  }
92 
93  IEnumerator IEnumerable.GetEnumerator() {
94  return GetEnumerator();
95  }
96 
97 
99  public void SetValue(string key, object value) {
100  if (String.IsNullOrEmpty(key))
101  throw new ArgumentNullException("key");
102 
103  if (value == null) {
104  values.Remove(key);
105  } else {
106  values[key] = value;
107  }
108  }
109 
111  public object GetValue(string key) {
112  if (String.IsNullOrEmpty(key))
113  throw new ArgumentNullException("key");
114 
115  object value;
116  if (values.TryGetValue(key, out value))
117  return value;
118 
119  if (!isRoot && Parent != null &&
120  ((value = Parent.GetValue(key)) != null))
121  return value;
122 
123  return null;
124  }
125  }
126 }
IEnumerable< string > GetKeys(ConfigurationLevel level)
Enumerates the keys that can be obtained by the object, at the given ConfigurationLevel.
readonly Dictionary< string, object > values
A source for stored configurations or destination to configurations to store.
Configuration(IConfiguration parent)
ConfigurationLevel
Defines the level of configuration settings to save or read
void SetValue(string key, object value)
Sets a given value for a key defined by this object.
IEnumerator< KeyValuePair< string, object > > GetEnumerator()
Configuration(IConfiguration parent, IConfigSource source)
Constructs the Configuration from the given parent.
Defines the contract for the configuration node of a component within the system or of the system its...
object GetValue(string key)
Gets a configuration setting for the given key.
Configuration(bool isRoot)
Constructs the Configuration.