Class KeyBasedCollection<TKey, TValue>
A KeyBasedCollection<TKey, TValue> implementation.
Inheritance
Implements
Inherited Members
Namespace: Sdl.LanguagePlatform.TranslationMemoryApi.LiteBCM.Collection
Assembly: Sdl.LanguagePlatform.TranslationMemoryApi.dll
Syntax
public class KeyBasedCollection<TKey, TValue> : IList<TValue>, ICollection<TValue>, IEnumerable<TValue>, IEnumerable, IEquatable<KeyBasedCollection<TKey, TValue>>
Type Parameters
Name | Description |
---|---|
TKey | The key. |
TValue | The value. |
Constructors
KeyBasedCollection(IEnumerable<TValue>, Func<TValue, TKey>, Func<TValue, IEnumerable<TValue>, (Boolean IsValid, String ErrorReason)>, Boolean)
Instantiates a new instance of the KeyBasedCollection<TKey, TValue> class.
Declaration
public KeyBasedCollection(IEnumerable<TValue> items, Func<TValue, TKey> keySelector, Func<TValue, IEnumerable<TValue>, (bool IsValid, string ErrorReason)> itemValidator = null, bool allowDefault = false)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<TValue> | items | The items to be used for initial seeding of the collection |
Func<TValue, TKey> | keySelector | The key selector used to map the items. |
Func<TValue, IEnumerable<TValue>, (T1, T2)<Boolean, String>> | itemValidator | Optional validator to be applied when items are added/inserted/replaced in the collection |
Boolean | allowDefault | Tells if default values are allowed for the items. Defaults to not allow default values. This check has precedence over the validator if both are present. |
KeyBasedCollection(Func<TValue, TKey>, Func<TValue, IEnumerable<TValue>, (Boolean IsValid, String ErrorReason)>, Boolean)
Instantiates a new instance of the KeyBasedCollection<TKey, TValue> class.
Declaration
public KeyBasedCollection(Func<TValue, TKey> keySelector, Func<TValue, IEnumerable<TValue>, (bool IsValid, string ErrorReason)> itemValidator = null, bool allowDefault = false)
Parameters
Type | Name | Description |
---|---|---|
Func<TValue, TKey> | keySelector | The key selector used to map the items. |
Func<TValue, IEnumerable<TValue>, (T1, T2)<Boolean, String>> | itemValidator | Optional validator to be applied when items are added/inserted/replaced in the collection |
Boolean | allowDefault | Tells if default values are allowed for the items. Defaults to not allow default values. This check has precedence over the validator if both are present. |
Fields
KeyMap
A map of keys to the values from the items held by the ThreadSafeListHandler> respecting the key selector.
Declaration
protected readonly ConcurrentDictionary<TKey, TValue> KeyMap
Field Value
Type | Description |
---|---|
ConcurrentDictionary<TKey, TValue> |
ThreadSafeListHandler
The thread-safe list handler that backs the items of the collection.
Declaration
protected readonly ThreadSafeListHandler<TValue> ThreadSafeListHandler
Field Value
Type | Description |
---|---|
ThreadSafeListHandler<TValue> |
Properties
Count
Number of elements in the collection.
Declaration
public int Count { get; }
Property Value
Type | Description |
---|---|
Int32 |
IsReadOnly
Tells if the collection is read-only. Always false.
Declaration
public bool IsReadOnly { get; }
Property Value
Type | Description |
---|---|
Boolean |
Item[TKey]
Gets the value for the given key. Throws if not found.
Declaration
public TValue this[TKey key] { get; }
Parameters
Type | Name | Description |
---|---|---|
TKey | key | The key used to lookup a value. |
Property Value
Type | Description |
---|---|
TValue |
Exceptions
Type | Condition |
---|---|
System.Collections.Generic.KeyNotFoundException | Thrown if the key does not match any item. |
Item[Int32]
Gets or sets items at a given index.
Declaration
public TValue this[int index] { get; set; }
Parameters
Type | Name | Description |
---|---|---|
Int32 | index | The index of the item to be retrieved/replaced. |
Property Value
Type | Description |
---|---|
TValue | The value from the given index. |
Exceptions
Type | Condition |
---|---|
ArgumentException | Throws on setting a new value at the index if the new value has a key that's already present in the collection for a different item. |
Methods
Add(TValue)
Adds items to the collection.
Declaration
public void Add(TValue item)
Parameters
Type | Name | Description |
---|---|---|
TValue | item | The item to be added. |
Exceptions
Type | Condition |
---|---|
ArgumentException | Thrown if the collection already has an item with the same key, according to the key selector. |
Clear()
Removes all items and key mappings from the collection.
Declaration
public void Clear()
Contains(TKey)
Tells if the collection contains an item for a given key.
Declaration
public bool Contains(TKey key)
Parameters
Type | Name | Description |
---|---|---|
TKey | key | The key to verify. |
Returns
Type | Description |
---|---|
Boolean | True if the the collection contains an item for the given key. False otherwise. |
Contains(TValue)
Tells if the collection contains an item.
Declaration
public bool Contains(TValue item)
Parameters
Type | Name | Description |
---|---|---|
TValue | item | The item to verify. |
Returns
Type | Description |
---|---|
Boolean | True if the item exists in the collection. False otherwise. |
ContainsKey(TKey)
Tells if the collection contains an item for a given key.
Declaration
public bool ContainsKey(TKey key)
Parameters
Type | Name | Description |
---|---|---|
TKey | key | The key to verify. |
Returns
Type | Description |
---|---|
Boolean | True if the the collection contains an item for the given key. False otherwise. |
CopyTo(TValue[], Int32)
Copies all the items to a given array reference starting at a given index.
Declaration
public void CopyTo(TValue[] array, int arrayIndex)
Parameters
Type | Name | Description |
---|---|---|
TValue[] | array | The array to copy items to. |
Int32 | arrayIndex | The index from where to start the copying. |
Equals(KeyBasedCollection<TKey, TValue>)
Indicates whether the current object is equal to another object of the same type.
Declaration
public bool Equals(KeyBasedCollection<TKey, TValue> other)
Parameters
Type | Name | Description |
---|---|---|
KeyBasedCollection<TKey, TValue> | other | An object to compare with this object. |
Returns
Type | Description |
---|---|
Boolean | true if the current object is equal to the |
Equals(Object)
Performs a deep-equals comparison.
System.Object.Equals(object)
Declaration
public override bool Equals(object obj)
Parameters
Type | Name | Description |
---|---|---|
Object | obj |
Returns
Type | Description |
---|---|
Boolean |
Overrides
ForEach(Action<TValue>)
Executes an action over each element of the collection.
Declaration
public void ForEach(Action<TValue> action)
Parameters
Type | Name | Description |
---|---|---|
Action<TValue> | action | The action. |
GetEnumerator()
Returns an enumerator that iterates over the collection.
Declaration
public IEnumerator<TValue> GetEnumerator()
Returns
Type | Description |
---|---|
IEnumerator<TValue> | An enumerator that can be used to iterate the collection. |
GetHashCode()
Returns a hash code for this instance.
Declaration
public override int GetHashCode()
Returns
Type | Description |
---|---|
Int32 | A hash code for this instance. |
Overrides
GetKeyFor(TValue)
Evaluates the key selector for a given item and gives back the key.
Declaration
public TKey GetKeyFor(TValue item)
Parameters
Type | Name | Description |
---|---|---|
TValue | item | The item for which to evaluate the key. |
Returns
Type | Description |
---|---|
TKey | The key matching the item. |
IndexOf(TValue)
Determines the index of a given item in the collection.
Declaration
public int IndexOf(TValue item)
Parameters
Type | Name | Description |
---|---|---|
TValue | item | The item to be searched for. |
Returns
Type | Description |
---|---|
Int32 | The index of the item if found in the collection. -1 otherwise |
Insert(Int32, TValue)
Inserts an item to the collection at a given index.
Declaration
public void Insert(int index, TValue item)
Parameters
Type | Name | Description |
---|---|---|
Int32 | index | Index to insert at in the collection. |
TValue | item | Item to be inserted at the given index. |
Exceptions
Type | Condition |
---|---|
ArgumentException | Thrown if the given item has a key that's already found in the collection. |
OnItemAdded(TValue)
Extension point for derived classes to perform additional operations when an item is added to the collection. Has no default implementation.
Declaration
protected virtual void OnItemAdded(TValue item)
Parameters
Type | Name | Description |
---|---|---|
TValue | item | The item that was just added to the collection. |
Remove(TKey)
Removes the item matching a given key.
Declaration
public bool Remove(TKey key)
Parameters
Type | Name | Description |
---|---|---|
TKey | key | The key that identifies the item to be removed. |
Returns
Type | Description |
---|---|
Boolean | True if the item was removed. False otherwise. |
Remove(TValue)
Removes a given item and its key mapping from the collection.
Declaration
public bool Remove(TValue item)
Parameters
Type | Name | Description |
---|---|---|
TValue | item | The item to be removed |
Returns
Type | Description |
---|---|
Boolean | True if the item was removed. False otherwise. |
RemoveAt(Int32)
Removes an item at a given index in the collection.
Declaration
public void RemoveAt(int index)
Parameters
Type | Name | Description |
---|---|---|
Int32 | index | Index of the item to be removed. |
TryGetValue(TKey)
Tries to get the value for the given key. Does not fail if the key is not found.
Declaration
public TValue TryGetValue(TKey key)
Parameters
Type | Name | Description |
---|---|---|
TKey | key | The key used to lookup a value. |
Returns
Type | Description |
---|---|
TValue | The value matching the key, if present. Returns the default of the value type otherwise. |
Explicit Interface Implementations
IEnumerable.GetEnumerator()
Declaration
IEnumerator IEnumerable.GetEnumerator()
Returns
Type | Description |
---|---|
IEnumerator |