Update according to design review
[platform/core/csapi/tizenfx.git] / src / Tizen.Network.IoTConnectivity / Tizen.Network.IoTConnectivity / ResourceTypes.cs
1  /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17
18 using System;
19 using System.Collections;
20 using System.Collections.Generic;
21 using System.Text.RegularExpressions;
22
23 namespace Tizen.Network.IoTConnectivity
24 {
25     /// <summary>
26     /// This class contains resource types and provides APIs to manage, add, remove those types.
27     /// A resource type indicates a class or category of resources.
28     /// </summary>
29     public class ResourceTypes : IEnumerable<string>, IDisposable
30     {
31         internal const int MaxLength = 61;
32         internal IntPtr _resourceTypeHandle = IntPtr.Zero;
33         private readonly HashSet<string> _resourceTypes = new HashSet<string>();
34         private bool _disposed = false;
35
36         /// <summary>
37         /// Constructor of ResourceTypes
38         /// </summary>
39         /// <seealso cref="Add()"/>
40         /// <seealso cref="Remove()"/>
41         /// <exception cref="NotSupportedException">Thrown when the iotcon is not supported</exception>
42         /// <exception cref="OutOfMemoryException">Thrown when there is not enough memory</exception>
43         /// <exception cref="ArgumentException">Thrown when there is an invalid parameter</exception>
44         /// <code>
45         /// ResourceTypes types = new ResourceTypes();
46         /// </code>
47         public ResourceTypes()
48         {
49             int ret = Interop.IoTConnectivity.Common.ResourceTypes.Create(out _resourceTypeHandle);
50             if (ret != (int)IoTConnectivityError.None)
51             {
52                 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create type");
53                 throw IoTConnectivityErrorFactory.GetException(ret);
54             }
55         }
56
57         /// <summary>
58         /// Constructor of ResourceTypes using list of types
59         /// </summary>
60         /// <param name="types">List of resource types</param>
61         /// <code>
62         /// ResourceTypes types = new ResourceTypes(new List<string>() { "org.tizen.light", "oic.if.room" });
63         /// </code>
64         public ResourceTypes(IEnumerable<string> types)
65         {
66             int ret = Interop.IoTConnectivity.Common.ResourceTypes.Create(out _resourceTypeHandle);
67             if (ret != (int)IoTConnectivityError.None)
68             {
69                 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create type");
70                 throw IoTConnectivityErrorFactory.GetException(ret);
71             }
72
73             foreach (string type in types)
74             {
75                 Add(type);
76             }
77         }
78
79         internal ResourceTypes(IntPtr typesHandleToClone)
80         {
81             int ret = Interop.IoTConnectivity.Common.ResourceTypes.Clone(typesHandleToClone, out _resourceTypeHandle);
82             if (ret != (int)IoTConnectivityError.None)
83             {
84                 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create type");
85                 throw IoTConnectivityErrorFactory.GetException(ret);
86             }
87
88             Interop.IoTConnectivity.Common.ResourceTypes.ForeachCallback cb = (string type, IntPtr data) =>
89             {
90                 _resourceTypes.Add(type);
91                 return true;
92             };
93
94             ret = Interop.IoTConnectivity.Common.ResourceTypes.Foreach(typesHandleToClone, cb, IntPtr.Zero);
95             if (ret != (int)IoTConnectivityError.None)
96             {
97                 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create type");
98                 throw IoTConnectivityErrorFactory.GetException(ret);
99             }
100         }
101
102         /// <summary>
103         /// Destructor of the ResourceTypes class.
104         /// </summary>
105         ~ResourceTypes()
106         {
107             Dispose(false);
108         }
109
110         /// <summary>
111         /// Indicates count of types in the list
112         /// </summary>
113         /// <code>
114         /// ResourceTypes types = new ResourceTypes(new List<string>() { "org.tizen.light", "oic.if.room" });
115         /// Console.WriteLine("There are {0} items", types.Count);
116         /// </code>
117         public int Count
118         {
119             get
120             {
121                 return _resourceTypes.Count;
122             }
123         }
124
125         /// <summary>
126         /// Adds a resource type into the list.
127         /// </summary>
128         /// <remarks>
129         /// The length of @a item should be less than or equal to 61.\n
130         /// The @a item must start with a lowercase alphabetic character, followed by a sequence
131         /// of lowercase alphabetic, numeric, ".", or "-" characters, and contains no white space.\n
132         /// Duplicate strings are not allowed.
133         /// </remarks>
134         /// <param name="item">The string data to insert into the resource types</param>
135         /// <seealso cref="Remove()"/>
136         /// <exception cref="NotSupportedException">Thrown when the iotcon is not supported</exception>
137         /// <exception cref="ArgumentException">Thrown when there is an invalid parameter</exception>
138         /// <exception cref="InvalidOperationException">Thrown when the operation is invalid</exception>
139         /// <code>
140         /// ResourceTypes resourceTypes = new ResourceTypes();
141         /// resourceTypes.Add("org.tizen.light");
142         /// </code>
143         public void Add(string item)
144         {
145             if (IsValid(item))
146             {
147                 int ret = Interop.IoTConnectivity.Common.ResourceTypes.Add(_resourceTypeHandle, item);
148                 if (ret != (int)IoTConnectivityError.None)
149                 {
150                     Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to add type");
151                     throw IoTConnectivityErrorFactory.GetException(ret);
152                 }
153                 _resourceTypes.Add(item);
154             }
155             else
156             {
157                 Log.Error(IoTConnectivityErrorFactory.LogTag, "Invalid type");
158                 throw IoTConnectivityErrorFactory.GetException((int)IoTConnectivityError.InvalidParameter);
159             }
160         }
161
162         /// <summary>
163         /// Removes a resource type from the list
164         /// </summary>
165         /// <param name="item">The string data to delete from the resource types</param>
166         /// <seealso cref="Add()"/>
167         /// <exception cref="NotSupportedException">Thrown when the iotcon is not supported</exception>
168         /// <exception cref="ArgumentException">Thrown when there is an invalid parameter</exception>
169         /// <exception cref="InvalidOperationException">Thrown when the operation is invalid</exception>
170         /// <code>
171         /// ResourceTypes resourceTypes = new ResourceTypes(new List<string>() { "org.tizen.light", "oic.if.room" });
172         /// resourceTypes.Remove("oic.if.room");
173         /// </code>
174         public void Remove(string item)
175         {
176             bool isRemoved = _resourceTypes.Remove(item);
177             if (isRemoved)
178             {
179                 int ret = Interop.IoTConnectivity.Common.ResourceTypes.Remove(_resourceTypeHandle, item);
180                 if (ret != (int)IoTConnectivityError.None)
181                 {
182                     Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to remove type");
183                     throw IoTConnectivityErrorFactory.GetException(ret);
184                 }
185             }
186             else
187                 throw IoTConnectivityErrorFactory.GetException((int)IoTConnectivityError.InvalidParameter);
188         }
189
190         /// <summary>
191         /// Return enumerator for the list of types
192         /// </summary>
193         /// <returns>The enumerator</returns>
194         /// <code>
195         /// ResourceTypes resourceTypes = new ResourceTypes(new List<string>() { "org.tizen.light", "oic.if.room" });
196         /// foreach(string item in resourceTypes)
197         /// {
198         ///     Console.WriteLine("Type : {0}", item);
199         /// }
200         /// </code>
201         public IEnumerator<string> GetEnumerator()
202         {
203             return _resourceTypes.GetEnumerator();
204         }
205
206         /// <summary>
207         /// Return enumerator for the list of types
208         /// </summary>
209         /// <returns>The enumerator</returns>
210         /// <code>
211         /// ResourceTypes resourceTypes = new ResourceTypes(new List<string>() { "org.tizen.light", "oic.if.room" });
212         /// foreach(string item in resourceTypes)
213         /// {
214         ///     Console.WriteLine("Type : {0}", item);
215         /// }
216         /// </code>
217         IEnumerator IEnumerable.GetEnumerator()
218         {
219             return _resourceTypes.GetEnumerator();
220         }
221
222         /// <summary>
223         /// Releases any unmanaged resources used by this object.
224         /// </summary>
225         public void Dispose()
226         {
227             Dispose(true);
228             GC.SuppressFinalize(this);
229         }
230
231         internal static bool IsValid(string type)
232         {
233             Regex r = new Regex("^[a-zA-Z0-9.-]+$");
234             return (type.Length <= MaxLength && char.IsLower(type[0]) && r.IsMatch(type));
235         }
236
237         /// <summary>
238         /// Releases any unmanaged resources used by this object. Can also dispose any other disposable objects.
239         /// </summary>
240         /// <param name="disposing">If true, disposes any disposable objects. If false, does not dispose disposable objects.</param>
241         protected virtual void Dispose(bool disposing)
242         {
243             if (_disposed)
244                 return;
245
246             if (disposing)
247             {
248                 // Free managed objects
249             }
250
251             Interop.IoTConnectivity.Common.ResourceTypes.Destroy(_resourceTypeHandle);
252             _disposed = true;
253         }
254     }
255 }