Index: Ringtoets/GrassCoverErosionInwards/src/Ringtoets.GrassCoverErosionInwards.Utils/GrassCoverErosionInwardsHelper.cs =================================================================== diff -u -r277b68e1c58f97f527567f5500be6bc39efacd97 -r38600213ce6ca43c1819c81dd95c8ce786650ac3 --- Ringtoets/GrassCoverErosionInwards/src/Ringtoets.GrassCoverErosionInwards.Utils/GrassCoverErosionInwardsHelper.cs (.../GrassCoverErosionInwardsHelper.cs) (revision 277b68e1c58f97f527567f5500be6bc39efacd97) +++ Ringtoets/GrassCoverErosionInwards/src/Ringtoets.GrassCoverErosionInwards.Utils/GrassCoverErosionInwardsHelper.cs (.../GrassCoverErosionInwardsHelper.cs) (revision 38600213ce6ca43c1819c81dd95c8ce786650ac3) @@ -21,6 +21,7 @@ using System; using System.Collections.Generic; +using System.Linq; using Ringtoets.Common.Data.Calculation; using Ringtoets.Common.Data.FailureMechanism; using Ringtoets.Common.Utils; @@ -29,96 +30,150 @@ namespace Ringtoets.GrassCoverErosionInwards.Utils { /// - /// Class holds helper methods to match objects + /// Class which defines helper methods to match objects /// with objects. /// public static class GrassCoverErosionInwardsHelper { /// /// Determine which objects are available for a - /// . + /// . /// - /// The objects. - /// The objects. + /// The objects. + /// The objects. /// A containing a - /// of objects + /// of objects /// for each section name which has calculations. - /// When any input parameter is null. - public static Dictionary> CollectCalculationsPerSegment( - IEnumerable sections, - IEnumerable calculations) + /// Thrown when any input parameter is null + /// or when an element in is null + /// Thrown when an element in is + /// null. + public static Dictionary> CollectCalculationsPerSection(IEnumerable sections, IEnumerable calculations) { - if (sections == null) - { - throw new ArgumentNullException("sections"); - } + return AssignUnassignCalculations.CollectCalculationsPerSection(sections, CalculationsToCalculationsWithLocations(calculations)); + } - if (calculations == null) + /// + /// Obtains the which intersects with the defined on + /// . + /// + /// The of to iterate over when trying to find a match + /// with the location of . + /// The which's location is used to match to the location of the + /// . + /// Returns the closest to or null if the + /// has no location. + /// Thrown when is null + /// Thrown when an element in is null. + /// + public static FailureMechanismSection FailureMechanismSectionForCalculation(IEnumerable sections, GrassCoverErosionInwardsCalculation calculation) + { + var asCalculationWithLocation = AsCalculationWithLocation(calculation); + if (asCalculationWithLocation != null) { - throw new ArgumentNullException("calculations"); + return AssignUnassignCalculations.FailureMechanismSectionForCalculation(sections, asCalculationWithLocation); } + return null; + } - SectionSegments[] sectionSegments = SectionSegmentsHelper.MakeSectionSegments(sections); + /// + /// Updates the for each element of that have the + /// assigned, or should have the assigned. + /// + /// The of to iterate while + /// possibly updating the assigned to it. + /// The which has a location that has been updated. + /// When is null + /// Thrown when element in is + /// null. + public static void Update(IEnumerable sectionResults, GrassCoverErosionInwardsCalculation calculation) + { + ValidateSectionResults(sectionResults); - var calculationsPerSegment = new Dictionary>(); - - foreach (var calculation in calculations) + var asCalculationWithLocation = AsCalculationWithLocation(calculation); + if (asCalculationWithLocation != null) { - FailureMechanismSection section = FindSectionForCalculation(sectionSegments, calculation); - if (section == null) - { - continue; - } - - UpdateCalculationsOfSegment(calculationsPerSegment, section.Name, calculation); + AssignUnassignCalculations.Update(sectionResults.Select(AsCalculationAssignment), asCalculationWithLocation); } - return calculationsPerSegment; } /// - /// Determine which geometrically contains the . + /// Removed the for each + /// element of that have the assigned. /// - /// The objects - /// whose are considered. - /// The . - /// The containing , or null. - /// When any input parameter is null. - public static FailureMechanismSection FailureMechanismSectionForCalculation( - IEnumerable sections, - GrassCoverErosionInwardsCalculation calculation) + /// The of to iterate while + /// removing the reference to the if present. + /// The which has a location that has been updated. + /// The of that were left after removing + /// . + /// When any input parameter is null or when an element + /// in is null. + /// Thrown when element in is + /// null. + public static void Delete(IEnumerable sectionResults, GrassCoverErosionInwardsCalculation calculation, IEnumerable calculations) { - if (sections == null) + ValidateSectionResults(sectionResults); + + AssignUnassignCalculations.Delete( + sectionResults.Select(AsCalculationAssignment), + calculation, + CalculationsToCalculationsWithLocations(calculations)); + } + + /// + /// Transforms the into and filter out the calculations + /// for which a could not be made. + /// + /// The collection to transform. + /// A collection of . + /// Thrown when is null or when + /// an element in is null. + private static IEnumerable CalculationsToCalculationsWithLocations(IEnumerable calculations) + { + if (calculations == null) { - throw new ArgumentNullException("sections"); + throw new ArgumentNullException("calculations"); } + return calculations.Select(AsCalculationWithLocation).Where(c => c != null); + } + /// + /// Transforms the into a . + /// + /// The to transform. + /// A new or null if the calculation had no value + /// or had no dike profile assigned. + /// Thrown when is null. + private static CalculationWithLocation AsCalculationWithLocation(GrassCoverErosionInwardsCalculation calculation) + { if (calculation == null) { throw new ArgumentNullException("calculation"); } - - SectionSegments[] sectionSegments = SectionSegmentsHelper.MakeSectionSegments(sections); - - return FindSectionForCalculation(sectionSegments, calculation); + if (calculation.InputParameters.DikeProfile == null) + { + return null; + } + return new CalculationWithLocation(calculation, calculation.InputParameters.DikeProfile.WorldReferencePoint); } - private static FailureMechanismSection FindSectionForCalculation(SectionSegments[] sectionSegmentsCollection, - GrassCoverErosionInwardsCalculation calculation) + private static void ValidateSectionResults(IEnumerable sectionResults) { - var dikeProfile = calculation.InputParameters.DikeProfile; - return dikeProfile != null - ? SectionSegmentsHelper.GetSectionForPoint(sectionSegmentsCollection, dikeProfile.WorldReferencePoint) - : null; + if (sectionResults == null) + { + throw new ArgumentNullException("sectionResults"); + } + if (sectionResults.Any(sr => sr == null)) + { + throw new ArgumentException("SectionResults contains an entry without value.", "sectionResults"); + } } - private static void UpdateCalculationsOfSegment(Dictionary> calculationsPerSegment, - string sectionName, GrassCoverErosionInwardsCalculation calculation) + private static SectionResultWithCalculationAssignment AsCalculationAssignment(GrassCoverErosionInwardsFailureMechanismSectionResult failureMechanismSectionResult) { - if (!calculationsPerSegment.ContainsKey(sectionName)) - { - calculationsPerSegment.Add(sectionName, new List()); - } - calculationsPerSegment[sectionName].Add(calculation); + return new SectionResultWithCalculationAssignment(failureMechanismSectionResult, + result => ((GrassCoverErosionInwardsFailureMechanismSectionResult) result).Calculation, + (result, calculation) => ((GrassCoverErosionInwardsFailureMechanismSectionResult) result).Calculation = (GrassCoverErosionInwardsCalculation) calculation); } } } \ No newline at end of file