// Copyright (C) Stichting Deltares 2018. All rights reserved. // // This file is part of the Dam Engine. // // The Dam Engine is free software: you can redistribute it and/or modify // it under the terms of the GNU Affero 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 Affero General Public License for more details. // // You should have received a copy of the GNU Affero 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; using System.Collections.Generic; namespace Deltares.DamEngine.Data.Standard { public class UniqueNameProvider { private static List collections = new List(); static UniqueNameProvider() { } public static void Clear() { UniqueNameProvider.collections.Clear(); } public static void Register(params IList[] lists) { foreach (IList list in lists) { if (!UniqueNameProvider.collections.Contains(list)) UniqueNameProvider.collections.Add(list); } } public static void ProvideUniqueName(IList collection, IName item, Func translator = null) { if (string.IsNullOrEmpty(item.Name)) { Type type = collection.GetType().GetGenericArgumentsFromFirstGenericSuperClass()[0]; item.Name = translator == null ? type.Name : translator(type.Name); } int indexer; if (UniqueNameProvider.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)); item.Name = newName; } else { if (!string.IsNullOrWhiteSpace(item.Name) && !UniqueNameProvider.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))) ++index; item.Name = string.Format(pattern, (object)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)) { 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)) ++num; } return item; } private static bool NameExists(IList list, IName excludedItem, string newName) { foreach (IName name in (IEnumerable)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) { if (name.Name == excludedItem) return true; } return false; } } }