/* * Copyright Amazon.com, Inc. or its affiliates. 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. * A copy of the License is located at * * http://aws.amazon.com/apache2.0 * * or in the "license" file accompanying this file. This file is distributed * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either * express or implied. See the License for the specific language governing * permissions and limitations under the License. */ using System.Collections.Generic; namespace Amazon.Extensions.S3.Encryption.Utils { internal class ConcurrentDictionary { private readonly object _lock = new object(); private readonly Dictionary _internalDictionary = new Dictionary(); public ValueType this[KeyType key] { get { lock (_lock) { return _internalDictionary[key]; } } set { lock (_lock) { _internalDictionary[key] = value; } } } public bool TryGetValue(KeyType key, out ValueType value) { lock (_lock) { return _internalDictionary.TryGetValue(key, out value); } } public bool ContainsKey(KeyType key) { lock (_lock) { return _internalDictionary.ContainsKey(key); } } public bool TryAdd(KeyType key, ValueType value) { lock (_lock) { if (_internalDictionary.ContainsKey(key)) { return false; } else { _internalDictionary.Add(key, value); return true; } } } public bool TryRemove(KeyType key, out ValueType value) { lock (_lock) { if (_internalDictionary.ContainsKey(key)) { value = _internalDictionary[key]; return _internalDictionary.Remove(key); } else { value = default; return false; } } } } }