ymikulski    2002/08/13 05:59:34

  Added:       csframework/src/cs/Parameters IParameterizable.cs
                        IReparameterizable.cs ParameterException.cs
                        Parameters.cs
  Log:
  no message
  
  Revision  Changes    Path
  1.1                  
jakarta-avalon-excalibur/csframework/src/cs/Parameters/IParameterizable.cs
  
  Index: IParameterizable.cs
  ===================================================================
  ///
  /// Copyright (C) The Apache Software Foundation. All rights reserved.
  ///
  /// This software is published under the terms of the Apache Software License
  /// version 1.1, a copy of which has been included  with this distribution in
  /// the LICENSE.txt file.
  ///
  using System;
  
  namespace Apache.Avalon.Parameters
  {
        /// <summary>
        /// Components should implement this interface if they wish to 
        /// be provided with parameters during startup. This interface
        /// will be called after IComposable.Compose(...) method and before
        /// <see cref="Apache.Avalon.Activity.IInitializable.Initialize()"/>.
        /// It is incompatible with the <see 
cref="Apache.Avalon.Configuration.IConfigurable"/> interface.
        /// </summary>
        public interface IParameterizable
        {
                /// <summary>
                /// Provides component with parameters.
                /// </summary>
                /// <param name="parameters">The Parameters</param>
                /// <exception cref="ParameterException">If parameters are 
invalid.</exception>
                void Parameterize(Parameters parameters );
        }
  }
  
  
  
  1.1                  
jakarta-avalon-excalibur/csframework/src/cs/Parameters/IReparameterizable.cs
  
  Index: IReparameterizable.cs
  ===================================================================
  ///
  /// Copyright (C) The Apache Software Foundation. All rights reserved.
  ///
  /// This software is published under the terms of the Apache Software License
  /// version 1.1, a copy of which has been included  with this distribution in
  /// the LICENSE.txt file.
  ///
  using System;
  
  namespace Apache.Avalon.Parameters
  {
        /// <summary>
        /// Components should implement this interface if they wish to
        /// be provided with parameters during its lifetime. This interface
        /// will be called after <see 
cref="Apache.Avalon.Activity.IStartable.Start()"/>
        /// and before <see cref="Apache.Avalon.Activity.IStartable.Stop()"/>.
        /// It is incompatible with the <see 
cref="Apache.Avalon.Configuration.IReconfigurable"/>
        /// interface.
        /// </summary>
        public interface IReparameterizable: IParameterizable
        {
                /// <summary>
                /// Provides component with parameters.
                /// </summary>
                /// <param name="parameters">The Parameters</param>
                /// <exception cref="ParameterException">If parameters are 
invalid.</exception>
                void Reparameterize( Parameters parameters );
        }
  }
  
  
  
  1.1                  
jakarta-avalon-excalibur/csframework/src/cs/Parameters/ParameterException.cs
  
  Index: ParameterException.cs
  ===================================================================
  ///
  /// Copyright (C) The Apache Software Foundation. All rights reserved.
  ///
  /// This software is published under the terms of the Apache Software License
  /// version 1.1, a copy of which has been included  with this distribution in
  /// the LICENSE.txt file.
  ///
  using System;
  
  namespace Apache.Avalon.Parameters
  {
        /// <summary>
        /// Thrown when a <see cref="IParameterizable"/> component cannot be 
parameterized
        /// properly, or if a value cannot be retrieved properly.
        /// </summary>
        public class ParameterException: Exception
        {
                /// <summary>
                /// Constructs a new <see cref="ParameterException"/> instance.
                /// </summary>
                public ParameterException(): this(null)
                {
                }
  
                /// <summary>
                /// Constructs a new <see cref="ParameterException"/> instance.
                /// </summary>
                /// <param name="message">The Detail message of the exception.</param>
                public ParameterException(string message): this(message, null)
                {
                }
  
                /// <summary>
                /// Constructs a new <see cref="ParameterException"/> instance.
                /// </summary>
                /// <param name="message">The Detail message of the exception.</param>
                /// <param name="inner">The Root cause of the exception.</param>
                public ParameterException(string message, Exception inner): base 
(message, inner)
                {
                }
        }
  }
  
  
  
  1.1                  
