@@ -12,15 +12,32 @@ namespace EppNet.Data
1212
1313 /// <summary>
1414 /// Handy interface that allows external code to associate data with a
15- /// specific instance
15+ /// specific instance. Generic functions are here simply for convenience and do not imply non-boxing.<br/>
16+ /// Boxing will occur no matter what function you elect to use.
1617 /// </summary>
1718
1819 public interface IDataHolder : INodeDescendant
1920 {
2021
22+ /// <summary>
23+ /// Maps the specified key to the specified value
24+ /// </summary>
25+ /// <typeparam name="T"></typeparam>
26+ /// <param name="key">A non-null non-empty string</param>
27+ /// <param name="value">The value to associate with the key. Null is permitted</param>
28+ /// <returns>Whether or not the data was mapped</returns>
29+
2130 public bool Set < T > ( [ NotNull ] string key , T value ) =>
2231 Node . DataStorage . AddOrUpdateCustomDataEntry ( this , key , value ) ;
2332
33+ /// <summary>
34+ /// Tries to map the specified key to the specified value<br/>
35+ /// if the key hasn't been map to a value already.
36+ /// </summary>
37+ /// <typeparam name="T"></typeparam>
38+ /// <param name="key">A non-null non-empty string</param>
39+ /// <param name="value">The value to associate with the key. Null is permitted</param>
40+ /// <returns>Whether or not the data was mapped</returns>
2441 public bool SetIfAbsent < T > ( [ NotNull ] string key , T value )
2542 {
2643 if ( ! Has ( key ) )
@@ -29,37 +46,90 @@ public bool SetIfAbsent<T>([NotNull] string key, T value)
2946 return false ;
3047 }
3148
49+ /// <summary>
50+ /// Gets the value associated with the specified key and casts it to<br/>
51+ /// the specified type.<br/><br/>
52+ /// <strong>Careful!</strong> This assumes that the entry exists and the value can be casted!
53+ /// </summary>
54+ /// <typeparam name="T"></typeparam>
55+ /// <param name="key">A non-null non-empty string</param>
56+ /// <returns>The value associated with the specified key</returns>
57+
3258 public T Get < T > ( [ NotNull ] string key ) =>
3359 ( T ) Node . DataStorage . GetCustomDataEntry ( this , key ) ;
3460
61+ /// <summary>
62+ /// Gets the value associated with the specified key
63+ /// </summary>
64+ /// <param name="key">A non-null non-empty string</param>
65+ /// <returns>The value associated with the specified key</returns>
3566 public object Get ( [ NotNull ] string key ) =>
3667 Node . DataStorage . GetCustomDataEntry ( this , key ) ;
3768
69+ /// <summary>
70+ /// Tries to get the value mapped with the specified key -- if it can be cast to T. If not, <br/>
71+ /// it returns the specified fallback value.
72+ /// </summary>
73+ /// <param name="key">A non-null non-empty string</param>
74+ /// <param name="value">The value associated with this entry</param>
75+ /// <returns>The value mapped to the specified key or the fallback value</returns>
76+
3877 public T GetOrDefault < T > ( [ NotNull ] string key , T fallback ) =>
3978 TryGet ( key , out T result ) ? result : fallback ;
4079
80+ /// <summary>
81+ /// Tries to get the value mapped with the specified key -- if it can be cast to T.
82+ /// </summary>
83+ /// <param name="key">A non-null non-empty string</param>
84+ /// <param name="value">The value associated with this entry</param>
85+ /// <returns>Whether or not a value was fetched successfully</returns>
4186 public bool TryGet < T > ( [ NotNull ] string key , out T value )
4287 {
4388 bool fetched = Node . DataStorage . TryGetCustomDataEntry ( this , key , out object obj ) ;
44- value = default ;
4589
4690 if ( fetched && obj is T casted )
4791 {
4892 value = casted ;
4993 return true ;
5094 }
5195
96+ value = default ;
5297 return false ;
5398 }
5499
100+ /// <summary>
101+ /// Tries to get the value mapped to the specified key
102+ /// </summary>
103+ /// <param name="key">A non-null non-empty string</param>
104+ /// <param name="value">The value associated with this entry</param>
105+ /// <returns>Whether or not a value was fetched successfully</returns>
55106 public bool TryGet ( [ NotNull ] string key , out object value ) =>
56107 Node . DataStorage . TryGetCustomDataEntry ( this , key , out value ) ;
57108
109+ /// <summary>
110+ /// Removes the specified custom data entry. Returns the value (if it existed)
111+ /// </summary>
112+ /// <param name="key">A non-null non-entry string</param>
113+ /// <param name="value">The value associated with the removed key</param>
114+ /// <returns>Whether or not an entry was deleted</returns>
58115 public bool Remove ( [ NotNull ] string key , out object value ) =>
59116 Node . DataStorage . RemoveCustomDataEntry ( this , key , out value ) ;
60117
118+ /// <summary>
119+ /// Whether or not this <see cref="IDataHolder"/> contains an entry with the specified name.
120+ /// </summary>
121+ /// <param name="key">A non-null non-empty string</param>
122+ /// <returns></returns>
61123 public bool Has ( [ NotNull ] string key ) =>
62124 Node . DataStorage . TryGetCustomDataEntry ( this , key , out _ ) ;
63125
126+ /// <summary>
127+ /// Deletes all custom data associated with this <see cref="IDataHolder"/>
128+ /// </summary>
129+ /// <returns>Whether or not a custom data dictionary was deleted.</returns>
130+ public bool DeleteAll ( ) =>
131+ Node . DataStorage . DeleteCustomData ( this ) ;
132+
64133 }
134+
65135}
0 commit comments