MruPropertyCache2.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. namespace Jint.Runtime
  4. {
  5. public class MruPropertyCache2<TKey, TValue> : IDictionary<TKey, TValue>
  6. where TValue:class
  7. {
  8. private IDictionary<TKey, TValue> _dictionary = new Dictionary<TKey, TValue>();
  9. private bool _set;
  10. private TKey _key;
  11. private TValue _value;
  12. public MruPropertyCache2() {
  13. }
  14. public TValue this[TKey key] {
  15. get {
  16. if (_set && key.Equals(_key))
  17. {
  18. return _value;
  19. }
  20. return _dictionary[key];
  21. }
  22. set {
  23. _set = true;
  24. _key = key;
  25. _value = value;
  26. _dictionary[key] = value;
  27. }
  28. }
  29. public int Count {
  30. get {
  31. return _dictionary.Count;
  32. }
  33. }
  34. public bool IsReadOnly {
  35. get {
  36. return _dictionary.IsReadOnly;
  37. }
  38. }
  39. public ICollection<TKey> Keys {
  40. get {
  41. return _dictionary.Keys;
  42. }
  43. }
  44. public ICollection<TValue> Values {
  45. get {
  46. return _dictionary.Values;
  47. }
  48. }
  49. public void Add(KeyValuePair<TKey, TValue> item) {
  50. _set = true;
  51. _key = item.Key;
  52. _value = item.Value;
  53. _dictionary.Add(item);
  54. }
  55. public void Add(TKey key, TValue value) {
  56. _set = true;
  57. _key = key;
  58. _value = value;
  59. _dictionary.Add(key, value);
  60. }
  61. public void Clear() {
  62. _set = false;
  63. _key = default(TKey);
  64. _value = null;
  65. _dictionary.Clear();
  66. }
  67. public bool Contains(KeyValuePair<TKey, TValue> item) {
  68. if(_set && item.Key.Equals(_key))
  69. {
  70. return true;
  71. }
  72. return _dictionary.Contains(item);
  73. }
  74. public bool ContainsKey(TKey key) {
  75. if (_set && key.Equals(_key))
  76. {
  77. return true;
  78. }
  79. return _dictionary.ContainsKey(key);
  80. }
  81. public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex) {
  82. _dictionary.CopyTo(array, arrayIndex);
  83. }
  84. public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator() {
  85. return _dictionary.GetEnumerator();
  86. }
  87. public bool Remove(KeyValuePair<TKey, TValue> item) {
  88. if(_set && item.Key.Equals(_key))
  89. {
  90. _set = false;
  91. _key = default(TKey);
  92. _value = null;
  93. }
  94. return _dictionary.Remove(item);
  95. }
  96. public bool Remove(TKey key) {
  97. if (_set && key.Equals(_key))
  98. {
  99. _set = false;
  100. _key = default(TKey);
  101. _value = null;
  102. }
  103. return _dictionary.Remove(key);
  104. }
  105. public bool TryGetValue(TKey key, out TValue value) {
  106. if (_set && key.Equals(_key))
  107. {
  108. value = _value;
  109. return true;
  110. }
  111. return _dictionary.TryGetValue(key, out value);
  112. }
  113. IEnumerator IEnumerable.GetEnumerator() {
  114. return _dictionary.GetEnumerator();
  115. }
  116. }
  117. }