/Lib/JsonDotNetUnity/Scripts/JsonNetUnity/Utilities/DictionaryWrapper.cs

https://gitlab.com/sheetanshusrivastava3/Unity3D-code-repository-design-patterns
C# | 400 lines | 356 code | 44 blank | 0 comment | 62 complexity | 09e1028a3e4178664ceb3c1ba503d943 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Collections;
  6. using System.Threading;
  7. namespace Newtonsoft.Json.Utilities
  8. {
  9. internal interface IWrappedDictionary : IDictionary
  10. {
  11. object UnderlyingDictionary { get; }
  12. }
  13. internal class DictionaryWrapper<TKey, TValue> : IDictionary<TKey, TValue>, IWrappedDictionary
  14. {
  15. private readonly IDictionary _dictionary;
  16. private readonly IDictionary<TKey, TValue> _genericDictionary;
  17. private object _syncRoot;
  18. public DictionaryWrapper(IDictionary dictionary)
  19. {
  20. ValidationUtils.ArgumentNotNull(dictionary, "dictionary");
  21. _dictionary = dictionary;
  22. }
  23. public DictionaryWrapper(IDictionary<TKey, TValue> dictionary)
  24. {
  25. ValidationUtils.ArgumentNotNull(dictionary, "dictionary");
  26. _genericDictionary = dictionary;
  27. }
  28. public void Add(TKey key, TValue value)
  29. {
  30. if (_genericDictionary != null)
  31. _genericDictionary.Add(key, value);
  32. else
  33. _dictionary.Add(key, value);
  34. }
  35. public bool ContainsKey(TKey key)
  36. {
  37. if (_genericDictionary != null)
  38. return _genericDictionary.ContainsKey(key);
  39. else
  40. return _dictionary.Contains(key);
  41. }
  42. public ICollection<TKey> Keys
  43. {
  44. get
  45. {
  46. if (_genericDictionary != null)
  47. return _genericDictionary.Keys;
  48. else
  49. return _dictionary.Keys.Cast<TKey>().ToList();
  50. }
  51. }
  52. public bool Remove(TKey key)
  53. {
  54. if (_genericDictionary != null)
  55. {
  56. return _genericDictionary.Remove(key);
  57. }
  58. else
  59. {
  60. if (_dictionary.Contains(key))
  61. {
  62. _dictionary.Remove(key);
  63. return true;
  64. }
  65. else
  66. {
  67. return false;
  68. }
  69. }
  70. }
  71. public bool TryGetValue(TKey key, out TValue value)
  72. {
  73. if (_genericDictionary != null)
  74. {
  75. return _genericDictionary.TryGetValue(key, out value);
  76. }
  77. else
  78. {
  79. if (!_dictionary.Contains(key))
  80. {
  81. value = default(TValue);
  82. return false;
  83. }
  84. else
  85. {
  86. value = (TValue)_dictionary[key];
  87. return true;
  88. }
  89. }
  90. }
  91. public ICollection<TValue> Values
  92. {
  93. get
  94. {
  95. if (_genericDictionary != null)
  96. return _genericDictionary.Values;
  97. else
  98. return _dictionary.Values.Cast<TValue>().ToList();
  99. }
  100. }
  101. public TValue this[TKey key]
  102. {
  103. get
  104. {
  105. if (_genericDictionary != null)
  106. return _genericDictionary[key];
  107. else
  108. return (TValue)_dictionary[key];
  109. }
  110. set
  111. {
  112. if (_genericDictionary != null)
  113. _genericDictionary[key] = value;
  114. else
  115. _dictionary[key] = value;
  116. }
  117. }
  118. public void Add(KeyValuePair<TKey, TValue> item)
  119. {
  120. if (_genericDictionary != null)
  121. _genericDictionary.Add(item);
  122. else
  123. ((IList)_dictionary).Add(item);
  124. }
  125. public void Clear()
  126. {
  127. if (_genericDictionary != null)
  128. _genericDictionary.Clear();
  129. else
  130. _dictionary.Clear();
  131. }
  132. public bool Contains(KeyValuePair<TKey, TValue> item)
  133. {
  134. if (_genericDictionary != null)
  135. return _genericDictionary.Contains(item);
  136. else
  137. return ((IList)_dictionary).Contains(item);
  138. }
  139. public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
  140. {
  141. if (_genericDictionary != null)
  142. {
  143. _genericDictionary.CopyTo(array, arrayIndex);
  144. }
  145. else
  146. {
  147. foreach (DictionaryEntry item in _dictionary)
  148. {
  149. array[arrayIndex++] = new KeyValuePair<TKey, TValue>((TKey)item.Key, (TValue)item.Value);
  150. }
  151. }
  152. }
  153. public int Count
  154. {
  155. get
  156. {
  157. if (_genericDictionary != null)
  158. return _genericDictionary.Count;
  159. else
  160. return _dictionary.Count;
  161. }
  162. }
  163. public bool IsReadOnly
  164. {
  165. get
  166. {
  167. if (_genericDictionary != null)
  168. return _genericDictionary.IsReadOnly;
  169. else
  170. return _dictionary.IsReadOnly;
  171. }
  172. }
  173. public bool Remove(KeyValuePair<TKey, TValue> item)
  174. {
  175. if (_genericDictionary != null)
  176. {
  177. return _genericDictionary.Remove(item);
  178. }
  179. else
  180. {
  181. if (_dictionary.Contains(item.Key))
  182. {
  183. object value = _dictionary[item.Key];
  184. if (object.Equals(value, item.Value))
  185. {
  186. _dictionary.Remove(item.Key);
  187. return true;
  188. }
  189. else
  190. {
  191. return false;
  192. }
  193. }
  194. else
  195. {
  196. return true;
  197. }
  198. }
  199. }
  200. public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
  201. {
  202. if (_genericDictionary != null)
  203. return _genericDictionary.GetEnumerator();
  204. else
  205. return _dictionary.Cast<DictionaryEntry>().Select(de => new KeyValuePair<TKey, TValue>((TKey)de.Key, (TValue)de.Value)).GetEnumerator();
  206. }
  207. IEnumerator IEnumerable.GetEnumerator()
  208. {
  209. return GetEnumerator();
  210. }
  211. void IDictionary.Add(object key, object value)
  212. {
  213. if (_genericDictionary != null)
  214. _genericDictionary.Add((TKey)key, (TValue)value);
  215. else
  216. _dictionary.Add(key, value);
  217. }
  218. bool IDictionary.Contains(object key)
  219. {
  220. if (_genericDictionary != null)
  221. return _genericDictionary.ContainsKey((TKey)key);
  222. else
  223. return _dictionary.Contains(key);
  224. }
  225. private struct DictionaryEnumerator<TEnumeratorKey, TEnumeratorValue> : IDictionaryEnumerator
  226. {
  227. private readonly IEnumerator<KeyValuePair<TEnumeratorKey, TEnumeratorValue>> _e;
  228. public DictionaryEnumerator(IEnumerator<KeyValuePair<TEnumeratorKey, TEnumeratorValue>> e)
  229. {
  230. ValidationUtils.ArgumentNotNull(e, "e");
  231. _e = e;
  232. }
  233. public DictionaryEntry Entry
  234. {
  235. get { return (DictionaryEntry)Current; }
  236. }
  237. public object Key
  238. {
  239. get { return Entry.Key; }
  240. }
  241. public object Value
  242. {
  243. get { return Entry.Value; }
  244. }
  245. public object Current
  246. {
  247. get { return new DictionaryEntry(_e.Current.Key, _e.Current.Value); }
  248. }
  249. public bool MoveNext()
  250. {
  251. return _e.MoveNext();
  252. }
  253. public void Reset()
  254. {
  255. _e.Reset();
  256. }
  257. }
  258. IDictionaryEnumerator IDictionary.GetEnumerator()
  259. {
  260. if (_genericDictionary != null)
  261. return new DictionaryEnumerator<TKey, TValue>(_genericDictionary.GetEnumerator());
  262. else
  263. return _dictionary.GetEnumerator();
  264. }
  265. bool IDictionary.IsFixedSize
  266. {
  267. get
  268. {
  269. if (_genericDictionary != null)
  270. return false;
  271. else
  272. return _dictionary.IsFixedSize;
  273. }
  274. }
  275. ICollection IDictionary.Keys
  276. {
  277. get
  278. {
  279. if (_genericDictionary != null)
  280. return _genericDictionary.Keys.ToList();
  281. else
  282. return _dictionary.Keys;
  283. }
  284. }
  285. public void Remove(object key)
  286. {
  287. if (_genericDictionary != null)
  288. _genericDictionary.Remove((TKey)key);
  289. else
  290. _dictionary.Remove(key);
  291. }
  292. ICollection IDictionary.Values
  293. {
  294. get
  295. {
  296. if (_genericDictionary != null)
  297. return _genericDictionary.Values.ToList();
  298. else
  299. return _dictionary.Values;
  300. }
  301. }
  302. object IDictionary.this[object key]
  303. {
  304. get
  305. {
  306. if (_genericDictionary != null)
  307. return _genericDictionary[(TKey)key];
  308. else
  309. return _dictionary[key];
  310. }
  311. set
  312. {
  313. if (_genericDictionary != null)
  314. _genericDictionary[(TKey)key] = (TValue)value;
  315. else
  316. _dictionary[key] = value;
  317. }
  318. }
  319. void ICollection.CopyTo(Array array, int index)
  320. {
  321. if (_genericDictionary != null)
  322. _genericDictionary.CopyTo((KeyValuePair<TKey, TValue>[])array, index);
  323. else
  324. _dictionary.CopyTo(array, index);
  325. }
  326. bool ICollection.IsSynchronized
  327. {
  328. get
  329. {
  330. if (_genericDictionary != null)
  331. return false;
  332. else
  333. return _dictionary.IsSynchronized;
  334. }
  335. }
  336. object ICollection.SyncRoot
  337. {
  338. get
  339. {
  340. if (_syncRoot == null)
  341. Interlocked.CompareExchange(ref _syncRoot, new object(), null);
  342. return _syncRoot;
  343. }
  344. }
  345. public object UnderlyingDictionary
  346. {
  347. get
  348. {
  349. if (_genericDictionary != null)
  350. return _genericDictionary;
  351. else
  352. return _dictionary;
  353. }
  354. }
  355. }
  356. }