2 // SynchronizedKeyedCollection.cs
5 // Atsushi Enomoto <atsushi@ximian.com>
7 // Copyright (C) 2005 Novell, Inc. http://www.novell.com
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
29 using System.Collections.Generic;
30 using System.Runtime.InteropServices;
31 using System.ServiceModel.Channels;
33 namespace System.Collections.Generic
35 [ComVisibleAttribute (false)]
36 public abstract class SynchronizedKeyedCollection<K, T>
37 : SynchronizedCollection<T>
39 Dictionary<K, T> dict;
41 protected SynchronizedKeyedCollection ()
42 : this (new object ())
46 protected SynchronizedKeyedCollection (object syncRoot)
49 dict = new Dictionary<K, T> ();
52 protected SynchronizedKeyedCollection (object syncRoot,
53 IEqualityComparer<K> comparer)
56 dict = new Dictionary<K, T> (comparer);
59 protected SynchronizedKeyedCollection (object syncRoot,
60 IEqualityComparer<K> comparer, int capacity)
63 dict = new Dictionary<K, T> (capacity, comparer);
68 public T this [int index] {
69 get { return base [index]; }
70 set { base [index] = value; }
74 public T this [K key] {
82 protected IDictionary<K, T> Dictionary {
86 public bool Contains (K key)
89 return dict.ContainsKey (key);
93 public bool Remove (K key)
96 return dict.Remove (key);
100 protected void ChangeItemKey (T item, K newKey)
103 K old = GetKeyForItem (item);
104 dict [old] = default (T);
105 dict [newKey] = item;
109 [MonoTODO ("This lock is not an atomic.")]
110 protected override void ClearItems ()
118 protected abstract K GetKeyForItem (T item);
120 [MonoTODO ("This lock is not an atomic.")]
121 protected override void InsertItem (int index, T item)
123 base.InsertItem (index, item);
124 dict.Add (GetKeyForItem (item), item);
127 [MonoTODO ("This lock is not an atomic.")]
128 protected override void RemoveItem (int index)
130 K key = GetKeyForItem (base [index]);
131 base.RemoveItem (index);
135 [MonoTODO ("This lock is not an atomic.")]
136 protected override void SetItem (int index, T item)
138 base.SetItem (index, item);
139 dict [GetKeyForItem (item)] = item;