// --------------------------------------------------------------------------- // // Copyright (c) Microsoft Corporation. All rights reserved. // // --------------------------------------------------------------------------- // --------------------------------------------------------------------- // // // --------------------------------------------------------------------- namespace Microsoft.Database.Isam { using System; using System.Collections; using System.Globalization; /// /// A Field Collection represents the set of fields that are in a given /// record. It can be used to efficiently navigate those fields. /// public class FieldCollection : DictionaryBase, IEnumerable { /// /// The location /// private Location location; /// /// The read only /// private bool readOnly = false; /// /// Gets a value indicating whether this field collection cannot be changed. /// public bool IsReadOnly { get { return this.readOnly; } } /// /// Gets the names. /// /// /// The names. /// public ICollection Names { get { return this.Dictionary.Keys; } } /// /// Gets or sets the location of the record that contained these fields. /// /// /// The location. /// public Location Location { get { return this.location; } set { this.CheckReadOnly(); this.location = value; } } /// /// Gets a value indicating whether the object has a fixed size. /// /// true if the object has a fixed size; otherwise, false. public bool IsFixedSize { get { return this.readOnly; } } /// /// Sets a value indicating whether [read only]. /// /// /// true if [read only]; otherwise, false. /// internal bool ReadOnly { set { this.readOnly = value; } } /// /// The field values for the specified column. /// /// /// The . /// /// the name of the column whose field values are desired /// A object to access values of this column. public FieldValueCollection this[string columnName] { get { return (FieldValueCollection)Dictionary[columnName.ToLower(CultureInfo.InvariantCulture)]; } set { this.Dictionary[columnName.ToLower(CultureInfo.InvariantCulture)] = value; } } /// /// The field values for the specified column /// /// /// The . /// /// the column ID whose field values are desired /// A object to access values of this column. public FieldValueCollection this[Columnid column] { get { return (FieldValueCollection)Dictionary[column.Name.ToLower(CultureInfo.InvariantCulture)]; } set { this.Dictionary[column.Name.ToLower(CultureInfo.InvariantCulture)] = value; } } /// /// Fetches an enumerator containing all the fields for this record. /// /// An enumerator containing all the fields for this record. /// /// This is the type safe version that may not work in other CLR /// languages. /// public new RecordEnumerator GetEnumerator() { return new RecordEnumerator(Dictionary.GetEnumerator()); } /// /// Adds the specified values. /// /// The values. public void Add(FieldValueCollection values) { this.Dictionary.Add(values.Name.ToLower(CultureInfo.InvariantCulture), values); } /// /// Returns whether the specifed column exists in the row. /// /// Name of the column. /// Whether the specifed column exists in the row. public bool Contains(string columnName) { return this.Dictionary.Contains(columnName.ToLower(CultureInfo.InvariantCulture)); } /// /// Returns whether the specifed column exists in the row. /// /// The column. /// Whether the specifed column exists in the row. public bool Contains(Columnid column) { return this.Dictionary.Contains(column.Name.ToLower(CultureInfo.InvariantCulture)); } /// /// Removes the specified column name. /// /// Name of the column. public void Remove(string columnName) { this.Dictionary.Remove(columnName.ToLower(CultureInfo.InvariantCulture)); } /// /// Removes the specified column. /// /// The column. public void Remove(Columnid column) { this.Dictionary.Remove(column.Name.ToLower(CultureInfo.InvariantCulture)); } /// /// Fetches an enumerator containing all the fields for this record /// /// /// An object that can be used to iterate through the collection. /// /// /// This is the standard version that will work with other CLR /// languages. /// IEnumerator IEnumerable.GetEnumerator() { return (IEnumerator)this.GetEnumerator(); } /// /// Performs additional custom processes before clearing the contents of the instance. /// protected override void OnClear() { this.CheckReadOnly(); } /// /// Performs additional custom processes before inserting a new element into the instance. /// /// The key of the element to insert. /// The value of the element to insert. protected override void OnInsert(object key, object value) { this.CheckReadOnly(); } /// /// Performs additional custom processes before removing an element from the instance. /// /// The key of the element to remove. /// The value of the element to remove. protected override void OnRemove(object key, object value) { this.CheckReadOnly(); } /// /// Performs additional custom processes before setting a value in the instance. /// /// The key of the element to locate. /// The old value of the element associated with . /// The new value of the element associated with . protected override void OnSet(object key, object oldValue, object newValue) { this.CheckReadOnly(); } /// /// Performs additional custom processes when validating the element with the specified key and value. /// /// The key of the element to validate. /// The value of the element to validate. /// /// key must be of type System.String;key /// or /// value must be of type FieldValueCollection;value /// or /// key must match value.Name;key /// protected override void OnValidate(object key, object value) { if (!(key is string)) { throw new ArgumentException("key must be of type System.String", "key"); } if (!(value is FieldValueCollection)) { throw new ArgumentException("value must be of type FieldValueCollection", "value"); } if (((string)key).ToLower(CultureInfo.InvariantCulture) != ((FieldValueCollection)value).Name.ToLower(CultureInfo.InvariantCulture)) { throw new ArgumentException("key must match value.Name", "key"); } } /// /// Checks the read only. /// /// this field collection cannot be changed private void CheckReadOnly() { if (this.readOnly) { throw new NotSupportedException("this field collection cannot be changed"); } } } }