8000 Add basic Tuple<T1, T2> type · Folcon/libgit2sharp@1032a14 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1032a14

Browse files
committed
Add basic Tuple<T1, T2> type
1 parent d526b18 commit 1032a14

File tree

14 files changed

+135
-4
lines changed

14 files changed

+135
-4
lines changed

LibGit2Sharp.Tests/LazyFixture.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
using System;
2-
using LibGit2Sharp.Core;
2+
using LibGit2Sharp.Core.Compat;
33
using LibGit2Sharp.Tests.TestHelpers;
44
using NUnit.Framework;
55

LibGit2Sharp.Tests/LibGit2Sharp.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
<Compile Include="TestHelpers\SignatureExtensions.cs" />
6565
<Compile Include="TestHelpers\TemporaryCloneOfTestRepo.cs" />
6666
<Compile Include="TreeFixture.cs" />
67+
<Compile Include="TupleFixture.cs" />
6768
</ItemGroup>
6869
<ItemGroup>
6970
<ProjectReference Include="..\LibGit2Sharp\LibGit2Sharp.csproj">

LibGit2Sharp.Tests/TupleFixture.cs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
using LibGit2Sharp.Core.Compat;
2+
using LibGit2Sharp.Tests.TestHelpers;
3+
using NUnit.Framework;
4+
5+
namespace LibGit2Sharp.Tests
6+
{
7+
[TestFixture]
8+
public class TupleFixture
9+
{
10+
const int integer = 2;
11+
const string stringy = "hello";
12+
13+
private readonly Tuple<int, string> sut = new Tuple<int, string>(integer, stringy);
14+
15+
[Test]
16+
public void Properties()
17+
{
18+
sut.Item1.ShouldEqual(integer);
19+
sut.Item2.ShouldEqual(stringy);
20+
}
21+
22+
[Test]
23+
public void GetHashCodeIsTheSame()
24+
{
25+
var sut2 = new Tuple<int, string>(integer, stringy);
26+
27+
sut.GetHashCode().ShouldEqual(sut2.GetHashCode());
28+
}
29+
30+
[Test]
31+
public void GetHashCodeIsDifferent()
32+
{
33+
var sut2 = new Tuple<int, string>(integer + 1, stringy);
34+
35+
sut.GetHashCode().ShouldNotEqual(sut2.GetHashCode());
36+
}
37+
38+
[Test]
39+
public void Equals()
40+
{
41+
var sut2 = new Tuple<int, string>(integer, stringy);
42+
43+
sut.Equals(sut2).ShouldBeTrue();
44+
Equals(sut, sut2).ShouldBeTrue();
45+
}
46+
47+
[Test]
48+
public void NotEquals()
49+
{
50+
var sut2 = new Tuple<int, string>(integer + 1, stringy);
51+
52+
sut.Equals(sut2).ShouldBeFalse();
53+
Equals(sut, sut2).ShouldBeFalse();
54+
}
55+
}
56+
}

LibGit2Sharp/Branch.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Globalization;
33
using System.Linq;
44
using LibGit2Sharp.Core;
5+
using LibGit2Sharp.Core.Compat;
56

