/* Copyright 2010-present MongoDB Inc. * * Licensed 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. */ using System; using System.Collections; using System.Collections.Generic; namespace MongoDB.Bson.Serialization.Conventions { /// /// A mutable pack of conventions. /// public class ConventionPack : IConventionPack, IEnumerable { // private fields private readonly List _conventions; // constructors /// /// Initializes a new instance of the class. /// public ConventionPack() { _conventions = new List(); } // public properties /// /// Gets the conventions. /// public IEnumerable Conventions { get { return _conventions; } } // public methods /// /// Adds the specified convention. /// /// The convention. /// public void Add(IConvention convention) { if (convention == null) { throw new ArgumentNullException("convention"); } _conventions.Add(convention); } /// /// Adds a class map convention created using the specified action upon a class map. /// /// The name of the convention. /// The action the convention should take upon the class map. public void AddClassMapConvention(string name, Action action) { if (name == null) { throw new ArgumentNullException("name"); } Add(new DelegateClassMapConvention(name, action)); } /// /// Adds a member map convention created using the specified action upon a member map. /// /// The name of the convention. /// The action the convention should take upon the member map. public void AddMemberMapConvention(string name, Action action) { if (name == null) { throw new ArgumentNullException("name"); } Add(new DelegateMemberMapConvention(name, action)); } /// /// Adds a post processing convention created using the specified action upon a class map. /// /// The name of the convention. /// The action the convention should take upon the class map. public void AddPostProcessingConvention(string name, Action action) { if (name == null) { throw new ArgumentNullException("name"); } Add(new DelegatePostProcessingConvention(name, action)); } /// /// Adds a range of conventions. /// /// The conventions. /// public void AddRange(IEnumerable conventions) { if (conventions == null) { throw new ArgumentNullException("conventions"); } _conventions.AddRange(conventions); } /// /// Appends the conventions in another pack to the end of this pack. /// /// The other pack. public void Append(IConventionPack other) { if (other == null) { throw new ArgumentNullException("other"); } AddRange(other.Conventions); } /// /// Gets an enumerator for the conventions. /// /// An enumerator. public IEnumerator GetEnumerator() { return _conventions.GetEnumerator(); } /// /// Inserts the convention after another convention specified by the name. /// /// The name. /// The convention. public void InsertAfter(string name, IConvention convention) { if (name == null) { throw new ArgumentNullException("name"); } if (convention == null) { throw new ArgumentNullException("convention"); } var index = _conventions.FindIndex(x => x.Name == name); if (index == -1) { var message = string.Format("Unable to find a convention by the name of '{0}'.", name); throw new ArgumentOutOfRangeException("name", message); } _conventions.Insert(index + 1, convention); } /// /// Inserts the convention before another convention specified by the name. /// /// The name. /// The convention. public void InsertBefore(string name, IConvention convention) { if (name == null) { throw new ArgumentNullException("name"); } if (convention == null) { throw new ArgumentNullException("convention"); } var index = _conventions.FindIndex(x => x.Name == name); if (index == -1) { var message = string.Format("Unable to find a convention by the name of '{0}'.", name); throw new ArgumentOutOfRangeException("name", message); } _conventions.Insert(index, convention); } /// /// Removes the named convention. /// /// The name of the convention. public void Remove(string name) { if (name == null) { throw new ArgumentNullException("name"); } _conventions.RemoveAll(x => x.Name == name); } // explicit interface implementations IEnumerator IEnumerable.GetEnumerator() { return _conventions.GetEnumerator(); } } }