-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathSystemTypeObjectProvider.cs
More file actions
86 lines (75 loc) · 2.33 KB
/
SystemTypeObjectProvider.cs
File metadata and controls
86 lines (75 loc) · 2.33 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
using System;
using System.Collections.Generic;
using System.Linq;
using ArchUnitNET.Domain;
using ArchUnitNET.Domain.Extensions;
namespace ArchUnitNET.Fluent
{
internal class SystemTypeObjectProvider<T> : ISizedObjectProvider<T>
where T : IType
{
private readonly List<Type> _types;
public SystemTypeObjectProvider(IEnumerable<Type> types)
{
_types = types.ToList();
Description = string.Join(" or ", _types.Select(type => $"\"{type.FullName}\""));
}
public string Description { get; }
public int Count => _types.Count;
public IEnumerable<T> GetObjects(Architecture architecture)
{
return _types
.Select(architecture.GetITypeOfType)
.Select(
(type) =>
{
if (!(type is T result))
{
throw new ArgumentException($"Type {type} is not of type {typeof(T)}");
}
return result;
}
);
}
public string FormatDescription(
string emptyDescription,
string singleDescription,
string multipleDescription
)
{
switch (Count)
{
case 0:
return emptyDescription;
case 1:
return $"{singleDescription} {Description}";
}
return $"{multipleDescription} {Description}";
}
private bool Equals(SystemTypeObjectProvider<T> other)
{
return _types.SequenceEqual(other._types);
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj))
{
return false;
}
if (ReferenceEquals(this, obj))
{
return true;
}
return obj.GetType() == GetType() && Equals((SystemTypeObjectProvider<T>)obj);
}
public override int GetHashCode()
{
return _types != null
? _types.Aggregate(
0,
(current, type) => (current * 397) ^ (type?.GetHashCode() ?? 0)
)
: 0;
}
}
}