4e671634668ca2a8972b142bb3492f96cfee8607
[mono.git] / mcs / class / referencesource / System.Data.Entity / System / Data / Common / Utils / Pair.cs
1 //---------------------------------------------------------------------
2 // <copyright file="Pair.cs" company="Microsoft">
3 //      Copyright (c) Microsoft Corporation.  All rights reserved.
4 // </copyright>
5 //
6 // @owner Microsoft
7 // @backupOwner Microsoft
8 //---------------------------------------------------------------------
9
10 using System;
11 using System.Collections;
12 using System.Collections.Generic;
13 using System.Text;
14 using System.Diagnostics;
15 using System.Linq;
16
17
18 namespace System.Data.Common.Utils
19 {
20     internal class Pair<TFirst, TSecond> : InternalBase
21     {
22         #region Fields
23         private readonly TFirst first;
24         private readonly TSecond second;
25
26         #endregion
27
28         #region Constructor
29         internal Pair(TFirst first, TSecond second)
30         {
31             this.first = first;
32             this.second = second;
33         }
34         #endregion
35
36         #region Properties
37         internal TFirst First
38         {
39             get
40             {
41                 return first;
42             }
43         }
44
45         internal TSecond Second
46         {
47             get
48             {
49                 return second;
50             }
51         }
52         #endregion 
53
54         #region Methods
55         public override int GetHashCode()
56         {
57             return (first.GetHashCode()<<5) ^ second.GetHashCode();
58         }
59
60         public bool Equals(Pair<TFirst, TSecond> other)
61         {
62             return first.Equals(other.first) && second.Equals(other.second);
63         }
64
65         public override bool Equals(object other)
66         {
67             Pair<TFirst, TSecond> otherPair = other as Pair<TFirst, TSecond>;
68
69             return (otherPair != null && Equals(otherPair));
70         }
71         #endregion
72
73         #region InternalBase
74         internal override void ToCompactString(StringBuilder builder)
75         {
76             builder.Append("<");
77             builder.Append(first.ToString());
78             builder.Append(", "+second.ToString());
79             builder.Append(">");
80         }
81         #endregion
82
83
84         internal class PairComparer : IEqualityComparer<Pair<TFirst, TSecond>>
85         {
86             private PairComparer() { }
87
88             internal static readonly PairComparer Instance = new PairComparer();
89             private static readonly EqualityComparer<TFirst> firstComparer = EqualityComparer<TFirst>.Default;
90             private static readonly EqualityComparer<TSecond> secondComparer = EqualityComparer<TSecond>.Default;
91
92             public bool Equals(Pair<TFirst, TSecond> x, Pair<TFirst, TSecond> y)
93             {
94                 return firstComparer.Equals(x.First, y.First) && secondComparer.Equals(x.Second, y.Second);
95             }
96
97             public int GetHashCode(Pair<TFirst, TSecond> source)
98             {
99                 return source.GetHashCode();
100             }
101         }
102     }
103
104     
105
106 }