From bd9634e58468389e7a94e22dc09ea969f3a80667 Mon Sep 17 00:00:00 2001 From: xodial Date: Sun, 16 Feb 2025 15:32:26 -0600 Subject: [PATCH] entity-table: set now overwrites, TryAdd added for migration path --- .../src/EntityTableTest.cs | 14 +++++++++++++- .../collections/entity_table/EntityTableOfTId.cs | 10 +++++++++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/Chickensoft.Collections.Tests/src/EntityTableTest.cs b/Chickensoft.Collections.Tests/src/EntityTableTest.cs index aba5d79..378cb5d 100644 --- a/Chickensoft.Collections.Tests/src/EntityTableTest.cs +++ b/Chickensoft.Collections.Tests/src/EntityTableTest.cs @@ -11,7 +11,7 @@ public void Initializes() { } [Fact] - public void StoresValues() { + public void SetStoresValuesAndOverwritesExistingValues() { var table = new EntityTable(); table.Set("a", "one"); @@ -20,6 +20,9 @@ public void StoresValues() { table.Get("a").ShouldBe("one"); table.Get("b").ShouldNotBeNull(); + table.Set("a", "two"); + table.Get("a").ShouldBe("two"); + table.Remove("a"); table.Remove(null); @@ -27,6 +30,15 @@ public void StoresValues() { table.Get("b").ShouldNotBeNull(); } + [Fact] + public void TryAddOnlyStoresValuesForNewKeys() { + var table = new EntityTable(); + + table.TryAdd("a", "one").ShouldBeTrue(); + table.TryAdd("a", "two").ShouldBeFalse(); + table.Get("a").ShouldBe("one"); + } + [Fact] public void Clears() { var table = new EntityTable(); diff --git a/Chickensoft.Collections/src/collections/entity_table/EntityTableOfTId.cs b/Chickensoft.Collections/src/collections/entity_table/EntityTableOfTId.cs index 6ea6d06..ff58a71 100644 --- a/Chickensoft.Collections/src/collections/entity_table/EntityTableOfTId.cs +++ b/Chickensoft.Collections/src/collections/entity_table/EntityTableOfTId.cs @@ -16,7 +16,15 @@ public class EntityTable where TId : notnull { /// /// Entity id. /// Entity object. - public void Set(TId id, object entity) => _entities.TryAdd(id, entity); + public void Set(TId id, object entity) => _entities[id] = entity; + + /// + /// Attempts to add an entity to the table returning true if successful. + /// + /// Entity id. + /// Entity object. + /// `true` if the entity was added, `false` otherwise. + public bool TryAdd(TId id, object entity) => _entities.TryAdd(id, entity); /// /// Remove an entity from the table.