/*
* Copyright(c) 2008 Solutions Design.
* Copyright(c) 2017 openmetaverse.co.
* All rights reserved.
*
* - Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* - Neither the name of the openmetaverse.co nor the names
* of its contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT(INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
using System;
using System.Collections.Generic;
namespace LibreMetaverse
{
///
/// Extension to the normal Dictionary. This class can store more than one value for every key. It keeps a HashSet for every Key value.
/// Calling Add with the same Key and multiple values will store each value under the same Key in the Dictionary. Obtaining the values
/// for a Key will return the HashSet with the Values of the Key.
///
/// The type of the key.
/// The type of the value.
public class MultiValueDictionary : Dictionary>
{
///
/// Adds the specified value under the specified key
///
/// The key.
/// The value.
public void Add(TKey key, TValue value)
{
if (key == null) throw new ArgumentNullException(nameof(key));
IList container;
if (!TryGetValue(key, out container))
{
container = new List();
base.Add(key, container);
}
container.Add(value);
}
///
/// Determines whether this dictionary contains the specified value for the specified key
///
/// The key.
/// The value.
/// true if the value is stored for the specified key in this dictionary, false otherwise
public bool ContainsValue(TKey key, TValue value)
{
if (key == null) throw new ArgumentNullException(nameof(key));
bool toReturn = false;
IList values;
if (TryGetValue(key, out values))
{
toReturn = values.Contains(value);
}
return toReturn;
}
///
/// Removes the specified value for the specified key. It will leave the key in the dictionary.
///
/// The key.
/// The value.
public void Remove(TKey key, TValue value)
{
if (key == null) throw new ArgumentNullException(nameof(key));
IList container;
if (TryGetValue(key, out container))
{
container.Remove(value);
if (container.Count <= 0)
{
Remove(key);
}
}
}
///
/// Merges the specified multivaluedictionary into this instance.
///
/// To merge with.
public void Merge(MultiValueDictionary toMergeWith)
{
if (toMergeWith == null)
{
return;
}
foreach (var pair in toMergeWith)
{
foreach (TValue value in pair.Value)
{
Add(pair.Key, value);
}
}
}
///
/// Gets the values for the key specified. This method is useful if you want to avoid an exception for key value retrieval and you can't use TryGetValue
/// (e.g. in lambdas)
///
/// The key.
/// if set to true and the key isn't found, an empty hashset is returned, otherwise, if the key isn't found, null is returned
///
/// This method will return null (or an empty set if returnEmptySet is true) if the key wasn't found, or
/// the values if key was found.
///
public IList GetValues(TKey key, bool returnEmptySet)
{
IList toReturn;
if (!TryGetValue(key, out toReturn) && returnEmptySet)
{
toReturn = new List();
}
return toReturn;
}
}
}