67
namespace LibGit2Sharp
78
{

LibGit2Sharp/Commit.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Runtime.InteropServices;
44
using LibGit2Sharp.Core;
5+
using LibGit2Sharp.Core.Compat;
56

67
namespace LibGit2Sharp
78
{

LibGit2Sharp/Core/Lazy.cs renamed to LibGit2Sharp/Core/Compat/Lazy.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using System;
22

3-
namespace LibGit2Sharp.Core
3+
namespace LibGit2Sharp.Core.Compat
44
{
55
/// <summary>
66
/// Provides support for lazy initialization.

LibGit2Sharp/Core/Compat/Tuple.cs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
using System.Collections.Generic;
2+
3+
namespace LibGit2Sharp.Core.Compat
4+
{
5+
/// <summary>
6+
/// Represents a 2-tuple, or pair.
7+
/// </summary>
8+
/// <typeparam name = "T1">The type of the tuple's first component.</typeparam>
9+
/// <typeparam name = "T2">The type of the tuple's second component.</typeparam>
10+
public class Tuple<T1, T2>
11+
{
12+
private readonly KeyValuePair<T1, T2> kvp;
13+
14+
/// <summary>
15+
/// Initializes a new instance of the <see cref="Tuple{T1,T2}"/> class.
16+
/// </summary>
17+
/// <param name="item1">The value of the tuple's first component.</param>
18+
/// <param name="item2">The value of the tuple's second component.</param>
19+
public Tuple(T1 item1, T2 item2)
20+
{
21+
kvp = new KeyValuePair<T1, T2>(item1, item2);
22+
}
23+
24+
/// <summary>
25+
/// Gets the value of the current <see cref = "Tuple{T1,T2}" /> object's second component.
26+
/// </summary>
27+
public T2 Item2
28+
{
29+
get { return kvp.Value; }
30+
}
31+
32+
/// <summary>
33+
/// Gets the value of the current <see cref = "Tuple{T1,T2}" /> object's first component.
34+
/// </summary>
35+
public T1 Item1
36+
{
37+
get { return kvp.Key; }
38+
}
39+
40+
/// <summary>
41+
/// Returns the hash code for the current <see cref = "Tuple{T1,T2}" /> object.
42+
/// </summary>
43+
/// <returns>A 32-bit signed integer hash code.</returns>
44+
public override int GetHashCode()
45+
{
46+
return kvp.GetHashCode();
47+
}
48+
49+
/// <summary>
50+
/// Returns a value that indicates whether the current <see cref = "Tuple{T1,T2}" /> object is equal to a specified object.
51+
/// </summary>
52+
/// <param name = "obj">The object to compare with this instance.</param>
53+
/// <returns>true if the current instance is equal to the specified object; otherwise, false.</returns>
54+
public override bool Equals(object obj)
55+
{
56+
if (!(obj is Tuple<T1, T2>))
57+
{
58+
return false;
59+
}
60+
return kvp.Equals(((Tuple<T1, T2>)obj).kvp);
61+
}
62+
63+
64+
}
65+
}

LibGit2Sharp/DirectReference.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using LibGit2Sharp.Core;
2+
using LibGit2Sharp.Core.Compat;
23

34
namespace LibGit2Sharp
45
{

LibGit2Sharp/LibGit2Sharp.csproj

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
<Compile Include="CommitCollection.cs" />
5353
<Compile Include="Configuration.cs" />
5454
<Compile Include="ConfigurationLevel.cs" />
55+
<Compile Include="Core\Compat\Tuple.cs" />
5556
<Compile Include="Core\EnumExtensions.cs" />
5657
<Compile Include="DetachedHead.cs" />
5758
<Compile Include="LibGit2Exception.cs" />
@@ -67,7 +68,7 @@
6768
<Compile Include="FileStatus.cs" />
6869
<Compile Include="Core\GitTime.cs" />
6970
<Compile Include="Core\IntPtrExtensions.cs" />
70-
<Compile Include="Core\Lazy.cs" />
71+
<Compile Include="Core\Compat\Lazy.cs" />
7172
<Compile Include="Core\NativeMethods.cs" />
7273
<Compile Include="Core\SafeHandleExtensions.cs" />
7374
<Compile Include="Core\ObjectSafeWrapper.cs" />
@@ -132,6 +133,6 @@
132133
<ItemGroup>
133134
<NativeBinaries Include="$(MSBuildProjectDirectory)\..\Lib\NativeBinaries\**\*.*" />
134135
</ItemGroup>
135-
<Copy SourceFiles="@(NativeBinaries)" DestinationFiles="@(NativeBinaries->'$(OutputPath)NativeBinaries\%(RecursiveDir)%(Filename)%(Extension)')" SkipUnchangedFiles="true" />
136+
<Copy SourceFiles="@(NativeBinaries)" DestinationFiles="@(NativeBinaries->'$(OutputPath)NativeBinaries\%(RecursiveDir)%(Filename)%(Extension)')" SkipUnchangedFiles="true" />
136137
</Target>
137138
</Project>

LibGit2Sharp/NamedReference.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using LibGit2Sharp.Core;
3+
using LibGit2Sharp.Core.Compat;
34

45
namespace LibGit2Sharp
56
{

0 commit comments

Comments
 (0)
0