Googler

两情相悦,又岂在朝朝暮暮。

C# SyncDictionary类

 

代码
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Threading;

namespace Rocky
{
public class SyncDictionary : IDictionary
{
#region wrap
private IDictionary wrapDictionary;

public SyncDictionary()
:
this(new HybridDictionary())
{

}
public SyncDictionary(IDictionary dictionary)
{
if (dictionary == null)
{
throw new ArgumentNullException("dictionary");
}
wrapDictionary
= dictionary;
}
#endregion

#region IDictionary 成员
public void Add(object key, object value)
{
if (!wrapDictionary.Contains(key))
{
lock (wrapDictionary.SyncRoot)
{
if (!wrapDictionary.Contains(key))
{
wrapDictionary.Add(key, value);
}
}
}
}

public void Clear()
{
if (wrapDictionary.Count > 0)
{
lock (wrapDictionary.SyncRoot)
{
if (wrapDictionary.Count > 0)
{
wrapDictionary.Clear();
}
}
}
}

public bool Contains(object key)
{
return wrapDictionary.Contains(key);
}

public IDictionaryEnumerator GetEnumerator()
{
return wrapDictionary.GetEnumerator();
}

bool IDictionary.IsFixedSize
{
get { return wrapDictionary.IsFixedSize; }
}

bool IDictionary.IsReadOnly
{
get { return wrapDictionary.IsReadOnly; }
}

public ICollection Keys
{
get
{
lock (wrapDictionary.SyncRoot)
{
return wrapDictionary.Keys;
}
}
}

public void Remove(object key)
{
if (wrapDictionary.Contains(key))
{
lock (wrapDictionary.SyncRoot)
{
if (wrapDictionary.Contains(key))
{
wrapDictionary.Remove(key);
}
}
}
}

public ICollection Values
{
get
{
lock (wrapDictionary.SyncRoot)
{
return wrapDictionary.Values;
}
}
}

public object this[object key]
{
set
{
lock (wrapDictionary.SyncRoot)
{
wrapDictionary[key]
= value;
}
}
get
{
return wrapDictionary[key];
}
}
#endregion

#region ICollection 成员
public void CopyTo(Array array, int index)
{
lock (wrapDictionary.SyncRoot)
{
wrapDictionary.CopyTo(array, index);
}
}

public int Count
{
get { return wrapDictionary.Count; }
}

bool ICollection.IsSynchronized
{
get { return true; }
}

object ICollection.SyncRoot
{
get { return wrapDictionary.SyncRoot; }
}
#endregion

#region IEnumerable 成员
IEnumerator IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
#endregion
}

public class SyncDictionary<TKey, TValue> : IDictionary<TKey, TValue>
{
#region wrap
private object syncRoot;
private IDictionary<TKey, TValue> wrapDictionary;

private object SyncRoot
{
get
{
if (syncRoot == null)
{
Interlocked.CompareExchange(
ref syncRoot, new object(), null);
}
return syncRoot;
}
}

public SyncDictionary()
:
this(new Dictionary<TKey, TValue>())
{

}
public SyncDictionary(IDictionary<TKey, TValue> dictionary)
{
if (dictionary == null)
{
throw new ArgumentNullException("dictionary");
}
wrapDictionary
= dictionary;
}
#endregion

#region IDictionary<TKey,TValue> 成员
public void Add(TKey key, TValue value)
{
if (!wrapDictionary.ContainsKey(key))
{
lock (this.SyncRoot)
{
if (!wrapDictionary.ContainsKey(key))
{
wrapDictionary.Add(key, value);
}
}
}
}

public bool ContainsKey(TKey key)
{
return wrapDictionary.ContainsKey(key);
}

public ICollection<TKey> Keys
{
get
{
lock (this.SyncRoot)
{
return wrapDictionary.Keys;
}
}
}

public bool Remove(TKey key)
{
if (wrapDictionary.ContainsKey(key))
{
lock (this.SyncRoot)
{
if (wrapDictionary.ContainsKey(key))
{
return wrapDictionary.Remove(key);
}
}
}
return false;
}

public bool TryGetValue(TKey key, out TValue value)
{
lock (this.SyncRoot)
{
return wrapDictionary.TryGetValue(key, out value);
}
}

public ICollection<TValue> Values
{
get
{
lock (this.SyncRoot)
{
return wrapDictionary.Values;
}
}
}

public TValue this[TKey key]
{
set
{
lock (this.SyncRoot)
{
wrapDictionary[key]
= value;
}
}
get
{
TValue value;
wrapDictionary.TryGetValue(key,
out value);
return value;
}
}
#endregion

#region ICollection<KeyValuePair<TKey,TValue>> 成员
void ICollection<KeyValuePair<TKey, TValue>>.Add(KeyValuePair<TKey, TValue> item)
{
lock (this.SyncRoot)
{
((ICollection
<KeyValuePair<TKey, TValue>>)wrapDictionary).Add(item);
}
}

public void Clear()
{
if (wrapDictionary.Count > 0)
{
lock (this.SyncRoot)
{
if (wrapDictionary.Count > 0)
{
wrapDictionary.Clear();
}
}
}
}

bool ICollection<KeyValuePair<TKey, TValue>>.Contains(KeyValuePair<TKey, TValue> item)
{
return ((ICollection<KeyValuePair<TKey, TValue>>)wrapDictionary).Contains(item);
}

public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
{
lock (this.SyncRoot)
{
((ICollection
<KeyValuePair<TKey, TValue>>)wrapDictionary).CopyTo(array, arrayIndex);
}
}

public int Count
{
get { return wrapDictionary.Count; }
}

bool ICollection<KeyValuePair<TKey, TValue>>.IsReadOnly
{
get { return false; }
}

bool ICollection<KeyValuePair<TKey, TValue>>.Remove(KeyValuePair<TKey, TValue> item)
{
lock (this.SyncRoot)
{
return ((ICollection<KeyValuePair<TKey, TValue>>)wrapDictionary).Remove(item);
}
}
#endregion

#region IEnumerable<KeyValuePair<TKey,TValue>> 成员
public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
{
return ((ICollection<KeyValuePair<TKey, TValue>>)wrapDictionary).GetEnumerator();
}
#endregion

#region IEnumerable 成员
IEnumerator IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
#endregion
}
}

 

 

 

posted on 2010-06-11 16:01  RockyLOMO  阅读(660)  评论(0编辑  收藏  举报

导航

Apple/苹果笔记本 Mac Air MC968CH/A 行货在保 I5 11寸 超级本