using System;
using System.Globalization;
using LibGit2Sharp.Core;
using LibGit2Sharp.Core.Handles;
namespace LibGit2Sharp
{
///
/// A signature
///
public sealed class Signature : IEquatable
{
private readonly DateTimeOffset when;
private readonly string name;
private readonly string email;
private static readonly LambdaEqualityHelper equalityHelper =
new LambdaEqualityHelper(x => x.Name, x => x.Email, x => x.When);
internal Signature(IntPtr signaturePtr)
{
var handle = signaturePtr.MarshalAs();
name = LaxUtf8Marshaler.FromNative(handle.Name);
email = LaxUtf8Marshaler.FromNative(handle.Email);
when = Epoch.ToDateTimeOffset(handle.When.Time, handle.When.Offset);
}
///
/// Initializes a new instance of the class.
///
/// The name.
/// The email.
/// The when.
public Signature(string name, string email, DateTimeOffset when)
{
Ensure.ArgumentNotNullOrEmptyString(name, "name");
Ensure.ArgumentNotNullOrEmptyString(email, "email");
Ensure.ArgumentDoesNotContainZeroByte(name, "name");
Ensure.ArgumentDoesNotContainZeroByte(email, "email");
this.name = name;
this.email = email;
this.when = when;
}
///
/// Initializes a new instance of the class.
///
/// The identity.
/// The when.
public Signature(Identity identity, DateTimeOffset when)
{
Ensure.ArgumentNotNull(identity, "identity");
this.name = identity.Name;
this.email = identity.Email;
this.when = when;
}
internal SignatureSafeHandle BuildHandle()
{
return Proxy.git_signature_new(name, email, when);
}
///
/// Gets the name.
///
public string Name
{
get { return name; }
}
///
/// Gets the email.
///
public string Email
{
get { return email; }
}
///
/// Gets the date when this signature happened.
///
public DateTimeOffset When
{
get { return when; }
}
///
/// Determines whether the specified is equal to the current .
///
/// The to compare with the current .
/// True if the specified is equal to the current ; otherwise, false.
public override bool Equals(object obj)
{
return Equals(obj as Signature);
}
///
/// Determines whether the specified is equal to the current .
///
/// The to compare with the current .
/// True if the specified is equal to the current ; otherwise, false.
public bool Equals(Signature other)
{
return equalityHelper.Equals(this, other);
}
///
/// Returns the hash code for this instance.
///
/// A 32-bit signed integer hash code.
public override int GetHashCode()
{
return equalityHelper.GetHashCode(this);
}
///
/// Tests if two are equal.
///
/// First to compare.
/// Second to compare.
/// True if the two objects are equal; false otherwise.
public static bool operator ==(Signature left, Signature right)
{
return Equals(left, right);
}
///
/// Tests if two are different.
///
/// First to compare.
/// Second to compare.
/// True if the two objects are different; false otherwise.
public static bool operator !=(Signature left, Signature right)
{
return !Equals(left, right);
}
///
/// Returns " <>" for the current .
///
/// The and of the current .
public override string ToString()
{
return string.Format(CultureInfo.InvariantCulture, "{0} <{1}>", Name, Email);
}
}
}