view Collections/DictionaryUtils.cs @ 14:25b953087465

* Rename DictionaryToArrayConverter to DictionaryUtils * Add method to merge arrays no-open-ticket
author IBBoard <dev@ibboard.co.uk>
date Sun, 25 Jan 2009 11:03:38 +0000
parents Collections/DictionaryToArrayConverter.cs@6b762694f051
children 043a1d8101f2
line wrap: on
line source

// DictionaryToArrayConverter.cs is a part of the IBBoard utils library (referred to from here as "this program")
// 
// Copyright (C) 2009 IBBoard
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
//

using System;
using System.Collections.Generic;

namespace IBBoard
{	
	public class DictionaryUtils
	{
		/// <summary>
		/// Takes the set of values in a dictionary and returns them as an array of typed objects.
		/// </summary>
		/// <param name="dictionary">
		/// A <see cref="IDictionary"/> to extract an array of values from
		/// </param>
		/// <returns>
		/// An array of <see cref="VALUE_TYPE"/> objects taken from the Values property of the dictionary, or NULL if the dictionary is NULL
		/// </returns>
		public static  VALUE_TYPE[] Convert<KEY_TYPE, VALUE_TYPE>(IDictionary<KEY_TYPE, VALUE_TYPE> dictionary)
		{
			if (dictionary == null)
			{
				return null;
			}
			
			int entryCount = dictionary.Count;
			VALUE_TYPE[] col = new VALUE_TYPE[entryCount];
			
			if (entryCount > 0)
			{
				dictionary.Values.CopyTo(col, 0);
			}
			
			return col;
		}

		/// <summary>
		/// Takes two dictionaries and merges them. If a key exists in both dictionaries then the value in <code>firstDictionary</code> takes priority.
		/// </summary>
		/// <param name="firstDictionary">
		/// The <see cref="IDictionary"/> to merge values in to
		/// </param>
		/// <param name="secondDictionary">
		/// The <see cref="IDictionary"/> of values to merge in
		/// </param>
		/// <returns>
		/// A <see cref="IDictionary"/> made by adding values from <code>secondDictionary</code> where the key didn't exist in <code>firstDictionary</code>
		/// </returns>
		public static IDictionary<KEY_TYPE, VALUE_TYPE> Merge<KEY_TYPE, VALUE_TYPE>(IDictionary<KEY_TYPE, VALUE_TYPE> firstDictionary, IDictionary<KEY_TYPE, VALUE_TYPE> secondDictionary)
		{
			Dictionary<KEY_TYPE, VALUE_TYPE> mergedDictionary = new Dictionary<KEY_TYPE, VALUE_TYPE>(firstDictionary);

			foreach (KEY_TYPE key in secondDictionary.Keys)
			{
				if (!mergedDictionary.ContainsKey(key))
				{
					mergedDictionary.Add(key, secondDictionary[key]);
				}
			}

			return mergedDictionary;
		}
	}
}