diff options
-rw-r--r-- | LibGit2Sharp.Tests/EpochFixture.cs | 1 | ||||
-rw-r--r-- | LibGit2Sharp.Tests/LazyFixture.cs | 42 | ||||
-rw-r--r-- | LibGit2Sharp.Tests/LibGit2Sharp.Tests.csproj | 1 | ||||
-rw-r--r-- | LibGit2Sharp/Core/Lazy.cs | 49 | ||||
-rw-r--r-- | LibGit2Sharp/LibGit2Sharp.csproj | 1 |
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"> |