Update content of nuspec file
[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         /// <code>
42         /// ResourceTypes types = new ResourceTypes();
43         /// </code>
44         public ResourceTypes()
45         {
46             int ret = Interop.IoTConnectivity.Common.ResourceTypes.Create(out _resourceTypeHandle);
47             if (ret != (int)IoTConnectivityError.None)
48             {
49                 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create type");
50                 throw IoTConnectivityErrorFactory.GetException(ret);
51             }
52         }
53
54         /// <summary>
55         /// Constructor of ResourceTypes using list of types
56         /// </summary>
57         /// <param name="types">List of resource types</param>
58         /// <code>
59         /// ResourceTypes types = new ResourceTypes(new List<string>() { "org.tizen.light", "oic.if.room" });
60         /// </code>
61         public ResourceTypes(IEnumerable<string> types)
62         {
63             int ret = Interop.IoTConnectivity.Common.ResourceTypes.Create(out _resourceTypeHandle);
64             if (ret != (int)IoTConnectivityError.None)
65             {
66                 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create type");
67                 throw IoTConnectivityErrorFactory.GetException(ret);
68             }
69
70             foreach (string type in types)
71             {
72                 Add(type);
73             }
74         }
75
76         internal ResourceTypes(IntPtr typesHandleToClone)
77         {
78             int ret = Interop.IoTConnectivity.Common.ResourceTypes.Clone(typesHandleToClone, out _resourceTypeHandle);
79             if (ret != (int)IoTConnectivityError.None)
80             {
81                 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create type");
82                 throw IoTConnectivityErrorFactory.GetException(ret);
83             }
84
85             Interop.IoTConnectivity.Common.ResourceTypes.ForeachCallback cb = (string type, IntPtr data) =>
86             {
87                 _resourceTypes.Add(type);
88                 return true;
89             };
90
91             ret = Interop.IoTConnectivity.Common.ResourceTypes.Foreach(typesHandleToClone, cb, IntPtr.Zero);
92             if (ret != (int)IoTConnectivityError.None)
93             {
94                 Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to create type");
95                 throw IoTConnectivityErrorFactory.GetException(ret);
96             }
97         }
98
99         /// <summary>
100         /// Destructor of the ResourceTypes class.
101         /// </summary>
102         ~ResourceTypes()
103         {
104             Dispose(false);
105         }
106
107         /// <summary>
108         /// Indicates count of types in the list
109         /// </summary>
110         /// <code>
111         /// ResourceTypes types = new ResourceTypes(new List<string>() { "org.tizen.light", "oic.if.room" });
112         /// Console.WriteLine("There are {0} items", types.Count);
113         /// </code>
114         public int Count
115         {
116             get
117             {
118                 return _resourceTypes.Count;
119             }
120         }
121
122         /// <summary>
123         /// Adds a resource type into the list.
124         /// </summary>
125         /// <remarks>
126         /// The length of @a item should be less than or equal to 61.\n
127         /// The @a item must start with a lowercase alphabetic character, followed by a sequence
128         /// of lowercase alphabetic, numeric, ".", or "-" characters, and contains no white space.\n
129         /// Duplicate strings are not allowed.
130         /// </remarks>
131         /// <param name="item">The string data to insert into the resource types</param>
132         /// <seealso cref="Remove()"/>
133         /// <code>
134         /// ResourceTypes resourceTypes = new ResourceTypes();
135         /// resourceTypes.Add("org.tizen.light");
136         /// </code>
137         public void Add(string item)
138         {
139             if (IsValid(item))
140             {
141                 int ret = Interop.IoTConnectivity.Common.ResourceTypes.Add(_resourceTypeHandle, item);
142                 if (ret != (int)IoTConnectivityError.None)
143                 {
144                     Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to add type");
145                     throw IoTConnectivityErrorFactory.GetException(ret);
146                 }
147                 _resourceTypes.Add(item);
148             }
149             else
150             {
151                 Log.Error(IoTConnectivityErrorFactory.LogTag, "Invalid type");
152                 throw IoTConnectivityErrorFactory.GetException((int)IoTConnectivityError.InvalidParameter);
153             }
154         }
155
156         /// <summary>
157         /// Removes a resource type from the list
158         /// </summary>
159         /// <param name="item">The string data to delete from the resource types</param>
160         /// <seealso cref="Add()"/>
161         /// <code>
162         /// ResourceTypes resourceTypes = new ResourceTypes(new List<string>() { "org.tizen.light", "oic.if.room" });
163         /// resourceTypes.Remove("oic.if.room");
164         /// </code>
165         public void Remove(string item)
166         {
167             bool isRemoved = _resourceTypes.Remove(item);
168             if (isRemoved)
169             {
170                 int ret = Interop.IoTConnectivity.Common.ResourceTypes.Remove(_resourceTypeHandle, item);
171                 if (ret != (int)IoTConnectivityError.None)
172                 {
173                     Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to remove type");
174                     throw IoTConnectivityErrorFactory.GetException(ret);
175                 }
176             }
177             else
178                 throw IoTConnectivityErrorFactory.GetException((int)IoTConnectivityError.InvalidParameter);
179         }
180
181         /// <summary>
182         /// Return enumerator for the list of types
183         /// </summary>
184         /// <returns>The enumerator</returns>
185         /// <code>
186         /// ResourceTypes resourceTypes = new ResourceTypes(new List<string>() { "org.tizen.light", "oic.if.room" });
187         /// foreach(string item in resourceTypes)
188         /// {
189         ///     Console.WriteLine("Type : {0}", item);
190         /// }
191         /// </code>
192         public IEnumerator<string> GetEnumerator()
193         {
194             return _resourceTypes.GetEnumerator();
195         }
196
197         /// <summary>
198         /// Return enumerator for the list of types
199         /// </summary>
200         /// <returns>The enumerator</returns>
201         /// <code>
202         /// ResourceTypes resourceTypes = new ResourceTypes(new List<string>() { "org.tizen.light", "oic.if.room" });
203         /// foreach(string item in resourceTypes)
204         /// {
205         ///     Console.WriteLine("Type : {0}", item);
206         /// }
207         /// </code>
208         IEnumerator IEnumerable.GetEnumerator()
209         {
210             return _resourceTypes.GetEnumerator();
211         }
212
213         /// <summary>
214         /// Releases any unmanaged resources used by this object.
215         /// </summary>
216         public void Dispose()
217         {
218             Dispose(true);
219             GC.SuppressFinalize(this);
220         }
221
222         internal static bool IsValid(string type)
223         {
224             Regex r = new Regex("^[a-zA-Z0-9.-]+$");
225             return (type.Length <= MaxLength && char.IsLower(type[0]) && r.IsMatch(type));
226         }
227
228         /// <summary>
229         /// Releases any unmanaged resources used by this object. Can also dispose any other disposable objects.
230         /// </summary>
231         /// <param name="disposing">If true, disposes any disposable objects. If false, does not dispose disposable objects.</param>
232         protected virtual void Dispose(bool disposing)
233         {
234             if (_disposed)
235                 return;
236
237             if (disposing)
238             {
239                 // Free managed objects
240             }
241
242             Interop.IoTConnectivity.Common.ResourceTypes.Destroy(_resourceTypeHandle);
243             _disposed = true;
244         }
245     }
246 }