-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathArchitectureCacheKey.cs
More file actions
96 lines (86 loc) · 3.24 KB
/
ArchitectureCacheKey.cs
File metadata and controls
96 lines (86 loc) · 3.24 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
using System;
using System.Collections.Generic;
using System.Linq;
using ArchUnitNET.Domain.Extensions;
namespace ArchUnitNET.Domain
{
/// <summary>
/// Identifies a cached <see cref="Architecture"/> by the set of loaded modules,
/// their namespace filters, and whether rule evaluation caching is disabled.
/// Two keys are equal when they represent the same combination of modules, filters,
/// and caching flag, regardless of insertion order.
/// </summary>
public class ArchitectureCacheKey : IEquatable<ArchitectureCacheKey>
{
private readonly SortedSet<(string moduleName, string filter)> _architectureCacheKey =
new SortedSet<(string moduleName, string filter)>(new ArchitectureCacheKeyComparer());
private bool _ruleEvaluationCacheDisabled;
public bool Equals(ArchitectureCacheKey other)
{
return other != null
&& _ruleEvaluationCacheDisabled == other._ruleEvaluationCacheDisabled
&& _architectureCacheKey.SequenceEqual(other._architectureCacheKey);
}
/// <summary>
/// Adds a module and optional namespace filter to this key.
/// </summary>
/// <param name="moduleName">The name of the loaded module.</param>
/// <param name="filter">
/// The namespace filter applied when loading, or <c>null</c> if no filter was used.
/// </param>
public void Add(string moduleName, string filter)
{
_architectureCacheKey.Add((moduleName, filter));
}
/// <summary>
/// Marks this key as representing an architecture with rule evaluation caching disabled.
/// Architectures with caching disabled are stored separately in the cache.
/// </summary>
public void SetRuleEvaluationCacheDisabled()
{
_ruleEvaluationCacheDisabled = true;
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj))
{
return false;
}
if (ReferenceEquals(this, obj))
{
return true;
}
return obj.GetType() == GetType() && Equals((ArchitectureCacheKey)obj);
}
public override int GetHashCode()
{
unchecked
{
var hashCode = 397;
_architectureCacheKey.ForEach(tuple =>
{
hashCode = (hashCode * 131) ^ tuple.GetHashCode();
});
hashCode = (hashCode * 131) ^ _ruleEvaluationCacheDisabled.GetHashCode();
return hashCode;
}
}
}
internal class ArchitectureCacheKeyComparer : IComparer<(string moduleName, string filter)>
{
public int Compare(
(string moduleName, string filter) x,
(string moduleName, string filter) y
)
{
var moduleNameComparison = string.Compare(
x.moduleName,
y.moduleName,
StringComparison.Ordinal
);
return moduleNameComparison == 0
? string.Compare(x.filter, y.filter, StringComparison.Ordinal)
: moduleNameComparison;
}
}
}