/[xmltv_parser]/trunk/libxmltv/Core/PropertyDictionary.cs
ViewVC logotype

Annotation of /trunk/libxmltv/Core/PropertyDictionary.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 90 - (hide annotations) (download)
Sat Mar 9 17:42:34 2013 UTC (7 years, 7 months ago) by william
File size: 4857 byte(s)

1 william 90 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    
8     namespace libxmltv.Core
9     {
10    
11     public class PropertyCollection<T> : IPropertyCollection<T>
12     {
13     private List<T> items = new List<T>();
14     public PropertyCollection() : this(new List<T>()) { }
15     public PropertyCollection(ICollection<T> collection) { foreach (var t in collection) { items.Add(t); } }
16     public int PropertyCount { get { return items.Count; } }
17     public bool IsReadOnly { get { return (items as ICollection<T>).IsReadOnly; } }
18     public void AddProperty(T item) { items.Add(item); }
19     public void ClearProperties() { items.Clear(); }
20     public bool ContainsProperty(T item) { return items.Contains(item); }
21     public void CopyPropertiesTo(T[] array, int arrayIndex) { items.CopyTo(array, arrayIndex); }
22     public bool RemoveProperty(T item) { return items.Remove(item); }
23     public IEnumerator<T> GetEnumerator() { return items.GetEnumerator(); }
24     System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return items.GetEnumerator(); }
25     }
26    
27     public class PropertyDictionary<TKey, TValue> : IPropertyDictionary<TKey, TValue>
28     {
29     private Dictionary<TKey, TValue> properties;
30     public PropertyDictionary() { properties = new Dictionary<TKey, TValue>(); }
31     public PropertyDictionary(IPropertyDictionary<TKey, TValue> dictionary) { properties = new Dictionary<TKey, TValue>(); foreach (var d in dictionary) { properties.Add(d.Key, d.Value); } }
32     public PropertyDictionary(IEqualityComparer<TKey> comparer) { properties = new Dictionary<TKey, TValue>(comparer); }
33     public PropertyDictionary(int capacity) { properties = new Dictionary<TKey, TValue>(capacity); }
34     public PropertyDictionary(IPropertyDictionary<TKey, TValue> dictionary, IEqualityComparer<TKey> comparer) : this(comparer) { foreach (var d in dictionary) { properties.Add(d.Key, d.Value); } }
35     public PropertyDictionary(int capacity, IEqualityComparer<TKey> comparer) { properties = new Dictionary<TKey, TValue>(capacity, comparer); }
36     protected PropertyDictionary(SerializationInfo info, StreamingContext context)
37     {
38     }
39     public IPropertyCollection<TKey> PropertyKeys { get { return new PropertyCollection<TKey>(properties.Keys); } }
40     public IPropertyCollection<TValue> PropertyValues { get { return new PropertyCollection<TValue>(properties.Values); } }
41     public TValue this[TKey key]
42     {
43     get
44     {
45     if (this.ContainsProperty(key))
46     {
47     return properties[key];
48     }
49     else
50     {
51     throw new KeyNotFoundException(string.Format("Property '{0}' does not exist.", key));
52     }
53     }
54     set
55     {
56     if (this.ContainsProperty(key))
57     {
58     properties[key] = value;
59     }
60     else
61     {
62     throw new KeyNotFoundException(string.Format("Property '{0}' does not exist.", key));
63     }
64     }
65     }
66    
67     public void AddProperty(TKey key, TValue value) { if (properties.ContainsKey(key)) { this[key] = value; } else { properties.Add(key, value); } }
68     public bool ContainsProperty(TKey key) { return properties.ContainsKey(key); }
69     public bool RemoveProperty(TKey key) { return properties.Remove(key); }
70     public bool TryGetPropertyValue(TKey key, out TValue value) { return properties.TryGetValue(key, out value); }
71     public int PropertyCount { get { return properties.Count; } }
72     public bool IsReadOnly { get { return (properties as ICollection<TKey>).IsReadOnly; } }
73     public void AddProperty(KeyValuePair<TKey, TValue> item) { AddProperty(item.Key, item.Value); }
74     public void ClearProperties() { properties.Clear(); }
75     public bool ContainsProperty(KeyValuePair<TKey, TValue> item) { return ContainsProperty(item.Key); }
76     public void CopyPropertiesTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex) { var list = properties.ToList(); list.CopyTo(array, arrayIndex); }
77     public bool RemoveProperty(KeyValuePair<TKey, TValue> item) { return RemoveProperty(item.Key); }
78     public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator() { return properties.GetEnumerator(); }
79     System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return properties.GetEnumerator(); }
80    
81     }
82    
83     public class PropertyDictionary : PropertyDictionary<string, object>
84     {
85     }
86     }

  ViewVC Help
Powered by ViewVC 1.1.22