diff --git a/Assets/BetterCommons/Runtime/Extensions/DictionaryExtensions.cs b/Assets/BetterCommons/Runtime/Extensions/DictionaryExtensions.cs new file mode 100644 index 0000000..0f4b921 --- /dev/null +++ b/Assets/BetterCommons/Runtime/Extensions/DictionaryExtensions.cs @@ -0,0 +1,97 @@ +using System; +using System.Collections.Generic; + +namespace Better.Commons.Runtime.Extensions +{ + public static class DictionaryExtensions + { + public static bool TryGetKey(this Dictionary self, TValue value, out TKey key) + { + if (self == null) + { + throw new ArgumentNullException(nameof(self)); + } + + foreach (var keyValuePair in self) + { + if (keyValuePair.Value.Equals(value)) + { + key = keyValuePair.Key; + return true; + } + } + + key = default; + return false; + } + + public static bool TryGetKeys(this Dictionary self, TValue value, out TKey[] keys) + { + if (self == null) + { + throw new ArgumentNullException(nameof(self)); + } + + var rawKeys = new List(); + foreach (var keyValuePair in self) + { + if (keyValuePair.Value.Equals(value)) + { + rawKeys.Add(keyValuePair.Key); + } + } + + keys = rawKeys.ToArray(); + return !keys.IsEmpty(); + } + + public static bool Remove(this Dictionary self, IEnumerable keys) + { + if (self == null) + { + throw new ArgumentNullException(nameof(self)); + } + + var removedAny = false; + foreach (var key in keys) + { + if (self.Remove(key)) + { + removedAny = true; + } + } + + return removedAny; + } + + public static bool Remove(this Dictionary self, TValue value, out TKey key) + { + if (self == null) + { + throw new ArgumentNullException(nameof(self)); + } + + return self.TryGetKey(value, out key) && self.Remove(key); + } + + public static bool Remove(this Dictionary self, TValue value) + { + return self.Remove(value, out _); + } + + public static bool RemoveAll(this Dictionary self, TValue value, out TKey[] keys) + { + if (self == null) + { + throw new ArgumentNullException(nameof(self)); + } + + return self.TryGetKeys(value, out keys) && self.Remove(keys); + } + + public static bool RemoveAll(this Dictionary self, TValue value) + { + return self.RemoveAll(value, out _); + } + } +} \ No newline at end of file diff --git a/Assets/BetterCommons/Runtime/Extensions/DictionaryExtensions.cs.meta b/Assets/BetterCommons/Runtime/Extensions/DictionaryExtensions.cs.meta new file mode 100644 index 0000000..450365d --- /dev/null +++ b/Assets/BetterCommons/Runtime/Extensions/DictionaryExtensions.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 41a94e77cae44269bbd65b894eec6314 +timeCreated: 1715971284 \ No newline at end of file diff --git a/Assets/BetterCommons/Runtime/Extensions/StringBuilderExtensions.cs b/Assets/BetterCommons/Runtime/Extensions/StringBuilderExtensions.cs index cfaf036..b1bffb2 100644 --- a/Assets/BetterCommons/Runtime/Extensions/StringBuilderExtensions.cs +++ b/Assets/BetterCommons/Runtime/Extensions/StringBuilderExtensions.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Text; using Better.Commons.Runtime.Utility; @@ -6,6 +7,8 @@ namespace Better.Commons.Runtime.Extensions { public static class StringBuilderExtensions { + private const string InfoFieldSeparator = ": "; + public static StringBuilder AppendLine(this StringBuilder self, object value) { if (self == null) @@ -22,5 +25,248 @@ public static StringBuilder AppendLine(this StringBuilder self, object value) return self.AppendLine(value.ToString()); } + + public static StringBuilder AppendFormatLine(this StringBuilder self, IFormatProvider provider, string format, object arg0) + { + if (self == null) + { + DebugUtility.LogException(nameof(self)); + return null; + } + + if (provider == null) + { + DebugUtility.LogException(nameof(self)); + return null; + } + + return self.AppendFormat(provider, format, arg0).AppendLine(); + } + + public static StringBuilder AppendFormatLine(this StringBuilder self, IFormatProvider provider, string format, object arg0, object arg1) + { + if (self == null) + { + DebugUtility.LogException(nameof(self)); + return null; + } + + if (provider == null) + { + DebugUtility.LogException(nameof(self)); + return null; + } + + return self.AppendFormat(provider, format, arg0, arg1).AppendLine(); + } + + public static StringBuilder AppendFormatLine(this StringBuilder self, IFormatProvider provider, string format, object arg0, object arg1, object arg2) + { + if (self == null) + { + DebugUtility.LogException(nameof(self)); + return null; + } + + if (provider == null) + { + DebugUtility.LogException(nameof(self)); + return null; + } + + return self.AppendFormat(provider, format, arg0, arg1, arg2).AppendLine(); + } + + public static StringBuilder AppendFormatLine(this StringBuilder self, IFormatProvider provider, string format, params object[] args) + { + if (self == null) + { + DebugUtility.LogException(nameof(self)); + return null; + } + + if (provider == null) + { + DebugUtility.LogException(nameof(self)); + return null; + } + + return self.AppendFormat(provider, format, args).AppendLine(); + } + + public static StringBuilder AppendFormatLine(this StringBuilder self, string format, object arg0) + { + if (self == null) + { + DebugUtility.LogException(nameof(self)); + return null; + } + + return self.AppendFormat(format, arg0).AppendLine(); + } + + public static StringBuilder AppendFormatLine(this StringBuilder self, string format, object arg0, object arg1) + { + if (self == null) + { + DebugUtility.LogException(nameof(self)); + return null; + } + + return self.AppendFormat(format, arg0, arg1).AppendLine(); + } + + public static StringBuilder AppendFormatLine(this StringBuilder self, string format, object arg0, object arg1, object arg2) + { + if (self == null) + { + DebugUtility.LogException(nameof(self)); + return null; + } + + return self.AppendFormat(format, arg0, arg1, arg2).AppendLine(); + } + + public static StringBuilder AppendFormatLine(this StringBuilder self, string format, params object[] args) + { + if (self == null) + { + DebugUtility.LogException(nameof(self)); + return null; + } + + if (args == null) + { + DebugUtility.LogException(nameof(self)); + return null; + } + + return self.AppendFormat(format, args).AppendLine(); + } + + public static StringBuilder AppendJoinLine(this StringBuilder self, char separator, params object[] values) + { + if (self == null) + { + DebugUtility.LogException(nameof(self)); + return null; + } + + if (values == null) + { + DebugUtility.LogException(nameof(self)); + return null; + } + + return self.AppendJoin(separator, values).AppendLine(); + } + + public static StringBuilder AppendJoinLine(this StringBuilder self, char separator, params string[] values) + { + if (self == null) + { + DebugUtility.LogException(nameof(self)); + return null; + } + + if (values == null) + { + DebugUtility.LogException(nameof(self)); + return null; + } + + return self.AppendJoin(separator, values).AppendLine(); + } + + public static StringBuilder AppendJoinLine(this StringBuilder self, string separator, params object[] values) + { + if (self == null) + { + DebugUtility.LogException(nameof(self)); + return null; + } + + if (values == null) + { + DebugUtility.LogException(nameof(self)); + return null; + } + + return self.AppendJoin(separator, values).AppendLine(); + } + + public static StringBuilder AppendJoinLine(this StringBuilder self, string separator, params string[] values) + { + if (self == null) + { + DebugUtility.LogException(nameof(self)); + return null; + } + + if (values == null) + { + DebugUtility.LogException(nameof(self)); + return null; + } + + return self.AppendJoin(separator, values).AppendLine(); + } + + public static StringBuilder AppendJoinLine(this StringBuilder self, char separator, IEnumerable values) + { + if (self == null) + { + DebugUtility.LogException(nameof(self)); + return null; + } + + if (values == null) + { + DebugUtility.LogException(nameof(self)); + return null; + } + + return self.AppendJoin(separator, values).AppendLine(); + } + + public static StringBuilder AppendJoinLine(this StringBuilder self, string separator, IEnumerable values) + { + if (self == null) + { + DebugUtility.LogException(nameof(self)); + return null; + } + + if (values == null) + { + DebugUtility.LogException(nameof(self)); + return null; + } + + return self.AppendJoin(separator, values).AppendLine(); + } + + public static StringBuilder AppendField(this StringBuilder self, string fieldName, object fieldValue) + { + if (self == null) + { + DebugUtility.LogException(nameof(self)); + return null; + } + + self.AppendJoin(InfoFieldSeparator, fieldName, fieldValue); + return self; + } + + public static StringBuilder AppendFieldLine(this StringBuilder self, string fieldName, object fieldValue) + { + if (self == null) + { + DebugUtility.LogException(nameof(self)); + return null; + } + + return self.AppendField(fieldName, fieldValue).AppendLine(); + } } } \ No newline at end of file diff --git a/Assets/BetterCommons/Runtime/Helpers/CompletionAwaiters/CompletionAwaiter.cs b/Assets/BetterCommons/Runtime/Helpers/CompletionAwaiters/CompletionAwaiter.cs index 4876478..bd50541 100644 --- a/Assets/BetterCommons/Runtime/Helpers/CompletionAwaiters/CompletionAwaiter.cs +++ b/Assets/BetterCommons/Runtime/Helpers/CompletionAwaiters/CompletionAwaiter.cs @@ -31,7 +31,7 @@ public CompletionAwaiter(CancellationToken cancellationToken) protected void SetResult(TValue value) { - if (!_completionSource.TrySetResult(value)) + if (_completionSource.TrySetResult(value)) { OnCompleted(value); } diff --git a/Assets/BetterCommons/package.json b/Assets/BetterCommons/package.json index 8ce6636..ac3ceb4 100644 --- a/Assets/BetterCommons/package.json +++ b/Assets/BetterCommons/package.json @@ -1,7 +1,7 @@ { "name": "com.tdw.better.commons", "displayName": "Better Commons", - "version": "0.0.6", + "version": "0.0.7", "unity": "2021.3", "description": " ", "dependencies": {