internal static void SetValue(UrlQueryParameter qp, string value)
                {
                    Collection changedColl = qp.GetSync(() =>
                    {
                        if (qp._set == null)
                        {
                            qp._value = value;
                            return(null);
                        }
                        return(qp._set.GetSync(() =>
                        {
                            if ((qp._value == null) ? value == null : value != null && value == qp._value)
                            {
                                return null;
                            }
                            qp._value = value;
                            return qp._set._collection;
                        }));
                    });

                    if (changedColl != null)
                    {
                        changedColl.OnChange();
                    }
                }
 public string this[string key]
 {
     get
     {
         return(GetSync(() =>
         {
             ValueSet vs = ValueSet.Get(this, key);
             if (vs == null)
             {
                 throw new KeyNotFoundException();
             }
             return vs.GetSync(() =>
             {
                 if (vs.Key != key)
                 {
                     for (UrlQueryParameter p = vs.First._nextValue; p != null; p = p._nextValue)
                     {
                         if (p._key != null && p._key == key)
                         {
                             return p.Value;
                         }
                     }
                 }
                 return vs.First.Value;
             });
         }));
     }
     set { ValueSet.Set(this, key, value); }
 }
            public IEnumerable <string> GetAllValues(string key)
            {
                ValueSet vs = ValueSet.Get(this, key);

                if (vs != null)
                {
                    for (UrlQueryParameter p = vs.First; p != null; p = p._nextValue)
                    {
                        if (p.GetSync(() => p._set == null || !ReferenceEquals(p._set, vs)))
                        {
                            throw new InvalidOperationException("Collection changed");
                        }
                        yield return(p.ToString());
                    }
                }
            }
 public UrlQueryParameter this[int index]
 {
     get
     {
         return(GetSync(() =>
         {
             int i = -1;
             for (UrlQueryParameter p = _firstParameter; p != null; p = p._nextParameter)
             {
                 if (++i == index)
                 {
                     return p;
                 }
             }
             throw new IndexOutOfRangeException();
         }));
     }
 }
                internal static void SetKey(UrlQueryParameter qp, string key)
                {
                    Collection changedColl = qp.GetSync(() =>
                    {
                        if (qp._set == null)
                        {
                            qp._key = key;
                            return(null);
                        }
                        return(qp._set.GetSync(() =>
                        {
                            if (qp.Key == key)
                            {
                                return null;
                            }
                            if (qp._set._collection == null)
                            {
                                // TODO: Not in collection
                                return null;
                            }
                            return qp._set._collection.GetSync((Collection c) =>
                            {
                                if (c._keyComparer.Equals(key, qp.Key))
                                {
                                    // TODO: Modify key
                                }
                                else
                                {
                                    // TODO: Ttoal key change
                                }
                                return c;
                            }, qp._set._collection);
                        }));
                    });

                    if (changedColl != null)
                    {
                        changedColl.OnChange();
                    }
                }
 internal static void Clear(Collection collection)
 {
     if (collection.GetSync(() =>
     {
         if (collection._firstSet == null)
         {
             return(false);
         }
         do
         {
             collection._firstSet.InvokeSync((ValueSet vs) =>
             {
                 if (vs._previous != null || collection._firstSet == null || !ReferenceEquals(collection._firstSet, vs))
                 {
                     throw new InvalidOperationException("Collection was modified");
                 }
                 for (UrlQueryParameter qp = vs._firstParameter; qp != null; qp = vs._firstParameter)
                 {
                     qp.InvokeSync(() =>
                     {
                         if (qp._previousValue != null || qp._set == null || !ReferenceEquals(qp._set, vs))
                         {
                             throw new InvalidOperationException("Collection was modified");
                         }
                         if ((vs._firstParameter = qp._nextValue) != null)
                         {
                             qp._nextValue = qp._nextValue._previousValue = null;
                         }
                         if (qp._key == null)
                         {
                             qp._key = vs._key;
                         }
                         qp._set = null;
                         if (qp._previousParameter == null)
                         {
                             if ((collection._firstParameter = qp._nextParameter) == null)
                             {
                                 collection._lastParameter = null;
                             }
                             else
                             {
                                 qp._nextParameter = qp._nextParameter._previousParameter = null;
                             }
                         }
                         else
                         {
                             if ((qp._previousParameter._nextParameter = qp._nextParameter) == null)
                             {
                                 collection._lastParameter = qp._previousParameter;
                             }
                             else
                             {
                                 qp._nextParameter._previousParameter = qp._previousParameter;
                                 qp._nextParameter = null;
                             }
                             qp._previousParameter = null;
                         }
                     });
                 }
                 if ((collection._firstSet = vs._next) == null)
                 {
                     collection._lastSet = null;
                 }
                 else
                 {
                     vs._next = collection._lastSet._previous = null;
                 }
                 vs._collection = null;
             }, collection._firstSet);
         } while (collection._firstSet != null);
         return(true);
     }))
     {
         collection.OnChange();
     }
 }
                internal static bool Remove(UrlQueryParameter qp)
                {
                    if (qp == null)
                    {
                        return(false);
                    }
                    Collection changedCol = qp.GetSync(() =>
                    {
                        if (qp._set == null)
                        {
                            return(null);
                        }
                        return(qp._set.GetSync((ValueSet vs) =>
                        {
                            if (vs._collection == null)
                            {
                                if (qp._previousValue == null)
                                {
                                    if ((vs._firstParameter = qp._nextValue) == null)
                                    {
                                        vs._lastParameter = null;
                                    }
                                    else
                                    {
                                        qp._nextValue = qp._nextValue._previousValue = null;
                                    }
                                }
                                else
                                {
                                    if ((qp._previousValue._nextValue = qp._nextValue) == null)
                                    {
                                        vs._lastParameter = qp._previousValue;
                                    }
                                    else
                                    {
                                        qp._nextValue._previousValue = qp._previousValue;
                                        qp._nextValue = null;
                                    }
                                    qp._previousValue = null;
                                }
                                qp._set = null;
                                return null;
                            }
                            vs._collection.GetSync((Collection c) =>
                            {
                                if (qp._previousParameter == null)
                                {
                                    if ((c._firstParameter = qp._nextParameter) == null)
                                    {
                                        c._lastParameter = vs._firstParameter = vs._lastParameter = null;
                                        c._firstSet = c._lastSet = qp._set = vs._next = vs._previous = null;
                                        vs._collection = null;
                                        return c;
                                    }
                                    qp._nextParameter = qp._nextParameter._previousParameter = null;
                                }
                                else
                                {
                                    if ((qp._previousParameter._nextParameter = qp._nextParameter) == null)
                                    {
                                        c._lastParameter = qp._previousParameter;
                                    }
                                    else
                                    {
                                        qp._nextParameter._previousParameter = qp._previousParameter;
                                        qp._nextParameter = null;
                                    }
                                    qp._previousParameter = null;
                                }

                                if (qp._previousValue == null)
                                {
                                    if ((vs._firstParameter = qp._nextValue) == null)
                                    {
                                        vs._lastParameter = null;
                                        if (vs._previous == null)
                                        {
                                            (c._firstSet = vs._next)._previous = null;
                                            vs._next = null;
                                        }
                                        else
                                        {
                                            if ((vs._previous._next = vs._next) == null)
                                            {
                                                c._lastSet = vs._previous;
                                            }
                                            else
                                            {
                                                vs._next._previous = vs._previous;
                                                vs._next = null;
                                            }
                                            vs._previous = null;
                                        }
                                        vs._collection = null;
                                        if (qp._key == null)
                                        {
                                            qp._key = vs._key;
                                        }
                                        qp._set = null;
                                    }
                                    else
                                    {
                                        qp._nextValue = qp._nextValue._previousValue = null;
                                    }
                                }
                                else
                                {
                                    if ((qp._previousValue._nextValue = qp._nextValue) == null)
                                    {
                                        vs._lastParameter = qp._previousValue;
                                    }
                                    else
                                    {
                                        qp._nextValue._previousValue = qp._previousValue;
                                        qp._nextValue = null;
                                    }
                                    qp._previousValue = null;
                                }
                                qp._set = null;
                                return c;
                            }, vs._collection);
                            return vs._collection;
                        }, qp._set));
                    });

                    if (changedCol == null)
                    {
                        return(false);
                    }
                    changedCol.OnChange();
                    return(true);
                }
                internal static bool Remove(ValueSet vs)
                {
                    if (vs == null)
                    {
                        return(false);
                    }
                    Collection changedColl = vs.GetSync(() =>
                    {
                        if (vs._collection == null)
                        {
                            return(null);
                        }

                        return(vs._collection.GetSync((Collection c) =>
                        {
                            for (UrlQueryParameter qp = vs._firstParameter; qp != null; qp = vs._firstParameter)
                            {
                                qp.InvokeSync(() =>
                                {
                                    if (qp._previousValue != null || qp._set == null || !ReferenceEquals(qp._set, vs))
                                    {
                                        throw new InvalidOperationException("Collection was modified");
                                    }
                                    if ((vs._firstParameter = qp._nextValue) != null)
                                    {
                                        qp._nextValue = qp._nextValue._previousValue = null;
                                    }
                                    if (qp._key == null)
                                    {
                                        qp._key = vs._key;
                                    }
                                    qp._set = null;
                                    if (qp._previousParameter == null)
                                    {
                                        if ((c._firstParameter = qp._nextParameter) == null)
                                        {
                                            c._lastParameter = null;
                                        }
                                        else
                                        {
                                            qp._nextParameter = qp._nextParameter._previousParameter = null;
                                        }
                                    }
                                    else
                                    {
                                        if ((qp._previousParameter._nextParameter = qp._nextParameter) == null)
                                        {
                                            c._lastParameter = qp._previousParameter;
                                        }
                                        else
                                        {
                                            qp._nextParameter._previousParameter = qp._previousParameter;
                                            qp._nextParameter = null;
                                        }
                                        qp._previousParameter = null;
                                    }
                                });
                            }
                            if (vs._previous == null)
                            {
                                if ((c._firstSet = vs._next) == null)
                                {
                                    c._lastSet = null;
                                }
                                else
                                {
                                    vs._next = vs._next._previous = null;
                                }
                            }
                            else
                            {
                                if ((vs._previous._next = vs._next) == null)
                                {
                                    c._lastSet = vs._previous;
                                }
                                else
                                {
                                    vs._next._previous = vs._previous;
                                    vs._next = null;
                                }
                                vs._previous = null;
                            }
                            vs._collection = null;
                            return c;
                        }, vs._collection));
                    });

                    if (changedColl == null)
                    {
                        return(false);
                    }
                    changedColl.OnChange();
                    return(true);
                }
                internal static void Set(Collection collection, string key, string value)
                {
                    if (collection.GetSync(() =>
                    {
                        ValueSet vs = Get(collection, key);
                        if (vs == null)
                        {
                            vs = new ValueSet(collection, key);

                            if ((vs._previous = collection._lastSet) == null)
                            {
                                collection._firstSet = vs;
                            }
                            else
                            {
                                vs._previous._next = vs;
                            }
                            collection._lastSet = vs;
                            vs._firstParameter = vs._lastParameter = new UrlQueryParameter(null, value, vs);
                            if ((vs._lastParameter._previousParameter = collection._lastParameter) == null)
                            {
                                collection._firstParameter = vs._lastParameter;
                            }
                            else
                            {
                                collection._lastParameter._nextParameter = vs._lastParameter;
                            }
                            collection._lastParameter = vs._lastParameter;
                            return(true);
                        }
                        return(vs.GetSync(() =>
                        {
                            UrlQueryParameter qp = vs._firstParameter._nextValue;
                            if (key == vs._key)
                            {
                                if ((vs._firstParameter._value == null) ? value == null : value != null && value == vs._firstParameter._value)
                                {
                                    if (qp == null)
                                    {
                                        return false;
                                    }
                                }
                            }
                            do
                            {
                                if (qp._previousValue != null)
                                {
                                    qp._previousValue._nextValue = null;
                                    qp._previousValue = null;
                                }
                                if (qp._previousParameter == null)
                                {
                                    if ((collection._firstParameter = qp._nextParameter) == null)
                                    {
                                        collection._lastParameter = null;
                                    }
                                    else
                                    {
                                        qp._nextParameter = qp._nextParameter._previousParameter = null;
                                    }
                                }
                                else
                                {
                                    if ((qp._previousParameter._nextParameter = qp._nextParameter) == null)
                                    {
                                        collection._lastParameter = qp._previousParameter;
                                    }
                                    else
                                    {
                                        qp._nextParameter._previousParameter = qp._previousParameter;
                                        qp._nextParameter = null;
                                    }
                                    qp._previousParameter = null;
                                }
                            } while ((qp = qp._nextValue) != null);
                            vs._firstParameter._key = null;
                            vs._firstParameter._value = value;
                            vs._key = key;
                            return true;
                        }));
                    }))
                    {
                        collection.OnChange();
                    }
                }
 public bool Remove(UrlQueryParameter parameter)
 {
     return(parameter != null && ValueSet.Remove(parameter));
 }