-/// Copyright 2016 by Samsung Electronics, Inc.,
-///
-/// This software is the confidential and proprietary information
-/// of Samsung Electronics, Inc. ("Confidential Information"). You
-/// shall not disclose such Confidential Information and shall use
-/// it only in accordance with the terms of the license agreement
-/// you entered into with Samsung.
+ /*
+ * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the License);
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an AS IS BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
using System;
using System.Collections;
namespace Tizen.Network.IoTConnectivity
{
/// <summary>
- /// Class to manage query of request.
+ /// This class provides APIs to manage query of request.
/// </summary>
+ /// <since_tizen>3</since_tizen>
public class ResourceQuery : IDictionary<string, string>, IDisposable
{
internal const int QueryMaxLenth = 64;
private bool _disposed = false;
/// <summary>
- /// Constructor
+ /// The resource query constructor
/// </summary>
+ /// <since_tizen>3</since_tizen>
+ /// <feature>http://tizen.org/feature/iot.ocf</feature>
+ /// <seealso cref="Add()"/>
+ /// <seealso cref="Remove()"/>
+ /// <exception cref="NotSupportedException">Thrown when the iotcon is not supported</exception>
+ /// <exception cref="OutOfMemoryException">Thrown when there is not enough memory</exception>
+ /// <code>
+ /// ResourceQuery query = new ResourceQuery();
+ /// </code>
public ResourceQuery()
{
int ret = Interop.IoTConnectivity.Common.Query.Create(out _resourceQueryHandle);
}
}
+ /// <summary>
+ /// Destructor of the ResourceQuery class.
+ /// </summary>
~ResourceQuery()
{
Dispose(false);
}
/// <summary>
- /// Resource type of the query
+ /// Gets and sets the resource type of the query
/// </summary>
+ /// <since_tizen>3</since_tizen>
+ /// <value>The resource type of the query.</value>
+ /// <exception cref="NotSupportedException">Thrown when the iotcon is not supported</exception>
+ /// <exception cref="ArgumentException">Thrown when there is an invalid parameter</exception>
+ /// <exception cref="InvalidOperationException">Thrown when the operation is invalid</exception>
+ /// <code>
+ /// ResourceQuery query = new ResourceQuery();
+ /// query.Type = "org.tizen.light";
+ /// Console.WriteLine("Type of query : {0}", query.Type);
+ /// </code>
public string Type
{
get
if (ret != (int)IoTConnectivityError.None)
{
Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get type");
- throw IoTConnectivityErrorFactory.GetException(ret);
+ return "";
}
- return Marshal.PtrToStringAuto(type);
+ return (type != IntPtr.Zero) ? Marshal.PtrToStringAnsi(type) : string.Empty;
}
set
{
}
/// <summary>
- /// Resource interface of the query
+ /// Gets and sets the resource interface of the query
/// </summary>
+ /// <since_tizen>3</since_tizen>
+ /// <value>
+ /// The resource interface of the query.
+ /// Setter value could be a value such as <see cref="ResourceInterfaces.DefaultInterface"/>
+ /// </value>
+ /// <exception cref="NotSupportedException">Thrown when the iotcon is not supported</exception>
+ /// <exception cref="ArgumentException">Thrown when there is an invalid parameter</exception>
+ /// <exception cref="InvalidOperationException">Thrown when the operation is invalid</exception>
+ /// <code>
+ /// ResourceQuery query = new ResourceQuery();
+ /// query.Interface = ResourceInterfaces.LinkInterface;
+ /// </code>
public string Interface
{
get
if (ret != (int)IoTConnectivityError.None)
{
Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to get interface");
- throw IoTConnectivityErrorFactory.GetException(ret);
+ return "";
}
- return Marshal.PtrToStringAuto(iface);
+ return (iface != IntPtr.Zero) ? Marshal.PtrToStringAnsi(iface) : string.Empty;
}
set
{
}
/// <summary>
- /// Contains all keys of Query
+ /// Contains all the query keys
/// </summary>
+ /// <since_tizen>3</since_tizen>
+ /// <value>All the query keys.</value>
+ /// <code>
+ /// ResourceQuery query = new ResourceQuery();
+ /// query.Add("key", "value");
+ /// query.Add("newKey", "sample value");
+ /// var keys = query.Keys;
+ /// Console.WriteLine("Resource query contains keys {0} and {1}", keys.ElementAt(0), keys.ElementAt(1));
+ /// </code>
public ICollection<string> Keys
{
get
}
/// <summary>
- /// Contains all the values of Query
+ /// Contains all the query values
/// </summary>
+ /// <since_tizen>3</since_tizen>
+ /// <value>All the query values.</value>
+ /// <code>
+ /// ResourceQuery query = new ResourceQuery();
+ /// query.Add("key", "value");
+ /// query.Add("newKey", "sample value");
+ /// var values = query.Values;
+ /// Console.WriteLine("Resource query contains values {0} and {1}", values.ElementAt(0), values.ElementAt(1));
+ /// </code>
public ICollection<string> Values
{
get
/// <summary>
/// Gets the number of query elements
/// </summary>
+ /// <since_tizen>3</since_tizen>
+ /// <value>The number of query elements.</value>
+ /// <code>
+ /// ResourceQuery query = new ResourceQuery();
+ /// query.Add("key", "value");
+ /// query.Add("newKey", "sample value");
+ /// var count = query.Count;
+ /// Console.WriteLine("There are {0} keys in the query object", count);
+ /// </code>
public int Count
{
get
}
/// <summary>
- /// Represents whether Query is readonly
+ /// Represents whether the collection is readonly
/// </summary>
+ /// <since_tizen>3</since_tizen>
+ /// <value>Whether the collection is readonly.</value>
+ /// <code>
+ /// ResourceQuery query = new ResourceQuery();
+ /// if (query.IsReadOnly)
+ /// Console.WriteLine("Read only query");
+ /// </code>
public bool IsReadOnly
{
get
}
/// <summary>
- /// Gets or sets the Query key..
+ /// Gets or sets the query data
/// </summary>
- /// <param name="key">The key of the Query to get or set.</param>
- /// <returns>The element with the specified key.</returns>
+ /// <since_tizen>3</since_tizen>
+ /// <value>The query data.</value>
+ /// <param name="key">The query key to get or set.</param>
+ /// <returns>The query with the specified key.</returns>
+ /// <code>
+ /// ResourceQuery query = new ResourceQuery();
+ /// query["key1"] = "sample-data";
+ /// Console.WriteLine("query has : {0}", query["key1"]);
+ /// </code>
public string this[string key]
{
get
}
/// <summary>
- /// Checks the given key exists in Query
+ /// Checks whether the given key exists in Query collection
/// </summary>
- /// <param name="key">The Query key</param>
+ /// <since_tizen>3</since_tizen>
+ /// <param name="key">The key to look for</param>
/// <returns>true if exists. Otherwise, false</returns>
+ /// <code>
+ /// ResourceQuery query = new ResourceQuery();
+ /// query.Add("key1", "value1");
+ /// if (query.ContainsKey("key1"))
+ /// Console.WriteLine("query conatins key : key1");
+ /// </code>
public bool ContainsKey(string key)
{
return _query.ContainsKey(key);
}
/// <summary>
- /// Adds Query element
+ /// Adds a new key and correspoding value into the query.
/// </summary>
- /// <param name="key">The key representing the Query</param>
- /// <param name="value">The value representing the Query</param>
+ /// <since_tizen>3</since_tizen>
+ /// <remarks>
+ /// The full length of query should be less than or equal to 64.
+ /// </remarks>
+ /// <param name="key">The key of the query to insert</param>
+ /// <param name="value">The string data to insert into the query</param>
+ /// <feature>http://tizen.org/feature/iot.ocf</feature>
+ /// <seealso cref="Remove()"/>
+ /// <exception cref="NotSupportedException">Thrown when the iotcon is not supported</exception>
+ /// <exception cref="ArgumentException">Thrown when there is an invalid parameter</exception>
+ /// <exception cref="InvalidOperationException">Thrown when the operation is invalid</exception>
+ /// <code>
+ /// ResourceQuery query = new ResourceQuery();
+ /// query.Add("key1", "value1");
+ /// </code>
public void Add(string key, string value)
{
if (CanAddQuery(key, value))
}
/// <summary>
- /// Removes a Query element from collection
+ /// Removes the key and its associated value from the query.
/// </summary>
- /// <param name="item">The Query element to remove</param>
- /// <returns>true if operation is success. Otherwise, false</returns>
+ /// <since_tizen>3</since_tizen>
+ /// <param name="key">The id of the query to delete</param>
+ /// <returns>True if operation is successful. Otherwise, false</returns>
+ /// <feature>http://tizen.org/feature/iot.ocf</feature>
+ /// <seealso cref="Add()"/>
+ /// <exception cref="NotSupportedException">Thrown when the iotcon is not supported</exception>
+ /// <exception cref="ArgumentException">Thrown when there is an invalid parameter</exception>
+ /// <exception cref="InvalidOperationException">Thrown when the operation is invalid</exception>
+ /// <code>
+ /// ResourceQuery query = new ResourceQuery();
+ /// query.Add("key1", "value1");
+ /// var result = query.Remove("key1");
+ /// </code>
public bool Remove(string key)
{
int ret = Interop.IoTConnectivity.Common.Query.Remove(_resourceQueryHandle, key);
throw IoTConnectivityErrorFactory.GetException(ret);
}
- return _query.Remove(key);
+ bool isRemoved = _query.Remove(key);
+
+ return isRemoved;
}
/// <summary>
/// Gets the value associated with the specified key.
/// </summary>
- /// <param name="key">The key whose value to get.</param>
- /// <param name="value"> The value associated with the specified key</param>
- /// <returns> true if the Query contains an element with the specified key; otherwise, false.</returns>
+ /// <since_tizen>3</since_tizen>
+ /// <param name="key">The query key</param>
+ /// <param name="value">Value corresponding to query key</param>
+ /// <returns>True if the key exists, false otherwise</returns>
+ /// <code>
+ /// ResourceQuery query = new ResourceQuery();
+ /// query.Add("key1", "value1");
+ /// string value;
+ /// var isPresent = query.TryGetValue("key1", out value);
+ /// if (isPresent)
+ /// Console.WriteLine("value : {0}", value);
+ /// </code>
public bool TryGetValue(string key, out string value)
{
return _query.TryGetValue(key, out value);
}
/// <summary>
- /// Adds Query as a key value pair
+ /// Adds query key and value as a key value pair
/// </summary>
+ /// <since_tizen>3</since_tizen>
/// <param name="item">The key value pair</param>
+ /// <feature>http://tizen.org/feature/iot.ocf</feature>
+ /// <seealso cref="Remove()"/>
+ /// <code>
+ /// ResourceQuery query = new ResourceQuery();
+ /// query.Add(new KeyValuePair<string, string>("key1", "value1"));
+ /// </code>
public void Add(KeyValuePair<string, string> item)
{
Add(item.Key, item.Value);
}
/// <summary>
- /// Clears Query
+ /// Clears the Query collection
/// </summary>
+ /// <since_tizen>3</since_tizen>
+ /// <feature>http://tizen.org/feature/iot.ocf</feature>
+ /// <exception cref="NotSupportedException">Thrown when the iotcon is not supported</exception>
+ /// <exception cref="InvalidOperationException">Thrown when the operation is invalid</exception>
+ /// <code>
+ /// ResourceQuery query = new ResourceQuery();
+ /// query.Add("key1", "value1");
+ /// query.Add("key2", "value2");
+ /// query.Clear();
+ /// </code>
public void Clear()
{
foreach (string key in _query.Keys)
}
/// <summary>
- /// Checks the given key value pair exists in Query
+ /// Checks if the given query pair exists
/// </summary>
+ /// <since_tizen>3</since_tizen>
/// <param name="item">The key value pair</param>
- /// <returns>true if exists. Otherwise, false</returns>
+ /// <returns>True if exists. Otherwise, false</returns>
+ /// <code>
+ /// ResourceQuery query = new ResourceQuery();
+ /// query.Add(new KeyValuePair<string, string>("key1", "value1"));
+ /// var isPresent = query.Contains(new KeyValuePair<string, string>("key1", "value1"));
+ /// if (isPresent)
+ /// Console.WriteLine("Key value pair is present");
+ /// </code>
public bool Contains(KeyValuePair<string, string> item)
{
return _query.Contains(item);
}
/// <summary>
- /// Copies the elements of the Query to an Array, starting at a particular index.
+ /// Copies the elements of the query collection to an Array, starting at a particular index.
/// </summary>
+ /// <since_tizen>3</since_tizen>
/// <param name="array">The destination array</param>
- /// <param name="arrayIndex">The zero-based index in array at which copying begins.</param>
+ /// <param name="arrayIndex">Index parameter</param>
+ /// <code>
+ /// ResourceQuery query = new ResourceQuery();
+ /// query.Add(new KeyValuePair<string, string>("key1", "value1"));
+ /// KeyValuePair<string, string>[] dest = new KeyValuePair<string, string>[query.Count];
+ /// query.CopyTo(dest, 0);
+ /// Console.WriteLine("Dest conatins ({0}, {1})", dest[0].Key, dest[0].Value);
+ /// </code>
public void CopyTo(KeyValuePair<string, string>[] array, int arrayIndex)
{
_query.CopyTo(array, arrayIndex);
}
/// <summary>
- /// Removes a Query element from collection
+ /// Remove the given key value pair from the query
/// </summary>
- /// <param name="item">The Query element to remove</param>
- /// <returns>true if operation is success. Otherwise, false</returns>
+ /// <since_tizen>3</since_tizen>
+ /// <param name="item">The key value pair to remove</param>
+ /// <returns>True if operation is successful. Otherwise, false</returns>
+ /// <feature>http://tizen.org/feature/iot.ocf</feature>
+ /// <seealso cref="Add()"/>
+ /// <exception cref="ArgumentException">Thrown when there is an invalid parameter</exception>
+ /// <code>
+ /// ResourceQuery query = new ResourceQuery();
+ /// query.Add(new KeyValuePair<string, string>("key1", "value1"));
+ /// var result = query.Remove(new KeyValuePair<string, string>("key1", "value1"));
+ /// </code>
public bool Remove(KeyValuePair<string, string> item)
{
- int ret = Interop.IoTConnectivity.Common.Query.Remove(_resourceQueryHandle, item.Key);
- if (ret != (int)IoTConnectivityError.None)
- {
- Log.Error(IoTConnectivityErrorFactory.LogTag, "Failed to remove query");
- throw IoTConnectivityErrorFactory.GetException(ret);
- }
-
- return _query.Remove(item);
+ return Remove(item.Key);
}
/// <summary>
- /// Returns an enumerator that iterates through the collection.
+ /// Get the enumerator to query collection
/// </summary>
- /// <returns> An enumerator that can be used to iterate through the collection.</returns>
+ /// <since_tizen>3</since_tizen>
+ /// <returns>Enumerator to query pairs</returns>
+ /// <code>
+ /// ResourceQuery query = new ResourceQuery();
+ /// query.Add(new KeyValuePair<string, string>("key1", "value1"));
+ /// query.Add(new KeyValuePair<string, string>("key2", "value2"));
+ /// foreach (KeyValuePair<string, string> pair in query)
+ /// {
+ /// Console.WriteLine("key : {0}, value : {1}", pair.Key, pair.Value);
+ /// }
+ /// </code>
public IEnumerator<KeyValuePair<string, string>> GetEnumerator()
{
return _query.GetEnumerator();
}
+ /// <summary>
+ /// Get the enumerator to query collection
+ /// </summary>
+ /// <since_tizen>3</since_tizen>
+ /// <returns>Enumerator to query pairs</returns>
+ /// <code>
+ /// ResourceQuery query = new ResourceQuery();
+ /// query.Add(new KeyValuePair<string, string>("key1", "value1"));
+ /// query.Add(new KeyValuePair<string, string>("key2", "value2"));
+ /// foreach (KeyValuePair<string, string> pair in query)
+ /// {
+ /// Console.WriteLine("key : {0}, value : {1}", pair.Key, pair.Value);
+ /// }
+ /// </code>
IEnumerator IEnumerable.GetEnumerator()
{
return _query.GetEnumerator();
}
+ /// <summary>
+ /// Releases any unmanaged resources used by this object.
+ /// </summary>
+ /// <since_tizen>3</since_tizen>
+ /// <feature>http://tizen.org/feature/iot.ocf</feature>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
+ /// <summary>
+ /// Releases any unmanaged resources used by this object. Can also dispose any other disposable objects.
+ /// </summary>
+ /// <since_tizen>3</since_tizen>
+ /// <param name="disposing">If true, disposes any disposable objects. If false, does not dispose disposable objects.</param>
+ /// <feature>http://tizen.org/feature/iot.ocf</feature>
protected virtual void Dispose(bool disposing)
{
if (_disposed)
int queryLenth = 0;
foreach (string key in Keys)
{
- queryLenth += key.Length;
+ queryLenth += key.Length + 2;
}
foreach (string value in Values)
{
queryLenth += value.Length;
}
- if ((newKey.Length + newValue.Length + queryLenth) < QueryMaxLenth)
+ if ((newKey.Length + newValue.Length + queryLenth + 2) < QueryMaxLenth)
return true;
return false;