#region Apache License // // Licensed to the Apache Software Foundation (ASF) under one or more // contributor license agreements. See the NOTICE file distributed with // this work for additional information regarding copyright ownership. // The ASF licenses this file to you under the Apache License, Version 2.0 // (the "License"); you may not use this file except in compliance with // the License. You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // #endregion using System; using System.Collections; namespace log4net.Util { /// /// This class aggregates several PropertiesDictionary collections together. /// /// /// /// Provides a dictionary style lookup over an ordered list of /// collections. /// /// /// Nicko Cadell public sealed class CompositeProperties { #region Private Instance Fields private PropertiesDictionary m_flattened = null; private ArrayList m_nestedProperties = new ArrayList(); #endregion Private Instance Fields #region Public Instance Constructors /// /// Constructor /// /// /// /// Initializes a new instance of the class. /// /// internal CompositeProperties() { } #endregion Public Instance Constructors #region Public Instance Properties /// /// Gets the value of a property /// /// /// The value for the property with the specified key /// /// /// /// Looks up the value for the specified. /// The collections are searched /// in the order in which they were added to this collection. The value /// returned is the value held by the first collection that contains /// the specified key. /// /// /// If none of the collections contain the specified key then /// null is returned. /// /// public object this[string key] { get { // Look in the flattened properties first if (m_flattened != null) { return m_flattened[key]; } // Look for the key in all the nested properties foreach(ReadOnlyPropertiesDictionary cur in m_nestedProperties) { if (cur.Contains(key)) { return cur[key]; } } return null; } } #endregion Public Instance Properties #region Public Instance Methods /// /// Add a Properties Dictionary to this composite collection /// /// the properties to add /// /// /// Properties dictionaries added first take precedence over dictionaries added /// later. /// /// public void Add(ReadOnlyPropertiesDictionary properties) { m_flattened = null; m_nestedProperties.Add(properties); } /// /// Flatten this composite collection into a single properties dictionary /// /// the flattened dictionary /// /// /// Reduces the collection of ordered dictionaries to a single dictionary /// containing the resultant values for the keys. /// /// public PropertiesDictionary Flatten() { if (m_flattened == null) { m_flattened = new PropertiesDictionary(); for(int i=m_nestedProperties.Count; --i>=0; ) { ReadOnlyPropertiesDictionary cur = (ReadOnlyPropertiesDictionary)m_nestedProperties[i]; foreach(DictionaryEntry entry in cur) { m_flattened[(string)entry.Key] = entry.Value; } } } return m_flattened; } #endregion Public Instance Methods } }