Predicated Collection - Kalkwst/Risotto GitHub Wiki
Namespace: Risotto.Collections
Decorates another ICollection to validate that additions match a specified predicate.
On this page
public class PredicatedCollection<T> : Risotto.Collections.AbstractCollectionDecorator<T>
T
The type of the elements in the collection.
Inheritance Object -> ICollection -> AbstractCollectionDecorator -> PredicatedCollection
Derived
The following example demonstrates how to create a PredicatedCollection
using the provided factory method. In this example, the List
contains only valid elements.
using System;
using System.Collections.Generic;
using Risotto.Collection;
using Risotto.Functors;
public class Example
{
public static void Main()
{
// Create a list of strings
List<string> strings = new List<string>();
// Add some strings to the list
strings.Add("hello");
strings.Add("world");
string.Add("banana");
// Create a new NotNullPredicate instance to validate the input to the collection
NotNullPredicate<string> predicate = new NullPredicate<string>();
// Create a new PredicatedCollection
PredicatedCollection<string> collection = PredicatedCollection<string>.GetCollection(list, predicate);
// Display the elements added to the collection. Since no element is null there should be 3
// strings in the collection
foreach(string str in collection)
{
Console.WriteLine(str);
}
/*
hello
world
banana
*/
}
}
The following example demonstrates that the PredicatedCollection
will throw an ArgumentException
if the decorated ICollection
contains an invalid element. In this example, List
contains a null value.
using System;
using System.Collections.Generic;
using Risotto.Collection;
using Risotto.Functors;
public class Example
{
public static void Main()
{
// Create a list of strings
List<string> strings = new List<string>();
// Add some strings to the list
strings.Add("hello");
strings.Add("world");
// This element is going to break the validation
string.Add(null);
// Create a new NotNullPredicate instance to validate the input to the collection
NotNullPredicate<string> predicate = new NullPredicate<string>();
// Create a new PredicatedCollection
// This will throw an ArgumentException since there was a null element in the decorated collection
PredicatedCollection<string> collection = PredicatedCollection<string>.GetCollection(list, predicate);
}
}
The following example demonstrates how to decorate a new empty list, add, add a range and try to add a simple object in a PredicatedCollection.
using System;
using System.Collections.Generic;
using Risotto.Collection;
using Risotto.Functors;
public class PCPart : IEquatable<PCPart>
{
public int PartId { get; set; }
public string PartName { get; set; }
public override string ToString()
{
return "ID: " + PartId + " Name: " + PartName;
}
public override bool Equals(object obj)
{
if (obj == null || GetType() != obj.GetType())
{
return false;
}
PCPart objAsPart = obj as PCPart;
if (objAsPart == null)
return false;
else
return base.Equals(obj);
}
public override int GetHashCode()
{
return PartId;
}
public bool Equals(PCPart other)
{
if (other == null)
return false;
return (this.PartId.Equals(other.PartId));
}
}
public class Example
{
public static void Main()
{
// Create a list of PC parts
List<PCPart> parts = new List<PCPart>();
// Create a new Predicate to validate the objects
// added to the list.
NotNullPredicate<PCPart> predicate = new NotNullPredicate<PCPart>();
// Decorate the parts list in order to become a PredicatedCollection
PredicatedCollection decorated = PredicatedCollection<PCPart>.GetCollection(list, predicate);
// Add parts to the collection
decorated.Add(new PCPart(){PartId = 1234, PartName="cpu"});
decorated.Add(new PCPart(){PartId = 1233, PartName="gpu"});
decorated.Add(new PCPart(){PartId = 3242, PartName="memory"});
decorated.Add(new PCPart(){PartId = 8430, PartName="mobo"});
foreach (PCPart item in decorated)
{
Console.WriteLine(item);
}
/*
ID: 1234 Name: cpu
ID: 1233 Name: gpu
ID: 3242 Name: memory
ID: 8430 Name: mobo
*/
// Add a range of new elements in the predicated collection
decorated.AddRange(parts);
foreach (PCPart item in decorated)
{
Console.WriteLine(item);
}
/*
ID: 1234 Name: cpu
ID: 1233 Name: gpu
ID: 3242 Name: memory
ID: 8430 Name: mobo
ID: 1234 Name: cpu
ID: 1233 Name: gpu
ID: 3242 Name: memory
ID: 8430 Name: mobo
*/
// Try to add a new valid element in the predicated collection
var added = decorated.TryAdd(new PCItem({PartId = 3289, PartName="CPU Fan"}));
//=> added = true
// Try to add a new invalid element in the predicated collection
added = decorated.TryAdd(null);
//=> added = false
}
Method | Description |
---|---|
Add(T) | Adds an object to the PredicatedCollection<T>. |
AddRange(ICollection<T>) | Adds the elements of the specified collection to the end of the PredicatedCollection<T>. |
TryAdd(T) | Attempts to add the specified object to the collection. |