jakarta-avalon-excalibur/csframework/src/cs/Parameters/Parameters.cs
  
  Index: Parameters.cs
  ===================================================================
  ///
  /// Copyright (C) The Apache Software Foundation. All rights reserved.
  ///
  /// This software is published under the terms of the Apache Software License
  /// version 1.1, a copy of which has been included  with this distribution in
  /// the LICENSE.txt file.
  ///
  using System;
  using System.Collections; 
  using System.Runtime.Serialization; 
  
  using Apache.Avalon.Util;
  
  
  namespace Apache.Avalon.Parameters
  {
        /// <summary>
        /// The Parameters represents a set of key-value pairs.
        /// An Each value stored in Parameters has a key.
        /// </summary>
        /// <remarks>
        /// Note, this class is not thread safe by default.
        /// If you require thread safety please synchronize 
        /// write access to this class to prevent potential data corruption.
        /// </remarks> 
        [Serializable]
        public class Parameters: IEnumerable 
        {
                private bool readOnly;
                private Hashtable parameters = new Hashtable();
  
                /// <summary>
                /// Creates a new Parameters instance.
                /// </summary>
                public Parameters()
                {
                }
  
                /// <summary>
                /// Gets a value indicating whether the parameters is read-only.
                /// </summary>
                /// <value>True if the parameters are read-only; otherwise, 
false.</value>
                public bool IsReadOnly
                {
                        get
                        {
                                return readOnly;
                        }
                }
  
                /// <summary>
                /// Gets or sets the parameter associated with the specified name.
                /// </summary>
                /// <value>The parameter associated with the specified name.</value>
                public object this[string name]
                {
                        get
                        {
                                return GetParameter(name, null); 
                        }
                        set 
                        {
                                CheckReadOnly();
  
                                if (name != null)
                                {
                                        if (value == null)
                                        {
                                                parameters.Remove(name);
                                        }
                                        else 
                                        {
                                                parameters[name] = value; 
                                        }
                                }
                                
                        }
                }
  
                /// <summary>
                /// Gets the parameter with the specified name and 
                /// converts it into specified <see cref="System.Type"/>.
                /// </summary>
                /// <param name="name">The Parameter name</param>
                /// <param name="type">The <see cref="System.Type"/></param>
                /// <returns>The parameter value converted into the specified 
type.</returns>
                public object GetParameter(string name, Type type)
                {
                        return GetParameter(name, type, null);
                }
  
                /// <summary>
                /// Gets the parameter with the specified name and 
                /// converts it into specified <see cref="System.Type"/>.
                /// </summary>
                /// <param name="name">The Parameter name</param>
                /// <param name="type">The <see cref="System.Type"/></param>
                /// <param name="defaultValue">
                /// The defaultValue returned if the parameter has wrong type.
                /// </param> 
                /// <returns>The parameter value converted into the specified 
type.</returns>
                /// <exception cref="ParameterException">
                /// If the name of the parameter is incorrect.
                /// </exception>
                public object GetParameter(string name, Type type, object defaultValue)
                {
                        if (name == null)
                        {
                                throw new ParameterException( "You cannot lookup a 
null parameter" );
                        }
  
                        object result = parameters[name];
  
                        if( result == null )
                        {
                                throw new ParameterException( string.Format("The 
parameter {0} does not contain a value.", name));
                        }
  
                        return Converter.ChangeType(result, type, defaultValue);
                }
                
                /// <summary>
                /// Removes the parameter with the specified name.
                /// </summary>
                /// <param name="name">The parameter name to remove.</param>
                public void Remove(string name)
                {
                        this[name] = null;
                }
  
                /// <summary>
                /// Determines whether a parameters is in the <c>Parameters</c>.
                /// </summary>
                /// <param name="name">The Parameter name</param>
                /// <returns>True if the parameter is found; otherwise, 
false.</returns>
                public bool Contains(string name)
                {
                        return parameters.ContainsKey(name);
                }
  
                /// <summary>
                /// Makes the parameters be read-only.
                /// </summary>
                public void MakeReadOnly()
                {
                        readOnly = true;
                }
  
                protected void CheckReadOnly()
                {
                        if( IsReadOnly )
                        {
                                throw new ParameterException( "The Parameters instance 
is read only and can not be modified" );
                        }
                }
  
                /// <summary>
                /// Merge parameters from another <c>Parameters</c> instance
                /// into this.
                /// </summary>
                /// <param name="pars">The Other parameters</param>
                public void Merge(Parameters pars)
                {
                        CheckReadOnly();
  
                        foreach (DictionaryEntry entry in pars)
                        {
                                parameters[entry.Key] = entry.Value;
                        }
                }
                
                /// <summary>
                /// Returns an <see cref="IDictionaryEnumerator"/> that can iterate 
through the parameters.
                /// </summary>
                /// <returns>An <see cref="IDictionaryEnumerator"/> for the 
context.</returns>
                public IEnumerator GetEnumerator()
                {
                        return parameters.GetEnumerator();
                }
        }
  }
  
  
  

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to