Index: Riskeer/Common/src/Riskeer.Common.Forms/Helpers/CalculationsDisplayNameHelper.cs =================================================================== diff -u --- Riskeer/Common/src/Riskeer.Common.Forms/Helpers/CalculationsDisplayNameHelper.cs (revision 0) +++ Riskeer/Common/src/Riskeer.Common.Forms/Helpers/CalculationsDisplayNameHelper.cs (revision f742d5fba12ab75ba892b2c73eab0faebd967335) @@ -0,0 +1,117 @@ +// Copyright (C) Stichting Deltares 2021. All rights reserved. +// +// This file is part of Riskeer. +// +// Riskeer is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +// +// All names, logos, and references to "Deltares" are registered trademarks of +// Stichting Deltares and remain full property of Stichting Deltares at all times. +// All rights reserved. + +using System; +using System.Collections.Generic; +using System.Linq; +using Riskeer.Common.Data.AssessmentSection; +using Riskeer.Common.Data.Hydraulics; +using Riskeer.Common.Forms.TypeConverters; + +namespace Riskeer.Common.Forms.Helpers +{ + /// + /// This class helps in obtaining unique display names for different types of calculations within + /// an . + /// + public static class CalculationsDisplayNameHelper + { + private static readonly NoProbabilityValueDoubleConverter noProbabilityValueDoubleConverter = new NoProbabilityValueDoubleConverter(); + + /// + /// Gets a unique water level calculations display name for the provided . + /// + /// The assessment section the belong to. + /// The water level calculations to get the unique display name for. + /// A unique water level calculations display name. + /// Thrown when any input parameter is null. + /// Thrown when is not part of the water level + /// calculations within . + public string GetUniqueDisplayNameForWaterLevelCalculations(IAssessmentSection assessmentSection, IEnumerable calculations) + { + if (assessmentSection == null) + { + throw new ArgumentNullException(nameof(assessmentSection)); + } + + if (calculations == null) + { + throw new ArgumentNullException(nameof(calculations)); + } + + var nonUniqueWaterLevelCalculationsDisplayNameLookup = new Dictionary + { + { + assessmentSection.WaterLevelCalculationsForLowerLimitNorm, + noProbabilityValueDoubleConverter.ConvertToString(assessmentSection.FailureMechanismContribution.LowerLimitNorm) + }, + { + assessmentSection.WaterLevelCalculationsForSignalingNorm, + noProbabilityValueDoubleConverter.ConvertToString(assessmentSection.FailureMechanismContribution.SignalingNorm) + } + }; + + foreach (HydraulicBoundaryLocationCalculationsForTargetProbability calculationsForTargetProbability in assessmentSection.WaterLevelCalculationsForUserDefinedTargetProbabilities) + { + nonUniqueWaterLevelCalculationsDisplayNameLookup.Add(calculationsForTargetProbability.HydraulicBoundaryLocationCalculations, + noProbabilityValueDoubleConverter.ConvertToString(calculationsForTargetProbability.TargetProbability)); + } + + if (!nonUniqueWaterLevelCalculationsDisplayNameLookup.ContainsKey(calculations)) + { + throw new InvalidOperationException("The provided calculations object is not part of the water level calculations within the assessment section."); + } + + return GetUniqueDisplayNameLookup(nonUniqueWaterLevelCalculationsDisplayNameLookup)[calculations]; + } + + private static Dictionary GetUniqueDisplayNameLookup(IDictionary nonUniqueDisplayNameLookup) + { + var uniqueDisplayNameLookup = new Dictionary(); + + while (nonUniqueDisplayNameLookup.Any()) + { + KeyValuePair firstElement = nonUniqueDisplayNameLookup.First(); + + IList> elementsWithSameDisplayNameAsFirstElement = nonUniqueDisplayNameLookup.Where(e => e.Value.Equals(firstElement.Value)) + .ToList(); + + if (elementsWithSameDisplayNameAsFirstElement.Count > 1) + { + for (var i = 0; i < elementsWithSameDisplayNameAsFirstElement.Count; i++) + { + KeyValuePair elementWithNonUniqueDisplayName = elementsWithSameDisplayNameAsFirstElement.ElementAt(i); + + uniqueDisplayNameLookup.Add(elementWithNonUniqueDisplayName.Key, elementWithNonUniqueDisplayName.Value + $"({i + 1})"); + nonUniqueDisplayNameLookup.Remove(elementWithNonUniqueDisplayName.Key); + } + } + else + { + uniqueDisplayNameLookup.Add(firstElement.Key, firstElement.Value); + nonUniqueDisplayNameLookup.Remove(firstElement.Key); + } + } + + return uniqueDisplayNameLookup; + } + } +} \ No newline at end of file Fisheye: Tag f742d5fba12ab75ba892b2c73eab0faebd967335 refers to a dead (removed) revision in file `Riskeer/Common/src/Riskeer.Common.Forms/Helpers/HydraulicBoundaryLocationCalculationsNamingHelper.cs'. Fisheye: No comparison available. Pass `N' to diff?