AggregateDictionary.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. //
  2. // System.Runtime.Remoting.Channels.AggregateDictionary.cs
  3. //
  4. // Author: Lluis Sanchez Gual ([email protected])
  5. //
  6. // 2002 (C) Copyright, Novell, Inc.
  7. //
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System.Collections;
  29. namespace System.Runtime.Remoting.Channels.Http
  30. {
  31. internal class AggregateDictionary: IDictionary
  32. {
  33. IDictionary[] dictionaries;
  34. ArrayList _values;
  35. ArrayList _keys;
  36. public AggregateDictionary (IDictionary[] dics)
  37. {
  38. dictionaries = dics;
  39. }
  40. public bool IsFixedSize
  41. {
  42. get { return true; }
  43. }
  44. public bool IsReadOnly
  45. {
  46. get { return true; }
  47. }
  48. public object this [object key]
  49. {
  50. get
  51. {
  52. foreach (IDictionary dic in dictionaries)
  53. if (dic.Contains (key)) return dic [key];
  54. return null;
  55. }
  56. set
  57. {
  58. throw new NotSupportedException ();
  59. }
  60. }
  61. public ICollection Keys
  62. {
  63. get
  64. {
  65. if (_keys != null) return _keys;
  66. _keys = new ArrayList ();
  67. foreach (IDictionary dic in dictionaries)
  68. _keys.AddRange (dic.Keys);
  69. return _keys;
  70. }
  71. }
  72. public ICollection Values
  73. {
  74. get
  75. {
  76. if (_values != null) return _values;
  77. _values = new ArrayList ();
  78. foreach (IDictionary dic in dictionaries)
  79. _values.AddRange (dic.Values);
  80. return _values;
  81. }
  82. }
  83. public void Add (object key, object value)
  84. {
  85. throw new NotSupportedException ();
  86. }
  87. public void Clear ()
  88. {
  89. throw new NotSupportedException ();
  90. }
  91. public bool Contains (object ob)
  92. {
  93. foreach (IDictionary dic in dictionaries)
  94. if (dic.Contains (ob)) return true;
  95. return false;
  96. }
  97. public IDictionaryEnumerator GetEnumerator ()
  98. {
  99. return new AggregateEnumerator (dictionaries);
  100. }
  101. IEnumerator IEnumerable.GetEnumerator ()
  102. {
  103. return new AggregateEnumerator (dictionaries);
  104. }
  105. public void Remove (object ob)
  106. {
  107. throw new NotSupportedException ();
  108. }
  109. public void CopyTo (Array array, int index)
  110. {
  111. foreach (object ob in this)
  112. array.SetValue (ob, index++);
  113. }
  114. public int Count
  115. {
  116. get
  117. {
  118. int c = 0;
  119. foreach (IDictionary dic in dictionaries)
  120. c += dic.Count;
  121. return c;
  122. }
  123. }
  124. public bool IsSynchronized
  125. {
  126. get { return false; }
  127. }
  128. public object SyncRoot
  129. {
  130. get { return this; }
  131. }
  132. }
  133. internal class AggregateEnumerator: IDictionaryEnumerator
  134. {
  135. IDictionary[] dictionaries;
  136. int pos = 0;
  137. IDictionaryEnumerator currente;
  138. public AggregateEnumerator (IDictionary[] dics)
  139. {
  140. dictionaries = dics;
  141. Reset ();
  142. }
  143. public DictionaryEntry Entry
  144. {
  145. get { return currente.Entry; }
  146. }
  147. public object Key
  148. {
  149. get { return currente.Key; }
  150. }
  151. public object Value
  152. {
  153. get { return currente.Value; }
  154. }
  155. public object Current
  156. {
  157. get { return currente.Current; }
  158. }
  159. public bool MoveNext ()
  160. {
  161. if (pos >= dictionaries.Length) return false;
  162. if (!currente.MoveNext())
  163. {
  164. pos++;
  165. if (pos >= dictionaries.Length) return false;
  166. currente = dictionaries [pos].GetEnumerator ();
  167. return MoveNext ();
  168. }
  169. return true;
  170. }
  171. public void Reset ()
  172. {
  173. pos = 0;
  174. if (dictionaries.Length > 0)
  175. currente = dictionaries [0].GetEnumerator ();
  176. }
  177. }
  178. }