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