// Copyright (C) Stichting Deltares 2025. 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.Concurrent; using System.Collections.Generic; using System.Linq; namespace Deltares.DamEngine.Calculators.KernelWrappers.MacroStabilityCommon.MacroStabilityIo; /// /// Class providing methods to register objects. /// internal class ObjectRegistry { private readonly ConcurrentDictionary objects = new ConcurrentDictionary(); private int currentId; /// /// Gets an unique id of an object based on its instance. /// /// The object to get the id from. /// The id. /// Thrown when is null. public int GetId(object @object) { AddsObjectToRegistryIfNotExistsInRegistry(@object); return objects[@object]; } /// /// Gets an object from an id. /// /// The id to get the object from. /// The object of null if not exists. /// Thrown when is not valid. public object GetObject(int id) { return objects.First((Func, bool>) (x => x.Value == id)).Key; } private void AddsObjectToRegistryIfNotExistsInRegistry(object @object) { if (IsObjectInRegistry(@object)) { return; } AddObjectToRegistry(@object); } private bool IsObjectInRegistry(object @object) => objects.ContainsKey(@object); private void AddObjectToRegistry(object @object) { objects.AddOrUpdate(@object, currentId, (Func) ((o, i) => objects.Count + 1)); ++currentId; } }