From 1f40129ad3715e67cd67565514f1b699d30674f2 Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Fri, 18 Dec 2020 16:39:52 +0100 Subject: [PATCH] extract GenericDataHolder behavior into an interface --- MLEM/Misc/GenericDataHolder.cs | 56 +++++++++++++++++++++------------- 1 file changed, 35 insertions(+), 21 deletions(-) diff --git a/MLEM/Misc/GenericDataHolder.cs b/MLEM/Misc/GenericDataHolder.cs index 2bd1b22..c9bdd64 100644 --- a/MLEM/Misc/GenericDataHolder.cs +++ b/MLEM/Misc/GenericDataHolder.cs @@ -3,21 +3,14 @@ using System.Collections.Generic; using System.Runtime.Serialization; namespace MLEM.Misc { - /// - /// Represents an object that can hold generic key-value based data. - /// A lot of MLEM components extend this class to allow for users to add additional data to them easily. - /// + /// [DataContract] - public class GenericDataHolder { + public class GenericDataHolder : IGenericDataHolder { - [DataMember] + [DataMember(EmitDefaultValue = false)] private Dictionary data; - /// - /// Store a piece of generic data on this object. - /// - /// The key to store the data by - /// The data to store in the object + /// public void SetData(string key, object data) { if (data == default) { if (this.data != null) @@ -29,22 +22,14 @@ namespace MLEM.Misc { } } - /// - /// Returns a piece of generic data of the given type on this object. - /// - /// The key that the data is stored by - /// The type of the data stored - /// The data, or default if it doesn't exist + /// public T GetData(string key) { if (this.data != null && this.data.TryGetValue(key, out var val) && val is T t) return t; return default; } - /// - /// Returns all of the generic data that this object stores. - /// - /// The generic data on this object + /// public IReadOnlyCollection GetDataKeys() { if (this.data == null) return Array.Empty(); @@ -52,4 +37,33 @@ namespace MLEM.Misc { } } + + /// + /// Represents an object that can hold generic key-value based data. + /// A lot of MLEM components extend this class to allow for users to add additional data to them easily. + /// + public interface IGenericDataHolder { + + /// + /// Store a piece of generic data on this object. + /// + /// The key to store the data by + /// The data to store in the object + void SetData(string key, object data); + + /// + /// Returns a piece of generic data of the given type on this object. + /// + /// The key that the data is stored by + /// The type of the data stored + /// The data, or default if it doesn't exist + T GetData(string key); + + /// + /// Returns all of the generic data that this object stores. + /// + /// The generic data on this object + IReadOnlyCollection GetDataKeys(); + + } } \ No newline at end of file