// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; using System.Collections; using System.Collections.Generic; using Microsoft.AspNet.Http; namespace Microsoft.AspNet.Http.Core.Collections { /// /// Accessors for query, forms, etc. /// public class ReadableStringCollection : IReadableStringCollection { /// /// Create a new wrapper /// /// public ReadableStringCollection([NotNull] IDictionary store) { Store = store; } private IDictionary Store { get; set; } /// /// Gets the number of elements contained in the collection. /// public int Count { get { return Store.Count; } } /// /// Gets a collection containing the keys. /// public ICollection Keys { get { return Store.Keys; } } /// /// Get the associated value from the collection. Multiple values will be merged. /// Returns null if the key is not present. /// /// /// public string this[string key] { get { return Get(key); } } /// /// Determines whether the collection contains an element with the specified key. /// /// /// public bool ContainsKey(string key) { return Store.ContainsKey(key); } /// /// Get the associated value from the collection. Multiple values will be merged. /// Returns null if the key is not present. /// /// /// public string Get(string key) { return GetJoinedValue(Store, key); } /// /// Get the associated values from the collection in their original format. /// Returns null if the key is not present. /// /// /// public IList GetValues(string key) { string[] values; Store.TryGetValue(key, out values); return values; } /// /// /// /// public IEnumerator> GetEnumerator() { return Store.GetEnumerator(); } /// /// /// /// IEnumerator System.Collections.IEnumerable.GetEnumerator() { return GetEnumerator(); } private static string GetJoinedValue(IDictionary store, string key) { string[] values; if (store.TryGetValue(key, out values)) { return string.Join(",", values); } return null; } } }