Index: Ringtoets/Common/src/Ringtoets.Common.Data/FailureMechanism/FailureMechanismSectionCollection.cs =================================================================== diff -u -r4b2daa16b6b210d5d5c9a5f8f2a10a610add4eb1 -rd507de965210c6af40d57936780fa55b05430e13 --- Ringtoets/Common/src/Ringtoets.Common.Data/FailureMechanism/FailureMechanismSectionCollection.cs (.../FailureMechanismSectionCollection.cs) (revision 4b2daa16b6b210d5d5c9a5f8f2a10a610add4eb1) +++ Ringtoets/Common/src/Ringtoets.Common.Data/FailureMechanism/FailureMechanismSectionCollection.cs (.../FailureMechanismSectionCollection.cs) (revision d507de965210c6af40d57936780fa55b05430e13) @@ -29,7 +29,7 @@ namespace Ringtoets.Common.Data.FailureMechanism { - public class FailureMechanismSectionCollection : Observable, IObservableEnumerable + public class FailureMechanismSectionCollection : Observable, IEnumerable { private readonly List sections = new List(); @@ -76,19 +76,23 @@ Clear(); - List sourceCollection = failureMechanismSections.ToList(); - if (!sourceCollection.Any()) + if (failureMechanismSections.Any()) { - return; - } + FailureMechanismSection firstSection = failureMechanismSections.First(); + var newSections = new List + { + firstSection + }; - FailureMechanismSection firstSection = sourceCollection.First(); - sections.Add(firstSection); - sourceCollection.Remove(firstSection); + FailureMechanismSection previousSection = firstSection; + foreach (FailureMechanismSection section in failureMechanismSections.Skip(1)) + { + ValidateSection(section, previousSection); + newSections.Add(section); + previousSection = section; + } - foreach (FailureMechanismSection section in sourceCollection) - { - InsertSectionWhileMaintainingConnectivityOrder(section); + sections.AddRange(newSections); } SourcePath = sourcePath; @@ -105,24 +109,21 @@ } /// - /// Inserts the section to the collection while maintaining connectivity - /// order (neighboring have same end points). + /// Validates the section on its connectivity order (neighboring + /// must have same end points). /// - /// The new section. - /// Thrown when cannot - /// be connected to elements already defined in this collection. - private void InsertSectionWhileMaintainingConnectivityOrder(FailureMechanismSection sectionToInsert) + /// The new section. + /// The previous section. + /// Thrown when cannot + /// be connected to the previous section. + private static void ValidateSection(FailureMechanismSection section, FailureMechanismSection previousSection) { - if (sections.Last().EndPoint.Equals(sectionToInsert.StartPoint)) + if (!previousSection.EndPoint.Equals(section.StartPoint)) { - sections.Add(sectionToInsert); + string message = string.Format(Resources.FailureMechanismSectionCollection_ValidateSection_Section_0_must_connect_to_existing_sections, + section.Name); + throw new ArgumentException(message, nameof(section)); } - else - { - string message = string.Format(Resources.BaseFailureMechanism_AddSection_Section_0_must_connect_to_existing_sections, - sectionToInsert.Name); - throw new ArgumentException(message, nameof(sectionToInsert)); - } } } } \ No newline at end of file