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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 93 by william, Sun Mar 10 08:44:53 2013 UTC revision 110 by william, Sun Mar 10 14:28:57 2013 UTC
# Line 10  using System.Reflection; Line 10  using System.Reflection;
10    
11  namespace libxmltv.Core  namespace libxmltv.Core
12  {  {
13        [Serializable]
14      public class PropertyCollection<T> : IPropertyCollection<T>      public class PropertyCollection<T> : IPropertyCollection<T>
15      {      {
16          private List<T> items = new List<T>();          private List<T> items = new List<T>();
17          public PropertyCollection() : this(new List<T>()) { }          public PropertyCollection() : this(new List<T>()) { }
18          public PropertyCollection(ICollection<T> collection) { foreach (var t in collection) { items.Add(t); } }          public PropertyCollection(ICollection<T> collection) { foreach (var t in collection) { items.Add(t); } }
19          public int PropertyCount { get { return items.Count; } }          public int PropertyCount { get { return items.Count; } }
20          public bool IsReadOnly { get { return (items as ICollection<T>).IsReadOnly; } }  
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); }          public void AddProperty(T item) { items.Add(item); }
35          public void ClearProperties() { items.Clear(); }          public void ClearProperties() { items.Clear(); }
36          public bool ContainsProperty(T item) { return items.Contains(item); }          public bool ContainsProperty(T item) { return items.Contains(item); }
# Line 25  namespace libxmltv.Core Line 38  namespace libxmltv.Core
38          public bool RemoveProperty(T item) { return items.Remove(item); }          public bool RemoveProperty(T item) { return items.Remove(item); }
39          public IEnumerator<T> GetEnumerator() { return items.GetEnumerator(); }          public IEnumerator<T> GetEnumerator() { return items.GetEnumerator(); }
40          System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return items.GetEnumerator(); }          System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return items.GetEnumerator(); }
41      }  
42      public class PropertyDictionary : PropertyDictionary<string, object>          public override string ToString()
     {  
         public PropertyDictionary() : base()  
43          {          {
44              ThrowHelper.ThrowNotSupportedException(ExceptionResource.NotSupported_ReadOnlyCollection);              return string.Format("Property Count: {0}", PropertyCount);
45          }          }
46        }
47        [Serializable]
48        public class PropertyDictionary : PropertyDictionary<string, object>, IPropertyDictionary
49        {
50            public PropertyDictionary() : base() { }
51          public PropertyDictionary(IPropertyDictionary dictionary) : base(dictionary) { }          public PropertyDictionary(IPropertyDictionary dictionary) : base(dictionary) { }
52          public PropertyDictionary(IEqualityComparer<string> comparer) : base(comparer) { }          public PropertyDictionary(IEqualityComparer<string> comparer) : base(comparer) { }
53          public PropertyDictionary(int capacity) :base(capacity) { }          public PropertyDictionary(int capacity) :base(capacity) { }
# Line 39  namespace libxmltv.Core Line 55  namespace libxmltv.Core
55          public PropertyDictionary(int capacity, IEqualityComparer<string> comparer) : base(capacity, comparer) { }          public PropertyDictionary(int capacity, IEqualityComparer<string> comparer) : base(capacity, comparer) { }
56          protected PropertyDictionary(SerializationInfo info, StreamingContext context) : base(info, context) { }          protected PropertyDictionary(SerializationInfo info, StreamingContext context) : base(info, context) { }
57      }      }
58        [Serializable]
59      public class PropertyDictionary<TKey, TValue> : IPropertyDictionary<TKey, TValue>      public class PropertyDictionary<TKey, TValue> : IPropertyDictionary<TKey, TValue>
60      {      {
61          [StructLayout(LayoutKind.Sequential)]          [StructLayout(LayoutKind.Sequential)]
# Line 48  namespace libxmltv.Core Line 65  namespace libxmltv.Core
65              public int next;              public int next;
66              public TKey name;              public TKey name;
67              public TValue value;              public TValue value;
68                public override string ToString()
69                {
70                    return new PropertyValuePair<TKey, TValue>(name, value).ToString();
71                }
72          }          }
73          private Entry[] entries          private Entry[] entries
74          {          {
# Line 119  namespace libxmltv.Core Line 140  namespace libxmltv.Core
140          public bool RemoveProperty(TKey key) { return properties.Remove(key); }          public bool RemoveProperty(TKey key) { return properties.Remove(key); }
141          public bool TryGetPropertyValue(TKey key, out TValue value) { return properties.TryGetValue(key, out value); }          public bool TryGetPropertyValue(TKey key, out TValue value) { return properties.TryGetValue(key, out value); }
142          public int PropertyCount { get { return properties.Count; } }          public int PropertyCount { get { return properties.Count; } }
143          public bool IsReadOnly { get { return (properties as ICollection<TKey>).IsReadOnly; } }          public bool IsReadOnly
144          public void AddProperty(PropertyValuePair<TKey, TValue> item) { AddProperty(item.Name, item.Value); }          {
145                get
146                {
147                    IDictionary<TKey, TValue> collection = (properties as IDictionary<TKey, TValue>);
148                    if (collection == null)
149                    {
150                        throw new InvalidCastException(string.Format("Unable to cast: '{0}' to '{1}'", properties.GetType().Name, typeof(IDictionary<TKey, TValue>).Name));
151                    }
152                    return collection.IsReadOnly;
153                }
154            }
155            public void AddProperty(IPropertyValuePair<TKey, TValue> item) { AddProperty(item.Name, item.Value); }
156          public void ClearProperties() { properties.Clear(); }          public void ClearProperties() { properties.Clear(); }
157          public bool ContainsProperty(PropertyValuePair<TKey, TValue> item) { return ContainsProperty(item.Name); }          public bool ContainsProperty(IPropertyValuePair<TKey, TValue> item) { return ContainsProperty(item.Name); }
158          public void CopyPropertiesTo(PropertyValuePair<TKey, TValue>[] array, int arrayIndex)          public void CopyPropertiesTo(IPropertyValuePair<TKey, TValue>[] array, int arrayIndex)
159          {          {
160              var list = properties.ToList().Cast<PropertyValuePair<TKey, TValue>>().ToList();              var list = properties.ToList().Cast<IPropertyValuePair<TKey, TValue>>().ToList();
161              list.CopyTo(array, arrayIndex);              list.CopyTo(array, arrayIndex);
162          }          }
163          public bool RemoveProperty(PropertyValuePair<TKey, TValue> item) { return RemoveProperty(item.Name); }          public bool RemoveProperty(IPropertyValuePair<TKey, TValue> item) { return RemoveProperty(item.Name); }
164          IEnumerator<PropertyValuePair<TKey, TValue>> IEnumerable<PropertyValuePair<TKey, TValue>>.GetEnumerator() { return new PropertyDictionaryEnumerator(this, 2); }          IEnumerator<IPropertyValuePair<TKey, TValue>> IEnumerable<IPropertyValuePair<TKey, TValue>>.GetEnumerator() { return new PropertyDictionaryEnumerator(this, 2); }
165          System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return properties.GetEnumerator(); }          System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return properties.GetEnumerator(); }
166    
167            public override string ToString()
168            {
169                return string.Format("Property Count: {0}", PropertyCount);
170            }
171          #region enumerator support          #region enumerator support
172          public class PropertyDictionaryEnumerator: IEnumerator<PropertyValuePair<TKey,TValue>>, IDisposable          public class PropertyDictionaryEnumerator : IEnumerator<IPropertyValuePair<TKey, TValue>>, IDisposable
173          {          {
174              private PropertyDictionary<TKey, TValue> dictionary;              private PropertyDictionary<TKey, TValue> dictionary;
175              private int index;              private int index;
176              private PropertyValuePair<TKey, TValue> current;              private IPropertyValuePair<TKey, TValue> current;
177              private int getEnumeratorRetType;              private int getEnumeratorRetType;
178              internal const int DictEntry = 1;              internal const int DictEntry = 1;
179              internal const int KeyValuePair = 2;              internal const int KeyValuePair = 2;
# Line 151  namespace libxmltv.Core Line 187  namespace libxmltv.Core
187                  this.current = new PropertyValuePair<TKey, TValue>();                  this.current = new PropertyValuePair<TKey, TValue>();
188              }              }
189    
190              public PropertyValuePair<TKey, TValue> Current              public IPropertyValuePair<TKey, TValue> Current
191              {              {
192                  get { return this.current; }                  get { return this.current; }
193              }              }
# Line 186  namespace libxmltv.Core Line 222  namespace libxmltv.Core
222                  {                  {
223                      if (this.dictionary.entries[this.index].hashCode >= 0)                      if (this.dictionary.entries[this.index].hashCode >= 0)
224                      {                      {
225                          this.current = new KeyValuePair<TKey, TValue>(this.dictionary.entries[this.index].name, this.dictionary.entries[this.index].value);                          this.current =(IPropertyValuePair<TKey,TValue>)(PropertyValuePair<TKey,TValue>)new KeyValuePair<TKey, TValue>(this.dictionary.entries[this.index].name, this.dictionary.entries[this.index].value);
226                          this.index++;                          this.index++;
227                          return true;                          return true;
228                      }                      }
# Line 203  namespace libxmltv.Core Line 239  namespace libxmltv.Core
239                      ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_EnumFailedVersion);                      ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_EnumFailedVersion);
240                  }                  }
241                  this.index = 0;                  this.index = 0;
242                  this.current = new KeyValuePair<TKey, TValue>();                  this.current = (IPropertyValuePair<TKey, TValue>)(PropertyValuePair<TKey, TValue>)new KeyValuePair<TKey, TValue>();
243              }              }
244          }          }
245          #endregion            #endregion  

Legend:
Removed from v.93  
changed lines
  Added in v.110

  ViewVC Help
Powered by ViewVC 1.1.22