using System; using System.Collections; using System.Collections.Generic; using System.Linq; namespace SharpCifs.Util.Sharpen { public abstract class AbstractMap : IDictionary { public virtual void Clear() { EntrySet().Clear(); } public virtual bool ContainsKey(object name) { return EntrySet().Any(p => p.Key.Equals((T)name)); } public abstract ICollection> EntrySet(); public virtual TU Get(object key) { return EntrySet().Where(p => p.Key.Equals(key)).Select(p => p.Value).FirstOrDefault(); } protected virtual IEnumerator> InternalGetEnumerator() { return EntrySet().GetEnumerator(); } public virtual bool IsEmpty() { return !EntrySet().Any(); } public virtual TU Put(T key, TU value) { throw new NotSupportedException(); } public virtual TU Remove(object key) { Iterator iterator = EntrySet() as Iterator; if (iterator == null) { throw new NotSupportedException(); } while (iterator.HasNext()) { TU local = iterator.Next(); if (local.Equals((T)key)) { iterator.Remove(); return local; } } return default(TU); } void ICollection>.Add(KeyValuePair item) { Put(item.Key, item.Value); } bool ICollection>.Contains(KeyValuePair item) { throw new NotImplementedException(); } void ICollection>.CopyTo(KeyValuePair[] array, int arrayIndex) { EntrySet().CopyTo(array, arrayIndex); } bool ICollection>.Remove(KeyValuePair item) { Remove(item.Key); return true; } void IDictionary.Add(T key, TU value) { Put(key, value); } bool IDictionary.ContainsKey(T key) { return ContainsKey(key); } bool IDictionary.Remove(T key) { if (ContainsKey(key)) { Remove(key); return true; } return false; } bool IDictionary.TryGetValue(T key, out TU value) { if (ContainsKey(key)) { value = Get(key); return true; } value = default(TU); return false; } IEnumerator> IEnumerable>.GetEnumerator() { return InternalGetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() { return InternalGetEnumerator(); } public virtual int Count { get { return EntrySet().Count; } } public TU this[T key] { get { return Get(key); } set { Put(key, value); } } public virtual IEnumerable Keys { get { return EntrySet().Select(p => p.Key); } } int ICollection>.Count { get { return Count; } } bool ICollection>.IsReadOnly { get { return false; } } ICollection IDictionary.Keys { get { return Keys.ToList(); } } ICollection IDictionary.Values { get { return Values.ToList(); } } public virtual IEnumerable Values { get { return EntrySet().Select(p => p.Value); } } } }