// Copyright © 2017-2018 Atomic Software, LLC. All Rights Reserved.
// See LICENSE.md for full license information.

namespace Atom.Core.Collections
{
    public class ConcurrentDictionary<TKey, TValue> : System.Collections.Concurrent.ConcurrentDictionary<TKey, TValue>
    {
        public new int Count { get; set; }

        public bool Add(TKey key, TValue value)
        {
            if (!TryAdd(key, value))
            {
                return false;
            }

            Count++;
            return true;
        }

        public bool Remove(TKey key, out TValue value)
        {
            if (!TryRemove(key, out value))
            {
                return false;
            }

            Count--;
            return true;
        }

        public new TValue this[TKey index]
        {
            get => !ContainsKey(index) ? default(TValue) : base[index];
            set
            {
                if (!ContainsKey(index))
                {
                    return;
                }

                base[index] = value;
            }
        }
    }
}
