CompositeProperties.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #region Apache License
  2. //
  3. // Licensed to the Apache Software Foundation (ASF) under one or more
  4. // contributor license agreements. See the NOTICE file distributed with
  5. // this work for additional information regarding copyright ownership.
  6. // The ASF licenses this file to you under the Apache License, Version 2.0
  7. // (the "License"); you may not use this file except in compliance with
  8. // the License. You may obtain a copy of the License at
  9. //
  10. // http://www.apache.org/licenses/LICENSE-2.0
  11. //
  12. // Unless required by applicable law or agreed to in writing, software
  13. // distributed under the License is distributed on an "AS IS" BASIS,
  14. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. // See the License for the specific language governing permissions and
  16. // limitations under the License.
  17. //
  18. #endregion
  19. using System;
  20. using System.Collections;
  21. namespace log4net.Util
  22. {
  23. /// <summary>
  24. /// This class aggregates several PropertiesDictionary collections together.
  25. /// </summary>
  26. /// <remarks>
  27. /// <para>
  28. /// Provides a dictionary style lookup over an ordered list of
  29. /// <see cref="PropertiesDictionary"/> collections.
  30. /// </para>
  31. /// </remarks>
  32. /// <author>Nicko Cadell</author>
  33. public sealed class CompositeProperties
  34. {
  35. #region Private Instance Fields
  36. private PropertiesDictionary m_flattened = null;
  37. private ArrayList m_nestedProperties = new ArrayList();
  38. #endregion Private Instance Fields
  39. #region Public Instance Constructors
  40. /// <summary>
  41. /// Constructor
  42. /// </summary>
  43. /// <remarks>
  44. /// <para>
  45. /// Initializes a new instance of the <see cref="CompositeProperties" /> class.
  46. /// </para>
  47. /// </remarks>
  48. internal CompositeProperties()
  49. {
  50. }
  51. #endregion Public Instance Constructors
  52. #region Public Instance Properties
  53. /// <summary>
  54. /// Gets the value of a property
  55. /// </summary>
  56. /// <value>
  57. /// The value for the property with the specified key
  58. /// </value>
  59. /// <remarks>
  60. /// <para>
  61. /// Looks up the value for the <paramref name="key" /> specified.
  62. /// The <see cref="PropertiesDictionary"/> collections are searched
  63. /// in the order in which they were added to this collection. The value
  64. /// returned is the value held by the first collection that contains
  65. /// the specified key.
  66. /// </para>
  67. /// <para>
  68. /// If none of the collections contain the specified key then
  69. /// <c>null</c> is returned.
  70. /// </para>
  71. /// </remarks>
  72. public object this[string key]
  73. {
  74. get
  75. {
  76. // Look in the flattened properties first
  77. if (m_flattened != null)
  78. {
  79. return m_flattened[key];
  80. }
  81. // Look for the key in all the nested properties
  82. foreach(ReadOnlyPropertiesDictionary cur in m_nestedProperties)
  83. {
  84. if (cur.Contains(key))
  85. {
  86. return cur[key];
  87. }
  88. }
  89. return null;
  90. }
  91. }
  92. #endregion Public Instance Properties
  93. #region Public Instance Methods
  94. /// <summary>
  95. /// Add a Properties Dictionary to this composite collection
  96. /// </summary>
  97. /// <param name="properties">the properties to add</param>
  98. /// <remarks>
  99. /// <para>
  100. /// Properties dictionaries added first take precedence over dictionaries added
  101. /// later.
  102. /// </para>
  103. /// </remarks>
  104. public void Add(ReadOnlyPropertiesDictionary properties)
  105. {
  106. m_flattened = null;
  107. m_nestedProperties.Add(properties);
  108. }
  109. /// <summary>
  110. /// Flatten this composite collection into a single properties dictionary
  111. /// </summary>
  112. /// <returns>the flattened dictionary</returns>
  113. /// <remarks>
  114. /// <para>
  115. /// Reduces the collection of ordered dictionaries to a single dictionary
  116. /// containing the resultant values for the keys.
  117. /// </para>
  118. /// </remarks>
  119. public PropertiesDictionary Flatten()
  120. {
  121. if (m_flattened == null)
  122. {
  123. m_flattened = new PropertiesDictionary();
  124. for(int i=m_nestedProperties.Count; --i>=0; )
  125. {
  126. ReadOnlyPropertiesDictionary cur = (ReadOnlyPropertiesDictionary)m_nestedProperties[i];
  127. foreach(DictionaryEntry entry in cur)
  128. {
  129. m_flattened[(string)entry.Key] = entry.Value;
  130. }
  131. }
  132. }
  133. return m_flattened;
  134. }
  135. #endregion Public Instance Methods
  136. }
  137. }