comparison Pml/Elements/Dictionary.cs @ 0:3ab940a0c7a0

Initial commit
author Ivo Smits <Ivo@UCIS.nl>
date Tue, 11 Sep 2012 16:28:53 +0200
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:3ab940a0c7a0
1 using System;
2 using System.Collections;
3 using System.Collections.Generic;
4
5 namespace UCIS.Pml {
6 public class PmlDictionary : PmlElement, IDictionary<string, PmlElement>, ICollection<PmlElement> {
7 private List<KeyValuePair<string, PmlElement>> pItems = new List<KeyValuePair<string, PmlElement>>();
8
9 public PmlDictionary() { }
10 public PmlDictionary(String[] keys, params PmlElement[] values) {
11 if (keys.Length != values.Length) throw new ArgumentException("keys.Length != values.Length", "values");
12 for (int i = 0; i < keys.Length; i++) Add(keys[i], values[i]);
13 }
14 public PmlDictionary(params KeyValuePair<string, PmlElement>[] Elements) {
15 foreach (KeyValuePair<string, PmlElement> Item in Elements) pItems.Add(Item);
16 }
17 public PmlDictionary(IEnumerable<KeyValuePair<string, PmlElement>> Elements) {
18 foreach (KeyValuePair<string, PmlElement> Item in Elements) pItems.Add(Item);
19 }
20
21 public override PmlElement GetChild(string Name) {
22 foreach (KeyValuePair<string, PmlElement> KVP in pItems) {
23 if (KVP.Key.Equals(Name, StringComparison.InvariantCultureIgnoreCase)) return KVP.Value;
24 }
25 return null;
26 }
27 public override PmlElement GetChild(int index) {
28 return pItems[index].Value;
29 }
30 public bool TryGetValue(string key, out PmlElement value) {
31 value = GetChild(key);
32 return (value != null);
33 }
34 public PmlElement this[string key] {
35 get { return GetChild(key); }
36 set {
37 Remove(key);
38 Add(key, value);
39 }
40 }
41
42 public PmlElement Add(string Key, PmlElement Element) {
43 if (Element == null) Element = new PmlNull();
44 pItems.Add(new KeyValuePair<string, PmlElement>(Key, Element));
45 return Element;
46 }
47 private void Add(KeyValuePair<string, PmlElement> item) {
48 pItems.Add(item);
49 }
50 public void Add(PmlElement item) {
51 Add("", item);
52 }
53
54 void IDictionary<string, PmlElement>.Add(string key, PmlElement value) {
55 Add(key, value);
56 }
57 void ICollection<KeyValuePair<string, PmlElement>>.Add(KeyValuePair<string, PmlElement> item) {
58 Add(item);
59 }
60
61 public bool Remove(PmlElement item) {
62 foreach (KeyValuePair<string, PmlElement> KVP in pItems) {
63 if (KVP.Value == item) {
64 pItems.Remove(KVP);
65 return true;
66 }
67 }
68 return false;
69 }
70 public bool Remove(string Key) {
71 foreach (KeyValuePair<string, PmlElement> KVP in pItems) {
72 if (KVP.Key.Equals(Key, StringComparison.InvariantCultureIgnoreCase)) {
73 pItems.Remove(KVP);
74 return true;
75 }
76 }
77 return false;
78 }
79 public bool Remove(KeyValuePair<string, PmlElement> item) {
80 return ((ICollection<KeyValuePair<string, PmlElement>>)pItems).Remove(item);
81 }
82
83 public int Count {
84 get { return pItems.Count; }
85 }
86
87 public void Clear() {
88 pItems.Clear();
89 }
90
91 public bool Contains(PmlElement item) {
92 foreach (KeyValuePair<string, PmlElement> KVP in pItems) {
93 if (KVP.Value == item) return true;
94 }
95 return false;
96 }
97 bool ICollection<KeyValuePair<string, PmlElement>>.Contains(KeyValuePair<string, PmlElement> item) {
98 return ((ICollection<KeyValuePair<string, PmlElement>>)pItems).Contains(item);
99 }
100 public bool ContainsKey(string key) {
101 foreach (KeyValuePair<string, PmlElement> KVP in pItems) {
102 if (KVP.Key.Equals(key, StringComparison.InvariantCultureIgnoreCase)) return true;
103 }
104 return false;
105 }
106
107 void ICollection<KeyValuePair<string, PmlElement>>.CopyTo(KeyValuePair<string, PmlElement>[] array, int arrayIndex) {
108 ((ICollection<KeyValuePair<string, PmlElement>>)pItems).CopyTo(array, arrayIndex);
109 }
110 void ICollection<PmlElement>.CopyTo(PmlElement[] array, int arrayIndex) {
111 foreach (KeyValuePair<string, PmlElement> KVP in pItems) {
112 array[arrayIndex] = KVP.Value;
113 arrayIndex += 1;
114 }
115 }
116
117 public ICollection<string> Keys {
118 get {
119 List<string> Ret = new List<string>();
120 foreach (KeyValuePair<string, PmlElement> KVP in pItems) Ret.Add(KVP.Key);
121 return Ret;
122 }
123 }
124 public ICollection<PmlElement> Values {
125 get {
126 List<PmlElement> Ret = new List<PmlElement>();
127 foreach (KeyValuePair<string, PmlElement> KVP in pItems) Ret.Add(KVP.Value);
128 return Ret;
129 }
130 }
131 public IEnumerator<KeyValuePair<string, PmlElement>> GetEnumerator() { return pItems.GetEnumerator(); }
132 IEnumerator IEnumerable.GetEnumerator() { return pItems.GetEnumerator(); }
133 IEnumerator<PmlElement> IEnumerable<PmlElement>.GetEnumerator() { return Values.GetEnumerator(); }
134
135 public bool IsReadOnly { get { return false; } }
136
137
138 public override PmlType Type { get { return PmlType.Dictionary; } }
139
140 public override object ToObject() { return pItems; }
141 public override string ToString() { return null; }
142 public override bool ToBoolean() { return pItems.Count > 0; }
143 public override byte ToByte() { return (Byte)pItems.Count; }
144 public override decimal ToDecimal() { return pItems.Count; }
145 public override double ToDouble() { return pItems.Count; }
146 public override short ToInt16() { return (Int16)pItems.Count; }
147 public override int ToInt32() { return pItems.Count; }
148 public override long ToInt64() { return pItems.Count; }
149 public override sbyte ToSByte() { return (SByte)pItems.Count; }
150 public override float ToSingle() { return pItems.Count; }
151 public override ushort ToUInt16() { return (UInt16)pItems.Count; }
152 public override uint ToUInt32() { return (UInt32)pItems.Count; }
153 public override ulong ToUInt64() { return (UInt64)pItems.Count; }
154 public override char ToChar() { return '\0'; }
155 public override byte[] ToByteArray() { return null; }
156
157 //public override PmlElement GetChild(string name) { return GetChild(name); }
158 //public override PmlElement GetChild(int index) { return GetChild(index); }
159 public override IEnumerable<PmlElement> GetChildren() { return Values; }
160 public override IEnumerable<KeyValuePair<String, PmlElement>> GetNamedChildren() { return pItems; }
161 public override int GetChildCount() { return pItems.Count; }
162 public override void AddChild(string name, PmlElement value) { Add(name, value); }
163 public override void AddChild(PmlElement value) { Add(value); }
164 }
165 }