/// <summary> /// Returns an empty template group. /// </summary> /// <param name="startingId">The lowest value for TemplateId in the group, which must be /// greater than or equal to 0.</param> public static ITemplateGroup CreateTemplateGroup(int startingId) { Contract.Requires(startingId >= 0); var group = new TemplateGroup(); // We consume startingId-1 so that the first id generator is equal to startingId group.TemplateIdGenerator.Consume(startingId - 1); return group; }
/// <summary> /// Merges a set of template groups together into one template group. An /// InvalidOperationException is thrown if there are two templates with the same TemplateId. /// </summary> /// <param name="groups">The template groups to merge.</param> /// <returns>A single template group that contains all of the templates within the given /// groups.</returns> /// <exception cref="InvalidOperationException">There were duplicate TemplateIds</exception> public static ITemplateGroup MergeTemplateGroups(IEnumerable<ITemplateGroup> groups) { // We store all of our templates in result. var result = new TemplateGroup(); // ids verifies that every template has a unique TemplateId by checking for insertion // failures (hash collisions) -- we could also use a SparseArray, but that could be // * very* slow if the template groups go into the high ranges. var ids = new HashSet<int>(); foreach (TemplateGroup templateGroup in groups.Cast<TemplateGroup>()) { foreach (ITemplate template in templateGroup.Templates) { int id = template.TemplateId; // verify that we don't have any duplicate template ids if (ids.Add(id) == false) { throw new InvalidOperationException("Duplicate template with id=" + id); } // add the template to the result and consume its id result.Templates.Add(template); result.TemplateIdGenerator.Consume(id); } } return result; }