aboutsummaryrefslogtreecommitdiff
path: root/Emby.Server.Implementations/IO/SharpCifs/Util/Sharpen/ConcurrentHashMap.cs
blob: 7f464ad362e349c233938b9f8ff674083d76a19f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
using System.Collections.Generic;

namespace SharpCifs.Util.Sharpen
{
    internal class ConcurrentHashMap<T, TU> : AbstractMap<T, TU>, IConcurrentMap<T, TU>
	{
		private Dictionary<T, TU> _table;

		public ConcurrentHashMap ()
		{
			_table = new Dictionary<T, TU> ();
		}

		public ConcurrentHashMap (int initialCapacity, float loadFactor, int concurrencyLevel)
		{
			_table = new Dictionary<T, TU> (initialCapacity);
		}

		public override void Clear ()
		{
			lock (_table) {
				_table = new Dictionary<T, TU> ();
			}
		}

		public override bool ContainsKey (object name)
		{
			return _table.ContainsKey ((T)name);
		}

		public override ICollection<KeyValuePair<T, TU>> EntrySet ()
		{
			return this;
		}

		public override TU Get (object key)
		{
			TU local;
			_table.TryGetValue ((T)key, out local);
			return local;
		}

		protected override IEnumerator<KeyValuePair<T, TU>> InternalGetEnumerator ()
		{
			return _table.GetEnumerator ();
		}

		public override bool IsEmpty ()
		{
			return _table.Count == 0;
		}

		public override TU Put (T key, TU value)
		{
			lock (_table) {
				TU old = Get (key);
				Dictionary<T, TU> newTable = new Dictionary<T, TU> (_table);
				newTable[key] = value;
				_table = newTable;
				return old;
			}
		}

		public TU PutIfAbsent (T key, TU value)
		{
			lock (_table) {
				if (!ContainsKey (key)) {
					Dictionary<T, TU> newTable = new Dictionary<T, TU> (_table);
					newTable[key] = value;
					_table = newTable;
					return value;
				}
				return Get (key);
			}
		}

		public override TU Remove (object key)
		{
			lock (_table) {
				TU old = Get ((T)key);
				Dictionary<T, TU> newTable = new Dictionary<T, TU> (_table);
				newTable.Remove ((T)key);
				_table = newTable;
				return old;
			}
		}

		public bool Remove (object key, object value)
		{
			lock (_table) {
				if (ContainsKey (key) && value.Equals (Get (key))) {
					Dictionary<T, TU> newTable = new Dictionary<T, TU> (_table);
					newTable.Remove ((T)key);
					_table = newTable;
					return true;
				}
				return false;
			}
		}

		public bool Replace (T key, TU oldValue, TU newValue)
		{
			lock (_table) {
				if (ContainsKey (key) && oldValue.Equals (Get (key))) {
					Dictionary<T, TU> newTable = new Dictionary<T, TU> (_table);
					newTable[key] = newValue;
					_table = newTable;
					return true;
				}
				return false;
			}
		}

		public override IEnumerable<T> Keys {
			get { return _table.Keys; }
		}

		public override IEnumerable<TU> Values {
			get { return _table.Values; }
		}
	}
}