StorageUtil.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. using System;
  2. using System.Collections.Generic;
  3. internal static class StorageUtil
  4. {
  5. public static bool Contain(Dictionary<string, int> storageDict, string key)
  6. {
  7. return storageDict.ContainsKey(key);
  8. }
  9. public static void Set(Dictionary<string, int> storageDict, string id, bool value)
  10. {
  11. if (string.IsNullOrEmpty(id))
  12. {
  13. throw new ArgumentNullException("id");
  14. }
  15. if (!storageDict.ContainsKey(id))
  16. {
  17. storageDict.Add(id, (!value) ? 0 : 1);
  18. }
  19. else
  20. {
  21. storageDict[id] = ((!value) ? 0 : 1);
  22. }
  23. }
  24. public static void Set(Dictionary<string, int> storageDict, string id, int value)
  25. {
  26. if (string.IsNullOrEmpty(id))
  27. {
  28. throw new ArgumentNullException("id");
  29. }
  30. if (!storageDict.ContainsKey(id))
  31. {
  32. storageDict.Add(id, value);
  33. }
  34. else
  35. {
  36. storageDict[id] = value;
  37. }
  38. }
  39. public static bool Get(Dictionary<string, int> storageDict, string id, bool defaultValue)
  40. {
  41. if (storageDict.ContainsKey(id))
  42. {
  43. return storageDict[id] == 1;
  44. }
  45. return defaultValue;
  46. }
  47. public static int Get(Dictionary<string, int> storageDict, string id, int defaultValue)
  48. {
  49. if (storageDict.ContainsKey(id))
  50. {
  51. return storageDict[id];
  52. }
  53. return defaultValue;
  54. }
  55. public static bool GetBool(Dictionary<string, int> storageDict, string id)
  56. {
  57. if (storageDict.ContainsKey(id))
  58. {
  59. return storageDict[id] == 1;
  60. }
  61. throw new KeyNotFoundException(id + " is not exist");
  62. }
  63. public static int Get(Dictionary<string, int> storageDict, string id)
  64. {
  65. if (storageDict.ContainsKey(id))
  66. {
  67. return storageDict[id];
  68. }
  69. throw new KeyNotFoundException(id + " is not exist");
  70. }
  71. public static void Clear(Dictionary<string, int> storageDict)
  72. {
  73. storageDict.Clear();
  74. }
  75. }