Index: DamEngine/trunk/src/Deltares.DamEngine.Calculators.Tests/KernelWrappers/MacroStabilityCommon/MacroStabilityIo/ObjectRegistryTest.cs =================================================================== diff -u --- DamEngine/trunk/src/Deltares.DamEngine.Calculators.Tests/KernelWrappers/MacroStabilityCommon/MacroStabilityIo/ObjectRegistryTest.cs (revision 0) +++ DamEngine/trunk/src/Deltares.DamEngine.Calculators.Tests/KernelWrappers/MacroStabilityCommon/MacroStabilityIo/ObjectRegistryTest.cs (revision 6367) @@ -0,0 +1,98 @@ +// Copyright (C) Stichting Deltares 2024. 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 Deltares.DamEngine.Calculators.KernelWrappers.MacroStabilityCommon.MacroStabilityIo; +using NUnit.Framework; + +namespace Deltares.DamEngine.Calculators.Tests.KernelWrappers.MacroStabilityCommon.MacroStabilityIo; + +[TestFixture] +public class ObjectRegistryTest +{ + [TestFixture] + public class GivenObjectRegistry + { + private static ObjectRegistry objectRegistry; + + [SetUp] + public static void Arrange() + { + objectRegistry = new ObjectRegistry(); + } + + [Test] + public static void WhenGetObjectFromNonExistingIndex_ThenThrowsInvalidOperationException() + { + Assert.That(() => objectRegistry.GetObject(0), Throws.InstanceOf()); + } + + [Test] + public static void WhenGetIdWithoutObject_ThenThrowsArgumentNullException() + { + Assert.That(() => objectRegistry.GetId(null), Throws.ArgumentNullException); + } + + [TestFixture] + public class WhenGetId : GivenObjectRegistry + { + private static int index; + private static object @object; + + [SetUp] + public new static void Arrange() + { + @object = new object(); + index = objectRegistry.GetId(@object); + } + + [Test] + public static void ThenIndexIsZero() + { + Assert.That(index, Is.Zero); + } + + [Test] + public static void WhenGetObjectUsingReceivedIndex_ThenObjectIsTheSame() + { + object receivedObject = objectRegistry.GetObject(index); + + Assert.That(receivedObject, Is.SameAs(@object)); + } + + [Test] + public static void WhenGetIdFromOtherObject_ThenReturnsIndexIncreasedWithOne() + { + int newIndex = objectRegistry.GetId(new object()); + + Assert.That(newIndex, Is.EqualTo(index + 1)); + } + + [Test] + public static void WhenGetIdFromSameObject_ThenReturnsIndex() + { + int newIndex = objectRegistry.GetId(@object); + + Assert.That(newIndex, Is.EqualTo(index)); + } + } + } +} \ No newline at end of file Index: DamEngine/trunk/src/Deltares.DamEngine.Calculators/KernelWrappers/MacroStabilityCommon/MacroStabilityIo/FillMacroStabilityInterfaceInputFromEngine.cs =================================================================== diff -u -r6361 -r6367 --- DamEngine/trunk/src/Deltares.DamEngine.Calculators/KernelWrappers/MacroStabilityCommon/MacroStabilityIo/FillMacroStabilityInterfaceInputFromEngine.cs (.../FillMacroStabilityInterfaceInputFromEngine.cs) (revision 6361) +++ DamEngine/trunk/src/Deltares.DamEngine.Calculators/KernelWrappers/MacroStabilityCommon/MacroStabilityIo/FillMacroStabilityInterfaceInputFromEngine.cs (.../FillMacroStabilityInterfaceInputFromEngine.cs) (revision 6367) @@ -47,38 +47,6 @@ namespace Deltares.DamEngine.Calculators.KernelWrappers.MacroStabilityCommon.MacroStabilityIo; -internal class ObjectRegistry -{ - private readonly ConcurrentDictionary objects = new ConcurrentDictionary(); - private int currentId; - - public int GetId(object @object) - { - AddsObjectToRegistryIfNotExistsInRegistry(@object); - return objects[@object]; - } - - 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) => this.objects.ContainsKey(@object); - - private void AddObjectToRegistry(object @object) - { - objects.AddOrUpdate(@object, currentId, (Func) ((o, i) => objects.Count + 1)); - ++currentId; - } -} - public class FillMacroStabilityInterfaceInputFromEngine { /// Gets or sets the UpliftVan calculation grid. Index: DamEngine/trunk/src/Deltares.DamEngine.Calculators/KernelWrappers/MacroStabilityCommon/MacroStabilityIo/ObjectRegistry.cs =================================================================== diff -u --- DamEngine/trunk/src/Deltares.DamEngine.Calculators/KernelWrappers/MacroStabilityCommon/MacroStabilityIo/ObjectRegistry.cs (revision 0) +++ DamEngine/trunk/src/Deltares.DamEngine.Calculators/KernelWrappers/MacroStabilityCommon/MacroStabilityIo/ObjectRegistry.cs (revision 6367) @@ -0,0 +1,75 @@ +// Copyright (C) Stichting Deltares 2024. 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) => this.objects.ContainsKey(@object); + + private void AddObjectToRegistry(object @object) + { + objects.AddOrUpdate(@object, currentId, (Func) ((o, i) => objects.Count + 1)); + ++currentId; + } +} +