diff options
author | nulltoken <emeric.fermas@gmail.com> | 2012-03-02 19:41:53 +0400 |
---|---|---|
committer | nulltoken <emeric.fermas@gmail.com> | 2012-03-03 14:51:22 +0400 |
commit | e6ba4c9d438aea285fff08d30f6f2cf5925348e2 (patch) | |
tree | ccff721968c07970df8614aa2ede6ceed417f68c /LibGit2Sharp | |
parent | 235ccd21cf3641fc540237c1e063c796ebee4cbc (diff) |
Refactor implementation of equality members of Branch and Tag types
Diffstat (limited to 'LibGit2Sharp')
-rw-r--r-- | LibGit2Sharp/Branch.cs | 61 | ||||
-rw-r--r-- | LibGit2Sharp/ReferenceWrapper.cs | 63 | ||||
-rw-r--r-- | LibGit2Sharp/Tag.cs | 56 |
3 files changed, 59 insertions, 121 deletions
diff --git a/LibGit2Sharp/Branch.cs b/LibGit2Sharp/Branch.cs index 4082a422..d3e591a7 100644 --- a/LibGit2Sharp/Branch.cs +++ b/LibGit2Sharp/Branch.cs @@ -1,7 +1,6 @@ using System; using System.Globalization; using System.Linq; -using LibGit2Sharp.Core; using LibGit2Sharp.Core.Compat; namespace LibGit2Sharp @@ -9,11 +8,8 @@ namespace LibGit2Sharp /// <summary> /// A branch is a special kind of reference /// </summary> - public class Branch : ReferenceWrapper<Commit>, IEquatable<Branch> + public class Branch : ReferenceWrapper<Commit> { - private static readonly LambdaEqualityHelper<Branch> equalityHelper = - new LambdaEqualityHelper<Branch>(new Func<Branch, object>[] { x => x.CanonicalName, x => x.Tip }); - private readonly Lazy<Branch> trackedBranch; /// <summary> @@ -134,39 +130,6 @@ namespace LibGit2Sharp get { return repo.Commits.QueryBy(new Filter { Since = this }); } } - #region IEquatable<Branch> Members - - /// <summary> - /// Determines whether the specified <see cref = "Branch" /> is equal to the current <see cref = "Branch" />. - /// </summary> - /// <param name = "other">The <see cref = "Branch" /> to compare with the current <see cref = "Branch" />.</param> - /// <returns>True if the specified <see cref = "Branch" /> is equal to the current <see cref = "Branch" />; otherwise, false.</returns> - public bool Equals(Branch other) - { - return equalityHelper.Equals(this, other); - } - - #endregion - - /// <summary> - /// Determines whether the specified <see cref = "Object" /> is equal to the current <see cref = "Branch" />. - /// </summary> - /// <param name = "obj">The <see cref = "Object" /> to compare with the current <see cref = "Branch" />.</param> - /// <returns>True if the specified <see cref = "Object" /> is equal to the current <see cref = "Branch" />; otherwise, false.</returns> - public override bool Equals(object obj) - { - return Equals(obj as Branch); - } - - /// <summary> - /// Returns the hash code for this instance. - /// </summary> - /// <returns>A 32-bit signed integer hash code.</returns> - public override int GetHashCode() - { - return equalityHelper.GetHashCode(this); - } - private Branch ResolveTrackedBranch() { var trackedRemote = repo.Config.Get<string>("branch", Name, "remote", null); @@ -220,27 +183,5 @@ namespace LibGit2Sharp throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "'{0}' does not look like a valid branch name.", canonicalName)); } - - /// <summary> - /// Tests if two <see cref = "Branch" /> are equal. - /// </summary> - /// <param name = "left">First <see cref = "Branch" /> to compare.</param> - /// <param name = "right">Second <see cref = "Branch" /> to compare.</param> - /// <returns>True if the two objects are equal; false otherwise.</returns> - public static bool operator ==(Branch left, Branch right) - { - return Equals(left, right); - } - - /// <summary> - /// Tests if two <see cref = "Branch" /> are different. - /// </summary> - /// <param name = "left">First <see cref = "Branch" /> to compare.</param> - /// <param name = "right">Second <see cref = "Branch" /> to compare.</param> - /// <returns>True if the two objects are different; false otherwise.</returns> - public static bool operator !=(Branch left, Branch right) - { - return !Equals(left, right); - } } } diff --git a/LibGit2Sharp/ReferenceWrapper.cs b/LibGit2Sharp/ReferenceWrapper.cs index 2e70ebdb..95b0674d 100644 --- a/LibGit2Sharp/ReferenceWrapper.cs +++ b/LibGit2Sharp/ReferenceWrapper.cs @@ -8,18 +8,18 @@ namespace LibGit2Sharp /// A base class for things that wrap a <see cref = "Reference" /> (branch, tag, etc). /// </summary> /// <typeparam name="TObject">The type of the referenced Git object.</typeparam> - public abstract class ReferenceWrapper<TObject> where TObject : GitObject + public abstract class ReferenceWrapper<TObject> : IEquatable<ReferenceWrapper<TObject>> where TObject : GitObject { /// <summary> - /// The repo. + /// The repository. /// </summary> protected readonly Repository repo; private readonly Lazy<TObject> objectBuilder; - /// <summary> - /// Initializes a new instance of the <see cref = "ReferenceWrapper{TObject}" /> class. - /// </summary> - /// <param name="repo">The repo.</param> + private static readonly LambdaEqualityHelper<ReferenceWrapper<TObject>> equalityHelper = + new LambdaEqualityHelper<ReferenceWrapper<TObject>>(new Func<ReferenceWrapper<TObject>, object>[] { x => x.CanonicalName, x => x.TargetObject }); + + /// <param name="repo">The repository.</param> /// <param name="reference">The reference.</param> /// <param name="canonicalNameSelector">A function to construct the reference's canonical name.</param> protected internal ReferenceWrapper(Repository repo, Reference reference, Func<Reference, string> canonicalNameSelector) @@ -87,5 +87,56 @@ namespace LibGit2Sharp return repo.Lookup<TObject>(target.Id); } + + /// <summary> + /// Determines whether the specified <see cref = "ReferenceWrapper{TObject}" /> is equal to the current <see cref = "ReferenceWrapper{TObject}" />. + /// </summary> + /// <param name = "other">The <see cref = "ReferenceWrapper{TObject}" /> to compare with the current <see cref = "ReferenceWrapper{TObject}" />.</param> + /// <returns>True if the specified <see cref = "ReferenceWrapper{TObject}" /> is equal to the current <see cref = "ReferenceWrapper{TObject}" />; otherwise, false.</returns> + public bool Equals(ReferenceWrapper<TObject> other) + { + return equalityHelper.Equals(this, other); + } + + /// <summary> + /// Determines whether the specified <see cref = "Object" /> is equal to the current <see cref = "ReferenceWrapper{TObject}" />. + /// </summary> + /// <param name = "obj">The <see cref = "Object" /> to compare with the current <see cref = "ReferenceWrapper{TObject}" />.</param> + /// <returns>True if the specified <see cref = "Object" /> is equal to the current <see cref = "ReferenceWrapper{TObject}" />; otherwise, false.</returns> + public override bool Equals(object obj) + { + return Equals(obj as ReferenceWrapper<TObject>); + } + + /// <summary> + /// Returns the hash code for this instance. + /// </summary> + /// <returns>A 32-bit signed integer hash code.</returns> + public override int GetHashCode() + { + return equalityHelper.GetHashCode(this); + } + + /// <summary> + /// Tests if two <see cref = "ReferenceWrapper{TObject}" /> are equal. + /// </summary> + /// <param name = "left">First <see cref = "ReferenceWrapper{TObject}" /> to compare.</param> + /// <param name = "right">Second <see cref = "ReferenceWrapper{TObject}" /> to compare.</param> + /// <returns>True if the two objects are equal; false otherwise.</returns> + public static bool operator ==(ReferenceWrapper<TObject> left, ReferenceWrapper<TObject> right) + { + return Equals(left, right); + } + + /// <summary> + /// Tests if two <see cref = "ReferenceWrapper{TObject}" /> are different. + /// </summary> + /// <param name = "left">First <see cref = "ReferenceWrapper{TObject}" /> to compare.</param> + /// <param name = "right">Second <see cref = "ReferenceWrapper{TObject}" /> to compare.</param> + /// <returns>True if the two objects are different; false otherwise.</returns> + public static bool operator !=(ReferenceWrapper<TObject> left, ReferenceWrapper<TObject> right) + { + return !Equals(left, right); + } } } diff --git a/LibGit2Sharp/Tag.cs b/LibGit2Sharp/Tag.cs index 83764b05..28ac6659 100644 --- a/LibGit2Sharp/Tag.cs +++ b/LibGit2Sharp/Tag.cs @@ -6,11 +6,8 @@ namespace LibGit2Sharp /// <summary> /// A Tag /// </summary> - public class Tag : ReferenceWrapper<GitObject>, IEquatable<Tag> + public class Tag : ReferenceWrapper<GitObject> { - private static readonly LambdaEqualityHelper<Tag> equalityHelper = - new LambdaEqualityHelper<Tag>(new Func<Tag, object>[] { x => x.CanonicalName, x => x.Target }); - internal Tag(Repository repo, Reference reference, string canonicalName) : base(repo, reference, _ => canonicalName) { @@ -62,56 +59,5 @@ namespace LibGit2Sharp return canonicalName.Substring("refs/tags/".Length); } - - /// <summary> - /// Determines whether the specified <see cref = "Object" /> is equal to the current <see cref = "Tag" />. - /// </summary> - /// <param name = "obj">The <see cref = "Object" /> to compare with the current <see cref = "Tag" />.</param> - /// <returns>True if the specified <see cref = "Object" /> is equal to the current <see cref = "Tag" />; otherwise, false.</returns> - public override bool Equals(object obj) - { - return Equals(obj as Tag); - } - - /// <summary> - /// Determines whether the specified <see cref = "Tag" /> is equal to the current <see cref = "Tag" />. - /// </summary> - /// <param name = "other">The <see cref = "Tag" /> to compare with the current <see cref = "Tag" />.</param> - /// <returns>True if the specified <see cref = "Tag" /> is equal to the current <see cref = "Tag" />; otherwise, false.</returns> - public bool Equals(Tag other) - { - return equalityHelper.Equals(this, other); - } - - /// <summary> - /// Returns the hash code for this instance. - /// </summary> - /// <returns>A 32-bit signed integer hash code.</returns> - public override int GetHashCode() - { - return equalityHelper.GetHashCode(this); - } - - /// <summary> - /// Tests if two <see cref = "Tag" /> are equal. - /// </summary> - /// <param name = "left">First <see cref = "Tag" /> to compare.</param> - /// <param name = "right">Second <see cref = "Tag" /> to compare.</param> - /// <returns>True if the two objects are equal; false otherwise.</returns> - public static bool operator ==(Tag left, Tag right) - { - return Equals(left, right); - } - - /// <summary> - /// Tests if two <see cref = "Tag" /> are different. - /// </summary> - /// <param name = "left">First <see cref = "Tag" /> to compare.</param> - /// <param name = "right">Second <see cref = "Tag" /> to compare.</param> - /// <returns>True if the two objects are different; false otherwise.</returns> - public static bool operator !=(Tag left, Tag right) - { - return !Equals(left, right); - } } } |