1 |
using System; |
2 |
using System.Collections.Generic; |
3 |
using System.Linq; |
4 |
using System.Text; |
5 |
using libxmltv.Interfaces; |
6 |
using System.Runtime.Serialization; |
7 |
using System.Collections; |
8 |
using System.Runtime.InteropServices; |
9 |
using System.Reflection; |
10 |
|
11 |
namespace libxmltv.Core |
12 |
{ |
13 |
|
14 |
public class PropertyCollection<T> : IPropertyCollection<T> |
15 |
{ |
16 |
private List<T> items = new List<T>(); |
17 |
public PropertyCollection() : this(new List<T>()) { } |
18 |
public PropertyCollection(ICollection<T> collection) { foreach (var t in collection) { items.Add(t); } } |
19 |
public int PropertyCount { get { return items.Count; } } |
20 |
|
21 |
public bool IsReadOnly |
22 |
{ |
23 |
get |
24 |
{ |
25 |
ICollection<T> collection = (items as ICollection<T>); |
26 |
if (collection == null) |
27 |
{ |
28 |
throw new InvalidCastException(string.Format("Unable to cast: '{0}' to '{1}'", items.GetType().Name, typeof(ICollection<T>).Name)); |
29 |
} |
30 |
return collection.IsReadOnly; |
31 |
} |
32 |
} |
33 |
|
34 |
public void AddProperty(T item) { items.Add(item); } |
35 |
public void ClearProperties() { items.Clear(); } |
36 |
public bool ContainsProperty(T item) { return items.Contains(item); } |
37 |
public void CopyPropertiesTo(T[] array, int arrayIndex) { items.CopyTo(array, arrayIndex); } |
38 |
public bool RemoveProperty(T item) { return items.Remove(item); } |
39 |
public IEnumerator<T> GetEnumerator() { return items.GetEnumerator(); } |
40 |
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return items.GetEnumerator(); } |
41 |
|
42 |
public override string ToString() |
43 |
{ |
44 |
return string.Format("Property Count: {0}", PropertyCount); |
45 |
} |
46 |
} |
47 |
public class PropertyDictionary : PropertyDictionary<string, object>, IPropertyDictionary |
48 |
{ |
49 |
public PropertyDictionary() : base() { } |
50 |
public PropertyDictionary(IPropertyDictionary dictionary) : base(dictionary) { } |
51 |
public PropertyDictionary(IEqualityComparer<string> comparer) : base(comparer) { } |
52 |
public PropertyDictionary(int capacity) :base(capacity) { } |
53 |
public PropertyDictionary(IPropertyDictionary dictionary, IEqualityComparer<string> comparer) : base(dictionary, comparer) { } |
54 |
public PropertyDictionary(int capacity, IEqualityComparer<string> comparer) : base(capacity, comparer) { } |
55 |
protected PropertyDictionary(SerializationInfo info, StreamingContext context) : base(info, context) { } |
56 |
} |
57 |
public class PropertyDictionary<TKey, TValue> : IPropertyDictionary<TKey, TValue> |
58 |
{ |
59 |
[StructLayout(LayoutKind.Sequential)] |
60 |
private struct Entry |
61 |
{ |
62 |
public int hashCode; |
63 |
public int next; |
64 |
public TKey name; |
65 |
public TValue value; |
66 |
public override string ToString() |
67 |
{ |
68 |
return new PropertyValuePair<TKey, TValue>(name, value).ToString(); |
69 |
} |
70 |
} |
71 |
private Entry[] entries |
72 |
{ |
73 |
get{ |
74 |
List<Entry> list = new List<Entry>(); |
75 |
int index = 0; |
76 |
foreach (var t in properties) |
77 |
{ |
78 |
Entry entry = new Entry(); |
79 |
entry.hashCode = this.properties.Comparer.GetHashCode(t.Key) & 0x7fffffff; |
80 |
entry.next = index + 1; |
81 |
entry.name = t.Key; |
82 |
entry.value = t.Value; |
83 |
list.Add(entry); |
84 |
index++; |
85 |
} |
86 |
return list.ToArray(); |
87 |
} |
88 |
} |
89 |
|
90 |
private int version |
91 |
{ |
92 |
get |
93 |
{ |
94 |
Type t = typeof(Dictionary<TKey, TValue>); |
95 |
var field = t.GetField("version", BindingFlags.NonPublic | BindingFlags.Instance); |
96 |
var v = Convert.ToInt32(field.GetValue(properties)); |
97 |
return v; |
98 |
} |
99 |
} |
100 |
private Dictionary<TKey, TValue> properties; |
101 |
public PropertyDictionary() { properties = new Dictionary<TKey, TValue>(); } |
102 |
public PropertyDictionary(IPropertyDictionary<TKey, TValue> dictionary) :this() { foreach (var d in dictionary) { properties.Add(d.Name, d.Value); } } |
103 |
public PropertyDictionary(IEqualityComparer<TKey> comparer) { properties = new Dictionary<TKey, TValue>(comparer); } |
104 |
public PropertyDictionary(int capacity) { properties = new Dictionary<TKey, TValue>(capacity); } |
105 |
public PropertyDictionary(IPropertyDictionary<TKey, TValue> dictionary, IEqualityComparer<TKey> comparer) : this(comparer) { foreach (var d in dictionary) { properties.Add(d.Name, d.Value); } } |
106 |
public PropertyDictionary(int capacity, IEqualityComparer<TKey> comparer) { properties = new Dictionary<TKey, TValue>(capacity, comparer); } |
107 |
protected PropertyDictionary(SerializationInfo info, StreamingContext context) { } |
108 |
public IPropertyCollection<TKey> PropertyKeys { get { return new PropertyCollection<TKey>(properties.Keys); } } |
109 |
public IPropertyCollection<TValue> PropertyValues { get { return new PropertyCollection<TValue>(properties.Values); } } |
110 |
public TValue this[TKey key] |
111 |
{ |
112 |
get |
113 |
{ |
114 |
if (this.ContainsProperty(key)) |
115 |
{ |
116 |
return properties[key]; |
117 |
} |
118 |
else |
119 |
{ |
120 |
throw new KeyNotFoundException(string.Format("Property '{0}' does not exist.", key)); |
121 |
} |
122 |
} |
123 |
set |
124 |
{ |
125 |
if (this.ContainsProperty(key)) |
126 |
{ |
127 |
properties[key] = value; |
128 |
} |
129 |
else |
130 |
{ |
131 |
throw new KeyNotFoundException(string.Format("Property '{0}' does not exist.", key)); |
132 |
} |
133 |
} |
134 |
} |
135 |
|
136 |
public void AddProperty(TKey key, TValue value) { if (properties.ContainsKey(key)) { this[key] = value; } else { properties.Add(key, value); } } |
137 |
public bool ContainsProperty(TKey key) { return properties.ContainsKey(key); } |
138 |
public bool RemoveProperty(TKey key) { return properties.Remove(key); } |
139 |
public bool TryGetPropertyValue(TKey key, out TValue value) { return properties.TryGetValue(key, out value); } |
140 |
public int PropertyCount { get { return properties.Count; } } |
141 |
public bool IsReadOnly |
142 |
{ |
143 |
get |
144 |
{ |
145 |
IDictionary<TKey, TValue> collection = (properties as IDictionary<TKey, TValue>); |
146 |
if (collection == null) |
147 |
{ |
148 |
throw new InvalidCastException(string.Format("Unable to cast: '{0}' to '{1}'", properties.GetType().Name, typeof(IDictionary<TKey, TValue>).Name)); |
149 |
} |
150 |
return collection.IsReadOnly; |
151 |
} |
152 |
} |
153 |
public void AddProperty(IPropertyValuePair<TKey, TValue> item) { AddProperty(item.Name, item.Value); } |
154 |
public void ClearProperties() { properties.Clear(); } |
155 |
public bool ContainsProperty(IPropertyValuePair<TKey, TValue> item) { return ContainsProperty(item.Name); } |
156 |
public void CopyPropertiesTo(IPropertyValuePair<TKey, TValue>[] array, int arrayIndex) |
157 |
{ |
158 |
var list = properties.ToList().Cast<IPropertyValuePair<TKey, TValue>>().ToList(); |
159 |
list.CopyTo(array, arrayIndex); |
160 |
} |
161 |
public bool RemoveProperty(IPropertyValuePair<TKey, TValue> item) { return RemoveProperty(item.Name); } |
162 |
IEnumerator<IPropertyValuePair<TKey, TValue>> IEnumerable<IPropertyValuePair<TKey, TValue>>.GetEnumerator() { return new PropertyDictionaryEnumerator(this, 2); } |
163 |
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return properties.GetEnumerator(); } |
164 |
|
165 |
public override string ToString() |
166 |
{ |
167 |
return string.Format("Property Count: {0}", PropertyCount); |
168 |
} |
169 |
#region enumerator support |
170 |
public class PropertyDictionaryEnumerator : IEnumerator<IPropertyValuePair<TKey, TValue>>, IDisposable |
171 |
{ |
172 |
private PropertyDictionary<TKey, TValue> dictionary; |
173 |
private int index; |
174 |
private IPropertyValuePair<TKey, TValue> current; |
175 |
private int getEnumeratorRetType; |
176 |
internal const int DictEntry = 1; |
177 |
internal const int KeyValuePair = 2; |
178 |
private int version; |
179 |
public PropertyDictionaryEnumerator(PropertyDictionary<TKey, TValue> dictionary, int getEnumeratorRetType) |
180 |
{ |
181 |
this.dictionary = dictionary; |
182 |
this.version = dictionary.version; |
183 |
this.index = 0; |
184 |
this.getEnumeratorRetType = getEnumeratorRetType; |
185 |
this.current = new PropertyValuePair<TKey, TValue>(); |
186 |
} |
187 |
|
188 |
public IPropertyValuePair<TKey, TValue> Current |
189 |
{ |
190 |
get { return this.current; } |
191 |
} |
192 |
|
193 |
public void Dispose() |
194 |
{ |
195 |
} |
196 |
|
197 |
object IEnumerator.Current |
198 |
{ |
199 |
get |
200 |
{ |
201 |
if ((this.index == 0) || (this.index == (this.dictionary.Count() + 1))) |
202 |
{ |
203 |
throw new InvalidOperationException("Operation can't happen"); |
204 |
} |
205 |
if (this.getEnumeratorRetType == 1) |
206 |
{ |
207 |
return new DictionaryEntry(this.current.Name, this.current.Value); |
208 |
} |
209 |
return new PropertyValuePair<TKey, TValue>(this.current.Name, this.current.Value); |
210 |
} |
211 |
} |
212 |
|
213 |
public bool MoveNext() |
214 |
{ |
215 |
if (this.version != this.dictionary.version) |
216 |
{ |
217 |
ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_EnumFailedVersion); |
218 |
} |
219 |
while (this.index < this.dictionary.Count()) |
220 |
{ |
221 |
if (this.dictionary.entries[this.index].hashCode >= 0) |
222 |
{ |
223 |
this.current =(IPropertyValuePair<TKey,TValue>)(PropertyValuePair<TKey,TValue>)new KeyValuePair<TKey, TValue>(this.dictionary.entries[this.index].name, this.dictionary.entries[this.index].value); |
224 |
this.index++; |
225 |
return true; |
226 |
} |
227 |
this.index++; |
228 |
} |
229 |
this.index = this.dictionary.Count() + 1; |
230 |
this.current = new PropertyValuePair<TKey, TValue>(); |
231 |
return false; |
232 |
} |
233 |
public void Reset() |
234 |
{ |
235 |
if (this.version != this.dictionary.version) |
236 |
{ |
237 |
ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_EnumFailedVersion); |
238 |
} |
239 |
this.index = 0; |
240 |
this.current = (IPropertyValuePair<TKey, TValue>)(PropertyValuePair<TKey, TValue>)new KeyValuePair<TKey, TValue>(); |
241 |
} |
242 |
} |
243 |
#endregion |
244 |
} |
245 |
|
246 |
|
247 |
} |