forked from TNG/ArchUnitNET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObjectProviderCache.cs
More file actions
35 lines (31 loc) · 1.16 KB
/
ObjectProviderCache.cs
File metadata and controls
35 lines (31 loc) · 1.16 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
// Copyright 2019 Florian Gather <florian.gather@tngtech.com>
// Copyright 2019 Paula Ruiz <paularuiz22@gmail.com>
// Copyright 2019 Fritz Brandhuber <fritz.brandhuber@tngtech.com>
//
// SPDX-License-Identifier: Apache-2.0
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using ArchUnitNET.Fluent;
namespace ArchUnitNET.Domain
{
public class ObjectProviderCache
{
private readonly Architecture _architecture;
private readonly ConcurrentDictionary<int, object> _cache;
public ObjectProviderCache(Architecture architecture)
{
_architecture = architecture;
_cache = new ConcurrentDictionary<int, object>();
}
public IEnumerable<T> GetOrCreateObjects<T>(IObjectProvider<T> objectProvider,
Func<Architecture, IEnumerable<T>> providingFunction) where T : ICanBeAnalyzed
{
unchecked
{
var key = (objectProvider.GetHashCode() * 397) ^ objectProvider.GetType().GetHashCode();
return (IEnumerable<T>) _cache.GetOrAdd(key, k => providingFunction(_architecture));
}
}
}
}