forked from libgit2/libgit2sharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLazy.cs
More file actions
54 lines (48 loc) · 1.42 KB
/
Lazy.cs
File metadata and controls
54 lines (48 loc) · 1.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
using System;
using System.Diagnostics;
namespace LibGit2Sharp.Core.Compat
{
/// <summary>
/// Provides support for lazy initialization.
/// </summary>
/// <typeparam name = "TType">Specifies the type of object that is being lazily initialized.</typeparam>
[DebuggerStepThrough]
public class Lazy<TType>
{
private readonly Func<TType> evaluator;
private TType value;
private bool hasBeenEvaluated;
private readonly object padLock = new object();
/// <summary>
/// Initializes a new instance of the <see cref = "Lazy{TType}" /> class.
/// </summary>
/// <param name = "evaluator"></param>
public Lazy(Func<TType> evaluator)
{
Ensure.ArgumentNotNull(evaluator, "evaluator");
this.evaluator = evaluator;
}
/// <summary>
/// Gets the lazily initialized value of the current instance.
/// </summary>
public TType Value
{
get { return Evaluate(); }
}
private TType Evaluate()
{
if (!hasBeenEvaluated)
{
lock (padLock)
{
if (!hasBeenEvaluated)
{
value = evaluator();
hasBeenEvaluated = true;
}
}
}
return value;
}
}
}