internal static int IndexOf(QueryParameterCollection target, KeyValuePair item, bool anyValue = false) { if (item == null) { return(-1); } Monitor.Enter(target._syncRoot); try { Monitor.Enter(item._syncRoot); try { if (item._owner != null) { if (ReferenceEquals(item._owner, target)) { for (int i = 0; i < target._innerList.Count; i++) { if (ReferenceEquals(target._innerList[i], item)) { return(i); } } } } int index = target._innerList.IndexOf(item); if (index < 0 && anyValue) { string key = item.Key; for (int i = 0; i < target._innerList.Count; i++) { if (target._innerList[i].Key == key) { return(i); } } } return(index); } finally { Monitor.Exit(item._syncRoot); } } finally { Monitor.Exit(target._syncRoot); } }
internal static int Add(QueryParameterCollection target, string key, string value) { if (key == null) { throw new ArgumentNullException("key"); } int index; Monitor.Enter(target._syncRoot); try { KeyValuePair item; if (target._innerDictionary.ContainsKey(key)) { List <KeyValuePair> list = target._innerDictionary[key]; item = list.FirstOrDefault(i => i.Key == key); if (item != null) { item.Value = value; return(IndexOf(target, item)); } item = new KeyValuePair(key, value); list.Add(item); } else { item = new KeyValuePair(key, value); target._innerDictionary.Add(key, new List <KeyValuePair> { item }); } index = target._innerList.Count; target._innerList.Add(item); item._owner = target; } finally { Monitor.Exit(target._syncRoot); } return(index); }
internal static int Add(QueryParameterCollection target, KeyValuePair item) { if (item == null) { throw new ArgumentNullException("item"); } int index; Monitor.Enter(item._syncRoot); try { if (item._owner != null) { if (ReferenceEquals(item._owner, target)) { return(IndexOf(target, item)); } return(Add(target, item.Key, item.Value)); } Monitor.Enter(target._syncRoot); try { index = IndexOf(target, item, true); if (index < 0) { if (target._innerDictionary.ContainsKey(item.Key)) { target._innerDictionary[item.Key].Add(item); } else { target._innerDictionary.Add(item.Key, new List <KeyValuePair> { item }); } index = target._innerList.Count; target._innerList.Add(item); } else { KeyValuePair oldItem = target._innerList[index]; Monitor.Enter(oldItem._syncRoot); try { List <KeyValuePair> list = target._innerDictionary[oldItem.Key]; if (list.Count == 1) { list[0] = item; } else { for (int i = 0; i < list.Count; i++) { if (ReferenceEquals(list[i], oldItem)) { list[i] = item; break; } } } target._innerList[index] = item; oldItem._owner = null; } finally { Monitor.Exit(oldItem._syncRoot); } } item._owner = target; } finally { Monitor.Exit(target._syncRoot); } } finally { Monitor.Exit(item._syncRoot); } return(index); }
internal static string Get(QueryParameterCollection target, string key) { throw new NotImplementedException(); }
internal static bool Remove(QueryParameterCollection target, KeyValuePair item) { throw new NotImplementedException(); }
internal static void Set(QueryParameterCollection target, int index, KeyValuePair item) { throw new NotImplementedException(); }
internal static int Set(QueryParameterCollection target, string key, string value) { if (key == null) { throw new ArgumentNullException("key"); } int index; Monitor.Enter(target._syncRoot); try { KeyValuePair item; if (target._innerDictionary.ContainsKey(key)) { List <KeyValuePair> list = target._innerDictionary[key]; item = list.FirstOrDefault(i => i.Key == key); if (item == null) { if (value == null) { item = list.FirstOrDefault(i => i.Value == null); } else { item = list.FirstOrDefault(i => i.Value != null && i.Value == value); } if (item == null) { item = new KeyValuePair(key, value); list.Add(item); item._owner = target; target._innerList.Add(item); } else { if (key.Length == 0) { item._decodedKey = item._encodedKey = ""; } else { item._decodedKey = key; item._encodedKey = null; } if (string.IsNullOrEmpty(value)) { item._decodedValue = item._encodedValue = value; } else { item._decodedValue = value; item._encodedValue = null; } } } else if (string.IsNullOrEmpty(value)) { item._decodedValue = item._encodedValue = value; } else { item._decodedValue = value; item._encodedValue = null; } if (list.Count > 1) { while (!ReferenceEquals(list[0], item)) { Remove(target, list[0]); } while (list.Count > 1) { Remove(target, list[1]); } } return(IndexOf(target, item)); } item = new KeyValuePair(key, value); target._innerDictionary.Add(key, new List <KeyValuePair> { item }); index = target._innerList.Count; target._innerList.Add(item); item._owner = target; } finally { Monitor.Exit(target._syncRoot); } return(index); }