// Copyright (C) Stichting Deltares 2017. All rights reserved. // // This file is part of Ringtoets. // // Ringtoets 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; namespace Ringtoets.Common.Forms.Helpers { /// /// Helper class for generating unique names. /// public static class NamingHelper { /// /// Generate an unique name given a collection of existing named objects. /// /// Type of objects in the collection. /// All existing named objects. /// The base naming scheme to use. /// Getter method to determine the name of each object in . /// A unique name based on that is not used /// in . public static string GetUniqueName(IEnumerable existingObjects, string nameBase, Func nameGetter) { var i = 1; string result = nameBase; string[] existingNames = existingObjects.Select(nameGetter).ToArray(); while (existingNames.Any(name => name.Equals(result))) { result = $"{nameBase} ({i++})"; } return result; } } }