Skip to content
This repository has been archived by the owner on Dec 30, 2020. It is now read-only.

Commit

Permalink
Added value semantics to Oid
Browse files Browse the repository at this point in the history
  • Loading branch information
samus committed Mar 13, 2010
1 parent c6360a7 commit e7dbb97
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 15 deletions.
44 changes: 41 additions & 3 deletions MongoDB.Net-Tests/TestOid.cs
Expand Up @@ -46,7 +46,34 @@ public class TestOid
[Test]
public void TestOidEquality(){
Oid val = new Oid("4a7067c30a57000000008ecb");
Assert.AreEqual(val, new Oid(val.ToString().Replace("\"", "")));
Oid other = new Oid("4a7067c30a57000000008ecb");

Assert.IsTrue(val.Equals(other), "Equals(Oid) did not work");
Assert.IsTrue(val == other, "== operator did not work");
}

[Test]
public void TestOidInEquality(){
Oid val = new Oid("4a7067c30a57000000008ecb");
Oid other = new Oid("5a7067c30a57000000008ecb");

Assert.IsFalse(val == null);
Assert.IsFalse(val == other);
Assert.IsFalse(val.Equals(other));
Assert.IsTrue(val != null);
Assert.IsTrue(val != other);
}

[Test]
public void TestOidComparisons(){
Oid lower = new Oid("4a7067c30a57000000008ecb");
Oid higher = new Oid("5a7067c30a57000000008ecb");

Assert.AreEqual(1, lower.CompareTo(null));
Assert.AreEqual(1, higher.CompareTo(lower));

Assert.IsTrue(lower < higher);
Assert.IsTrue(higher > lower);
}

[Test]
Expand Down Expand Up @@ -110,8 +137,19 @@ public class TestOid
//Expected: 2010-01-07 02:24:56.633
DateTime expected = new DateTime(2010,1,7,7,21,57,DateTimeKind.Utc);
Assert.AreEqual(expected,oid.Created);


}

[Test]
public void TestToByteArray(){
byte[] bytes = new byte[]{1,2,3,4,5,6,7,8,9,10,11,12};
string hex = "0102030405060708090a0b0c";

Oid bval = new Oid(bytes);
byte[] bytes2 = bval.ToByteArray();

Assert.IsNotNull(bytes2);
Assert.AreEqual(12, bytes2.Length);
Assert.AreEqual(bytes, bytes2);
}
}
}
52 changes: 40 additions & 12 deletions MongoDBDriver/Oid.cs
Expand Up @@ -7,7 +7,7 @@ namespace MongoDB.Driver{
/// <summary>
/// Oid is an immutable object that represents a Mongo ObjectId.
/// </summary>
public class Oid
public class Oid: IEquatable<Oid>, IComparable<Oid>
{
private static OidGenerator oidGenerator = new OidGenerator();

Expand All @@ -27,11 +27,6 @@ public class Oid
}
}

/// <summary>
/// Initializes a new instance of the <see cref="Oid"/> class.
/// </summary>
public Oid(){}

/// <summary>
/// Initializes a new instance of the <see cref="Oid"/> class.
/// </summary>
Expand All @@ -50,24 +45,40 @@ public class Oid
Array.Copy(value,bytes,12);
}


/// <summary>
/// Determines whether the specified <see cref="System.Object"/> is equal to this instance.
/// </summary>
/// <param name="obj">The <see cref="System.Object"/> to compare with this instance.</param>
/// <returns>
/// <c>true</c> if the specified <see cref="System.Object"/> is equal to this instance; otherwise, <c>false</c>.
/// </returns>
/// <exception cref="T:System.NullReferenceException">
/// The <paramref name="obj"/> parameter is null.
/// </exception>
public override bool Equals(object obj){
if(obj.GetType() == typeof(Oid)){
string hex = obj.ToString();
return this.ToString().Equals(hex);
if(obj is Oid){
return this.CompareTo((Oid)obj) == 0;
}
return false;
}

public bool Equals (Oid other){
return this.CompareTo(other) == 0;
}

public int CompareTo (Oid other){
if (System.Object.ReferenceEquals(other, null)){
return 1;
}
byte[] otherBytes = other.ToByteArray();
for(int x = 0; x < bytes.Length; x++){
if(bytes[x] < otherBytes[x]){
return -1;
}else if(bytes[x] > otherBytes[x]){
return 1;
}
}
return 0;
}

/// <summary>
/// Returns a hash code for this instance.
/// </summary>
Expand Down Expand Up @@ -106,6 +117,23 @@ public class Oid
public static Oid Generate(){
return oidGenerator.Generate();
}

public static bool operator ==(Oid a, Oid b){
return a.Equals(b);
}

public static bool operator !=(Oid a, Oid b){
return !(a == b);
}

public static bool operator >(Oid a, Oid b){
return a.CompareTo(b) > 0;
}

public static bool operator <(Oid a, Oid b){
return a.CompareTo(b) < 0;
}


/// <summary>
/// Validates the hex.
Expand Down

1 comment on commit e7dbb97

@lanwin
Copy link
Collaborator

@lanwin lanwin commented on e7dbb97 Mar 14, 2010

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we rename Generate to NewOid to match the interface of Guid?

Please sign in to comment.