Index: DamEngine/trunk/src/Deltares.DamEngine.Data/Standard/UniqueNameProvider.cs =================================================================== diff -u -r4000 -r4052 --- DamEngine/trunk/src/Deltares.DamEngine.Data/Standard/UniqueNameProvider.cs (.../UniqueNameProvider.cs) (revision 4000) +++ DamEngine/trunk/src/Deltares.DamEngine.Data/Standard/UniqueNameProvider.cs (.../UniqueNameProvider.cs) (revision 4052) @@ -23,132 +23,131 @@ using System.Collections; using System.Collections.Generic; -namespace Deltares.DamEngine.Data.Standard +namespace Deltares.DamEngine.Data.Standard; + +public class UniqueNameProvider { - public class UniqueNameProvider - { - private static readonly List collections = new List(); + private static readonly List collections = new List(); - static UniqueNameProvider() {} + static UniqueNameProvider() {} - public static void Clear() - { - collections.Clear(); - } + public static void Clear() + { + collections.Clear(); + } - public static void Register(params IList[] lists) + public static void Register(params IList[] lists) + { + foreach (IList list in lists) { - foreach (IList list in lists) + if (!collections.Contains(list)) { - if (!collections.Contains(list)) - { - collections.Add(list); - } + collections.Add(list); } } + } - public static void ProvideUniqueName(IList collection, IName item, Func translator = null) + public static void ProvideUniqueName(IList collection, IName item, Func translator = null) + { + if (string.IsNullOrEmpty(item.Name)) { - if (string.IsNullOrEmpty(item.Name)) - { - Type type = collection.GetType().GetGenericArgumentsFromFirstGenericSuperClass()[0]; - item.Name = translator == null ? type.Name : translator(type.Name); - } + Type type = collection.GetType().GetGenericArgumentsFromFirstGenericSuperClass()[0]; + item.Name = translator == null ? type.Name : translator(type.Name); + } - int indexer; - if (NameContainsIndexer(item.Name, out indexer)) + int indexer; + if (NameContainsIndexer(item.Name, out indexer)) + { + string newName = item.Name; + while (NameExists(collection, item, newName)) { - string newName = item.Name; - while (NameExists(collection, item, newName)) - { - newName = newName.Replace(string.Format("({0})", indexer), string.Format("({0})", ++indexer)); - } - - item.Name = newName; + newName = newName.Replace(string.Format("({0})", indexer), string.Format("({0})", ++indexer)); } - else - { - if (!string.IsNullOrWhiteSpace(item.Name) && !NameExists(collection, item, item.Name)) - { - return; - } - string pattern = string.IsNullOrWhiteSpace(item.Name) ? "{0}" : item.Name + " ({0})"; - var index = 1; - while (NameExists(collection, item, string.Format(pattern, index))) - { - ++index; - } - - item.Name = string.Format(pattern, index); - } + item.Name = newName; } - - public static string ProvideNewUniqueName(IList collection, string item, Func translator = null) + else { - if (string.IsNullOrEmpty(item)) + if (!string.IsNullOrWhiteSpace(item.Name) && !NameExists(collection, item, item.Name)) { - Type type = collection.GetType().GetGenericArgumentsFromFirstGenericSuperClass()[0]; - item = translator == null ? type.Name : translator(type.Name); + return; } - if (string.IsNullOrWhiteSpace(item) || NameExists(collection, item)) + string pattern = string.IsNullOrWhiteSpace(item.Name) ? "{0}" : item.Name + " ({0})"; + var index = 1; + while (NameExists(collection, item, string.Format(pattern, index))) { - string format = string.IsNullOrWhiteSpace(item) ? "{0}" : item + " ({0})"; - var num = 0; - for (item = string.Format(format, num); NameExists(collection, item); item = string.Format(format, num)) - { - ++num; - } + ++index; } - return item; + item.Name = string.Format(pattern, index); } + } - private static bool NameContainsIndexer(string originalName, out int indexer) + public static string ProvideNewUniqueName(IList collection, string item, Func translator = null) + { + if (string.IsNullOrEmpty(item)) { - indexer = -1; - if (string.IsNullOrEmpty(originalName)) - { - return false; - } + Type type = collection.GetType().GetGenericArgumentsFromFirstGenericSuperClass()[0]; + item = translator == null ? type.Name : translator(type.Name); + } - string str = originalName.TrimEnd(); - int num1 = str.LastIndexOf("(", StringComparison.Ordinal); - int num2 = str.LastIndexOf(")", StringComparison.Ordinal); - if (num1 < 0 || num2 < 0 || num1 > num2) + if (string.IsNullOrWhiteSpace(item) || NameExists(collection, item)) + { + string format = string.IsNullOrWhiteSpace(item) ? "{0}" : item + " ({0})"; + var num = 0; + for (item = string.Format(format, num); NameExists(collection, item); item = string.Format(format, num)) { - return false; + ++num; } - - int startIndex = num1 + 1; - return int.TryParse(str.Substring(startIndex, num2 - startIndex), out indexer); } - private static bool NameExists(IList list, IName excludedItem, string newName) + return item; + } + + private static bool NameContainsIndexer(string originalName, out int indexer) + { + indexer = -1; + if (string.IsNullOrEmpty(originalName)) { - foreach (IName name in list) - { - if (name != excludedItem && name.Name == newName) - { - return true; - } - } + return false; + } + string str = originalName.TrimEnd(); + int num1 = str.LastIndexOf("(", StringComparison.Ordinal); + int num2 = str.LastIndexOf(")", StringComparison.Ordinal); + if (num1 < 0 || num2 < 0 || num1 > num2) + { return false; } - private static bool NameExists(IList list, string excludedItem) + int startIndex = num1 + 1; + return int.TryParse(str.Substring(startIndex, num2 - startIndex), out indexer); + } + + private static bool NameExists(IList list, IName excludedItem, string newName) + { + foreach (IName name in list) { - foreach (IName name in list) + if (name != excludedItem && name.Name == newName) { - if (name.Name == excludedItem) - { - return true; - } + return true; } + } - return false; + return false; + } + + private static bool NameExists(IList list, string excludedItem) + { + foreach (IName name in list) + { + if (name.Name == excludedItem) + { + return true; + } } + + return false; } } \ No newline at end of file