#region Copyright 2012-2014 by Roger Knapp, Licensed under the Apache License, Version 2.0
/* 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
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License 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.
*/
#endregion
using System;
namespace CSharpTest.Net.Collections
{
/// Provides a delegate that performs an atomic update of a key/value pair
public delegate TValue KeyValueUpdate(TKey key, TValue original);
/// Provides a delegate that performs a test on key/value pair
public delegate bool KeyValuePredicate(TKey key, TValue original);
///
/// An interface to provide conditional or custom creation logic to a concurrent dictionary.
///
public interface ICreateValue
{
///
/// Called when the key was not found within the dictionary to produce a new value that can be added.
/// Return true to continue with the insertion, or false to prevent the key/value from being inserted.
///
bool CreateValue(TKey key, out TValue value);
}
///
/// An interface to provide conditional or custom update logic to a concurrent dictionary.
///
public interface IUpdateValue
{
///
/// Called when the key was found within the dictionary to produce a modified value to update the item
/// to. Return true to continue with the update, or false to prevent the key/value from being updated.
///
bool UpdateValue(TKey key, ref TValue value);
}
///
/// An interface to provide conditional or custom creation or update logic to a concurrent dictionary.
///
///
/// Generally implemented as a struct and passed by ref to save stack space and to retrieve the values
/// that where inserted or updated.
///
public interface ICreateOrUpdateValue : ICreateValue, IUpdateValue
{
}
///
/// An interface to provide conditional removal of an item from a concurrent dictionary.
///
///
/// Generally implemented as a struct and passed by ref to save stack space and to retrieve the values
/// that where inserted or updated.
///
public interface IRemoveValue
{
///
/// Called when the dictionary is about to remove the key/value pair provided, return true to allow
/// it's removal, or false to prevent it from being removed.
///
bool RemoveValue(TKey key, TValue value);
}
///
/// Extends the IDictionaryEx interface to encompass concurrent/atomic operations
///
public interface IConcurrentDictionary : IDictionaryEx
{
///
/// Adds a key/value pair to the if the key does not already exist.
///
/// The key of the element to add.
/// Constructs a new value for the key.
/// The is read-only.
TValue GetOrAdd(TKey key, Converter fnCreate);
///
/// Adds a key/value pair to the if the key does not already exist,
/// or updates a key/value pair if the key already exists.
///
TValue AddOrUpdate(TKey key, TValue addValue, KeyValueUpdate fnUpdate);
///
/// Adds a key/value pair to the if the key does not already exist,
/// or updates a key/value pair if the key already exists.
///
///
/// Adds or modifies an element with the provided key and value. If the key does not exist in the collection,
/// the factory method fnCreate will be called to produce the new value, if the key exists, the converter method
/// fnUpdate will be called to create an updated value.
///
TValue AddOrUpdate(TKey key, Converter fnCreate, KeyValueUpdate fnUpdate);
///
/// Add, update, or fetche a key/value pair from the dictionary via an implementation of the
/// interface.
///
bool AddOrUpdate(TKey key, ref T createOrUpdateValue) where T : ICreateOrUpdateValue;
///
/// Adds an element with the provided key and value to the
/// by calling the provided factory method to construct the value if the key is not already present in the collection.
///
bool TryAdd(TKey key, Converter fnCreate);
///
/// Modify the value associated with the result of the provided update method
/// as an atomic operation, Allows for reading/writing a single record within
/// the tree lock. Be cautious about the behavior and performance of the code
/// provided as it can cause a dead-lock to occur. If the method returns an
/// instance who .Equals the original, no update is applied.
///
bool TryUpdate(TKey key, KeyValueUpdate fnUpdate);
///
/// Removes the element with the specified key from the
/// if the fnCondition predicate is null or returns true.
///
bool TryRemove(TKey key, KeyValuePredicate fnCondition);
///
/// Conditionally removes a key/value pair from the dictionary via an implementation of the
/// interface.
///
bool TryRemove(TKey key, ref T removeValue) where T : IRemoveValue;
}
}