-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathComposedConditionNode.cs
More file actions
73 lines (66 loc) · 3.42 KB
/
ComposedConditionNode.cs
File metadata and controls
73 lines (66 loc) · 3.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
namespace Rules.Framework.Core.ConditionNodes
{
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
/// <summary>
/// A composed condition node which aggregates a set of child condition nodes and defines a
/// logical operator to apply to them.
/// </summary>
/// <typeparam name="TConditionNode">
/// The condition type that allows to filter rules based on a set of conditions.
/// </typeparam>
[DebuggerDisplay("Composed condition: apply {LogicalOperator.ToString(),nq} operator for {System.Linq.Enumerable.Count(ChildConditionNodes),nq} nodes")]
public class ComposedConditionNode<TConditionNode> : IConditionNode<TConditionNode>
{
/// <summary>
/// Creates a new <see cref="ComposedConditionNode{TConditionNode}"/>.
/// </summary>
/// <param name="logicalOperator">the logical operator.</param>
/// <param name="childConditionNodes">the set of child condition nodes.</param>
public ComposedConditionNode(LogicalOperators logicalOperator, IEnumerable<IConditionNode<TConditionNode>> childConditionNodes)
{
this.LogicalOperator = logicalOperator;
this.ChildConditionNodes = childConditionNodes;
this.Properties = new Dictionary<string, object>(StringComparer.Ordinal);
}
/// <summary>
/// Gets the child condition nodes.
/// </summary>
public IEnumerable<IConditionNode<TConditionNode>> ChildConditionNodes { get; }
/// <summary>
/// Gets the logical operator to apply between child condition nodes.
/// </summary>
public LogicalOperators LogicalOperator { get; }
/// <summary>
/// Gets the condition node properties.
/// </summary>
public IDictionary<string, object> Properties { get; }
/// <summary>
/// Clones the condition node into a different instance.
/// </summary>
/// <returns></returns>
public IConditionNode<TConditionNode> Clone()
=> new ComposedConditionNode<TConditionNode>(this.LogicalOperator, this.ChildConditionNodes.Select(cn => cn.Clone()).ToList().AsReadOnly());
/// <summary>
/// Determines whether the specified <see cref="System.Object"/>, is equal to this instance.
/// </summary>
/// <param name="obj">The <see cref="System.Object"/> to compare with this instance.</param>
/// <returns>
/// <c>true</c> if the specified <see cref="System.Object"/> is equal to this instance;
/// otherwise, <c>false</c>.
/// </returns>
public override bool Equals(object obj) => obj is ComposedConditionNode<TConditionNode> node && EqualityComparer<IEnumerable<IConditionNode<TConditionNode>>>.Default.Equals(this.ChildConditionNodes, node.ChildConditionNodes) && this.LogicalOperator == node.LogicalOperator && EqualityComparer<IDictionary<string, object>>.Default.Equals(this.Properties, node.Properties);
/// <summary>
/// Returns a hash code for this instance.
/// </summary>
/// <returns>
/// A hash code for this instance, suitable for use in hashing algorithms and data
/// structures like a hash table.
/// </returns>
public override int GetHashCode()
=> HashCode.Combine(this.ChildConditionNodes, this.LogicalOperator, this.Properties);
}
}