/* Copyright (c) Microsoft Corporation All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABLITY OR NON-INFRINGEMENT. See the Apache Version 2.0 License for specific language governing permissions and limitations under the License. */ using System; using System.Collections; using System.Collections.Generic; using System.Collections.Specialized; using System.Linq; using System.Text; namespace Microsoft.Research.DryadLinq { /// /// DryadLinq specific (string,string) dictionary that supports a readonly flag. /// internal class DryadLinqStringDictionary : IDictionary { private Dictionary _store = new Dictionary(); private bool _isReadOnly = false; /// /// Creatable only from DryadLinq assembly /// internal DryadLinqStringDictionary() { } //this is useful to transfer data to DryadLinqJobSubmission class internal Dictionary BackingStore { get { return _store; } } private void ThrowIfReadOnly() { if (_isReadOnly) { throw new NotSupportedException(SR.DryadLinqStringDictionaryReadonly); } } public void Add(string key, string value) { ThrowIfReadOnly(); _store.Add(key, value); } public bool ContainsKey(string key) { return _store.ContainsKey(key); } public ICollection Keys { get { return _store.Keys; } } public bool Remove(string key) { ThrowIfReadOnly(); return _store.Remove(key); } public bool TryGetValue(string key, out string value) { return _store.TryGetValue(key, out value); } public ICollection Values { get { return _store.Keys; } } public string this[string key] { get { return _store[key]; } set { ThrowIfReadOnly(); _store[key] = value; } } public void Add(KeyValuePair item) { ThrowIfReadOnly(); _store.Add(item.Key, item.Value); } public void Clear() { ThrowIfReadOnly(); _store.Clear(); } public bool Contains(KeyValuePair item) { return _store.Contains(item); } public void CopyTo(KeyValuePair[] array, int arrayIndex) { ((IDictionary)_store).CopyTo(array, arrayIndex); } public int Count { get { return _store.Count; } } public bool IsReadOnly { get { return this._isReadOnly; } } public bool Remove(KeyValuePair item) { ThrowIfReadOnly(); return ((IDictionary)_store).Remove(item); } public IEnumerator> GetEnumerator() { return _store.GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } /// /// Gets or sets the collection as read only /// internal DryadLinqStringDictionary GetImmutableClone() { DryadLinqStringDictionary clone = new DryadLinqStringDictionary(); foreach (var keyValuePair in this._store) { clone.Add(keyValuePair); } clone._isReadOnly = true; return clone; } } }