[NUI] Rebase develnui (DevelNUI only patches --> master) (#3910)
[platform/core/csapi/tizenfx.git] / test / Tizen.NUI.Devel.Tests.Ubuntu / nunit.framework / Internal / PropertyBag.cs
1 // ***********************************************************************
2 // Copyright (c) 2015 Charlie Poole
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining
5 // a copy of this software and associated documentation files (the
6 // "Software"), to deal in the Software without restriction, including
7 // without limitation the rights to use, copy, modify, merge, publish,
8 // distribute, sublicense, and/or sell copies of the Software, and to
9 // permit persons to whom the Software is furnished to do so, subject to
10 // the following conditions:
11 // 
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
14 // 
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 // ***********************************************************************
23 #define PORTABLE
24 #define TIZEN
25 #define NUNIT_FRAMEWORK
26 #define NUNITLITE
27 #define NET_4_5
28 #define PARALLEL
29 using System;
30 using System.Collections;
31 using System.Collections.Generic;
32 using NUnit.Framework.Interfaces;
33
34 namespace NUnit.Framework.Internal
35 {
36     /// <summary>
37     /// A PropertyBag represents a collection of name value pairs
38     /// that allows duplicate entries with the same key. Methods
39     /// are provided for adding a new pair as well as for setting
40     /// a key to a single value. All keys are strings but _values
41     /// may be of any type. Null _values are not permitted, since
42     /// a null entry represents the absence of the key.
43     /// </summary>
44     public class PropertyBag : IPropertyBag
45     {
46         private Dictionary<string, IList> inner = new Dictionary<string, IList>();
47
48         #region IPropertyBagMembers
49
50         /// <summary>
51         /// Adds a key/value pair to the property set
52         /// </summary>
53         /// <param name="key">The key</param>
54         /// <param name="value">The value</param>
55         public void Add(string key, object value)
56         {
57             IList list;
58             if (!inner.TryGetValue(key, out list))
59             {
60                 list = new List<object>();
61                 inner.Add(key, list);
62             }
63             list.Add(value);
64         }
65
66         /// <summary>
67         /// Sets the value for a key, removing any other
68         /// _values that are already in the property set.
69         /// </summary>
70         /// <param name="key"></param>
71         /// <param name="value"></param>
72         public void Set(string key, object value)
73         {
74             // Guard against mystery exceptions later!
75             Guard.ArgumentNotNull(key, "key");
76             Guard.ArgumentNotNull(value, "value");
77
78             IList list = new List<object>();
79             list.Add(value);
80             inner[key] = list;
81         }
82
83         /// <summary>
84         /// Gets a single value for a key, using the first
85         /// one if multiple _values are present and returning
86         /// null if the value is not found.
87         /// </summary>
88         /// <param name="key"></param>
89         /// <returns></returns>
90         public object Get(string key)
91         {
92             IList list;
93             return inner.TryGetValue(key, out list) && list.Count > 0
94                 ? list[0]
95                 : null;
96         }
97
98         /// <summary>
99         /// Gets a flag indicating whether the specified key has
100         /// any entries in the property set.
101         /// </summary>
102         /// <param name="key">The key to be checked</param>
103         /// <returns>
104         /// True if their are _values present, otherwise false
105         /// </returns>
106         public bool ContainsKey(string key)
107         {
108             return inner.ContainsKey(key);
109         }
110
111         /// <summary>
112         /// Gets a collection containing all the keys in the property set
113         /// </summary>
114         /// <value></value>
115         public ICollection<string> Keys
116         {
117             get { return inner.Keys; }
118         }
119
120         /// <summary>
121         /// Gets or sets the list of _values for a particular key
122         /// </summary>
123         public IList this[string key]
124         {
125             get
126             {
127                 IList list;
128                 if (!inner.TryGetValue(key, out list))
129                 {
130                     list = new List<object>();
131                     inner.Add(key, list);
132                 }
133                 return list;
134             }
135             set
136             {
137                 inner[key] = value;
138             }
139         }
140
141         #endregion
142
143         #region IXmlNodeBuilder Members
144
145         /// <summary>
146         /// Returns an XmlNode representating the current PropertyBag.
147         /// </summary>
148         /// <param name="recursive">Not used</param>
149         /// <returns>An XmlNode representing the PropertyBag</returns>
150         public TNode ToXml(bool recursive)
151         {
152             return AddToXml(new TNode("dummy"), recursive);
153         }
154
155         /// <summary>
156         /// Returns an XmlNode representing the PropertyBag after
157         /// adding it as a child of the supplied parent node.
158         /// </summary>
159         /// <param name="parentNode">The parent node.</param>
160         /// <param name="recursive">Not used</param>
161         /// <returns></returns>
162         public TNode AddToXml(TNode parentNode, bool recursive)
163         {
164             TNode properties = parentNode.AddElement("properties");
165
166             foreach (string key in Keys)
167             {
168                 foreach (object value in this[key])
169                 {
170                     TNode prop = properties.AddElement("property");
171
172                     // TODO: Format as string
173                     prop.AddAttribute("name", key.ToString());
174                     prop.AddAttribute("value", value.ToString());
175                 }
176             }
177
178             return properties;
179         }
180
181         #endregion
182     }
183 }