Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/mono/libgit2sharp.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornulltoken <emeric.fermas@gmail.com>2011-06-23 23:24:52 +0400
committernulltoken <emeric.fermas@gmail.com>2011-06-23 23:24:52 +0400
commitdb9c4950f1874981388a422a664604c344bddf51 (patch)
tree23a7cbaa5004fca6b3bc4783b37455396251d2fc
parent375f820b66e86940ec6faa5210bb34a6c5c1d5b9 (diff)
Add basic Lazy<T> type
-rw-r--r--LibGit2Sharp.Tests/EpochFixture.cs1
-rw-r--r--LibGit2Sharp.Tests/LazyFixture.cs42
-rw-r--r--LibGit2Sharp.Tests/LibGit2Sharp.Tests.csproj1
-rw-r--r--LibGit2Sharp/Core/Lazy.cs49
-rw-r--r--LibGit2Sharp/LibGit2Sharp.csproj1
5 files changed, 93 insertions, 1 deletions
diff --git a/LibGit2Sharp.Tests/EpochFixture.cs b/LibGit2Sharp.Tests/EpochFixture.cs
index e82c97d6..4dd4e0ab 100644
--- a/LibGit2Sharp.Tests/EpochFixture.cs
+++ b/LibGit2Sharp.Tests/EpochFixture.cs
@@ -1,5 +1,4 @@
using System;
-using System.Globalization;
using LibGit2Sharp.Core;
using NUnit.Framework;
diff --git a/LibGit2Sharp.Tests/LazyFixture.cs b/LibGit2Sharp.Tests/LazyFixture.cs
new file mode 100644
index 00000000..42c7e4a7
--- /dev/null
+++ b/LibGit2Sharp.Tests/LazyFixture.cs
@@ -0,0 +1,42 @@
+using System;
+using LibGit2Sharp.Core;
+using NUnit.Framework;
+using LibGit2Sharp.Tests.TestHelpers;
+
+namespace LibGit2Sharp.Tests
+{
+ [TestFixture]
+ public class LazyFixture
+ {
+ [Test]
+ public void CanReturnTheValue()
+ {
+ var lazy = new Lazy<int>(() => 2);
+ lazy.Value.ShouldEqual(2);
+ }
+
+ [Test]
+ public void IsLazilyEvaluated()
+ {
+ int i = 0;
+
+ var evaluator = new Func<int>(() => ++i);
+
+ var lazy = new Lazy<int>(evaluator);
+ lazy.Value.ShouldEqual(1);
+ }
+
+ [Test]
+ public void IsEvaluatedOnlyOnce()
+ {
+ int i = 0;
+
+ var evaluator = new Func<int>(() => ++i);
+
+ var lazy = new Lazy<int>(evaluator);
+
+ lazy.Value.ShouldEqual(1);
+ lazy.Value.ShouldEqual(1);
+ }
+ }
+} \ No newline at end of file
diff --git a/LibGit2Sharp.Tests/LibGit2Sharp.Tests.csproj b/LibGit2Sharp.Tests/LibGit2Sharp.Tests.csproj
index 8924209d..ddf10dfc 100644
--- a/LibGit2Sharp.Tests/LibGit2Sharp.Tests.csproj
+++ b/LibGit2Sharp.Tests/LibGit2Sharp.Tests.csproj
@@ -42,6 +42,7 @@
<Reference Include="System.Core" />
</ItemGroup>
<ItemGroup>
+ <Compile Include="LazyFixture.cs" />
<Compile Include="TestHelpers\BaseFixture.cs" />
<Compile Include="BlobFixture.cs" />
<Compile Include="BranchFixture.cs" />
diff --git a/LibGit2Sharp/Core/Lazy.cs b/LibGit2Sharp/Core/Lazy.cs
new file mode 100644
index 00000000..77ceff8d
--- /dev/null
+++ b/LibGit2Sharp/Core/Lazy.cs
@@ -0,0 +1,49 @@
+using System;
+
+namespace LibGit2Sharp.Core
+{
+ /// <summary>
+ /// Provides support for lazy initialization.
+ /// </summary>
+ /// <typeparam name="TType">Specifies the type of object that is being lazily initialized.</typeparam>
+ 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;
+ }
+ }
+} \ No newline at end of file
diff --git a/LibGit2Sharp/LibGit2Sharp.csproj b/LibGit2Sharp/LibGit2Sharp.csproj
index dedb29d5..e38804e6 100644
--- a/LibGit2Sharp/LibGit2Sharp.csproj
+++ b/LibGit2Sharp/LibGit2Sharp.csproj
@@ -60,6 +60,7 @@
<Compile Include="Core\GitSignature.cs" />
<Compile Include="Core\GitTime.cs" />
<Compile Include="Core\IntPtrExtensions.cs" />
+ <Compile Include="Core\Lazy.cs" />
<Compile Include="Core\NativeMethods.cs" />
<Compile Include="Core\ObjectSafeWrapper.cs" />
<Compile Include="Core\PosixPathHelper.cs">