Permalink
Browse files

Add MongoTimestamp type.

  • Loading branch information...
lanwin committed Sep 7, 2010
1 parent 2336ca3 commit 3eb78738313e1526b1eb60b85f2b27d7362c3329
@@ -108,6 +108,7 @@
<Compile Include="IntegrationTests\Linq\MongoQueryTests.cs" />
<Compile Include="IntegrationTests\Linq\MongoQueryProviderTests.cs" />
<Compile Include="UnitTests\Configuration\MongoConfigurationTests.cs" />
+ <Compile Include="UnitTests\MongoTimestampTests.cs" />
<Compile Include="UnitTests\Serialization\Builders\DocumentPropertyTests.cs" />
<Compile Include="UnitTests\Serialization\Builders\EmbeddedClassPropertyTests.cs" />
<Compile Include="UnitTests\Serialization\Builders\EnumerablePropertyTests.cs" />
@@ -0,0 +1,56 @@
+using System.IO;
+using System.Runtime.Serialization.Formatters.Binary;
+using System.Xml.Serialization;
+using NUnit.Framework;
+
+namespace MongoDB.UnitTests
+{
+ public class MongoTimestampTests
+ {
+ [Test]
+ public void Ctor_WithoutArgs_CanBeCreated()
+ {
+ var stamp = new MongoTimestamp();
+
+ Assert.AreEqual(0, stamp.Value);
+ }
+
+ [Test]
+ public void Ctor_WithLong_CanBeCreatedAndValueIsLongValue()
+ {
+ const long value = long.MaxValue - 100;
+ var stmap = new MongoTimestamp(value);
+
+ Assert.AreEqual(value, stmap.Value);
+ }
+
+ [Test]
+ public void CanBeBinarySerialized()
+ {
+ var source = new MongoTimestamp(long.MaxValue - 100);
+
+ var formatter = new BinaryFormatter();
+
+ var mem = new MemoryStream();
+ formatter.Serialize(mem, source);
+ mem.Position = 0;
+
+ var dest = (MongoTimestamp)formatter.Deserialize(mem);
+
+ Assert.AreEqual(source, dest);
+ }
+
+ [Test]
+ public void CanBeXmlSerialized()
+ {
+ var source = new MongoTimestamp(long.MaxValue - 100);
+ var serializer = new XmlSerializer(typeof(MongoTimestamp));
+
+ var writer = new StringWriter();
+ serializer.Serialize(writer, source);
+ var dest = (MongoTimestamp)serializer.Deserialize(new StringReader(writer.ToString()));
+
+ Assert.AreEqual(source, dest);
+ }
+ }
+}
@@ -144,6 +144,7 @@
<Compile Include="MapReduce.cs" />
<Compile Include="Commands\MapReduceCommand.cs" />
<Compile Include="MongoRegexOption.cs" />
+ <Compile Include="MongoTimestamp.cs" />
<Compile Include="Protocol\ResponseFlags.cs" />
<Compile Include="Results\MapReduceResult.cs" />
<Compile Include="Serialization\Builders\DictionaryBuilder.cs" />
@@ -0,0 +1,139 @@
+using System;
+using System.Xml;
+using System.Xml.Schema;
+using System.Xml.Serialization;
+
+namespace MongoDB
+{
+ /// <summary>
+ /// Type that holds the bson timestamp type.
+ /// </summary>
+ [Serializable]
+ public class MongoTimestamp : IEquatable<MongoTimestamp>, IEquatable<long>, IComparable<MongoTimestamp>, IComparable<long>, IXmlSerializable
+ {
+ /// <summary>
+ /// Initializes a new instance of the <see cref="MongoTimestamp"/> class.
+ /// </summary>
+ public MongoTimestamp()
+ {
+ }
+
+ /// <summary>
+ /// Initializes a new instance of the <see cref="MongoTimestamp"/> class.
+ /// </summary>
+ /// <param name="value">The value.</param>
+ public MongoTimestamp(long value)
+ {
+ Value = value;
+ }
+
+ /// <summary>
+ /// Gets or sets the value.
+ /// </summary>
+ /// <value>The value.</value>
+ public long Value { get; set; }
+
+ /// <summary>
+ /// Equalses the specified other.
+ /// </summary>
+ /// <param name="other">The other.</param>
+ /// <returns></returns>
+ public bool Equals(MongoTimestamp other)
+ {
+ if(ReferenceEquals(null, other))
+ return false;
+ if(ReferenceEquals(this, other))
+ return true;
+ return other.Value == Value;
+ }
+
+ /// <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(ReferenceEquals(null, obj))
+ return false;
+ if(ReferenceEquals(this, obj))
+ return true;
+ return obj.GetType() == typeof(MongoTimestamp) && Equals((MongoTimestamp)obj);
+ }
+
+ /// <summary>
+ /// Equalses the specified other.
+ /// </summary>
+ /// <param name="other">The other.</param>
+ /// <returns></returns>
+ public bool Equals(long other)
+ {
+ return Value.Equals(other);
+ }
+
+ /// <summary>
+ /// Returns a hash code for this instance.
+ /// </summary>
+ /// <returns>
+ /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.
+ /// </returns>
+ public override int GetHashCode()
+ {
+ return Value.GetHashCode();
+ }
+
+ /// <summary>
+ /// Compares to.
+ /// </summary>
+ /// <param name="other">The other.</param>
+ /// <returns></returns>
+ public int CompareTo(MongoTimestamp other)
+ {
+ return ReferenceEquals(other, null) ? 1 : Value.CompareTo(other);
+ }
+
+ /// <summary>
+ /// Compares to.
+ /// </summary>
+ /// <param name="other">The other.</param>
+ /// <returns></returns>
+ public int CompareTo(long other)
+ {
+ return Value.CompareTo(other);
+ }
+
+ /// <summary>
+ /// This method is reserved and should not be used. When implementing the IXmlSerializable interface, you should return null (Nothing in Visual Basic) from this method, and instead, if specifying a custom schema is required, apply the <see cref="T:System.Xml.Serialization.XmlSchemaProviderAttribute"/> to the class.
+ /// </summary>
+ /// <returns>
+ /// An <see cref="T:System.Xml.Schema.XmlSchema"/> that describes the XML representation of the object that is produced by the <see cref="M:System.Xml.Serialization.IXmlSerializable.WriteXml(System.Xml.XmlWriter)"/> method and consumed by the <see cref="M:System.Xml.Serialization.IXmlSerializable.ReadXml(System.Xml.XmlReader)"/> method.
+ /// </returns>
+ XmlSchema IXmlSerializable.GetSchema()
+ {
+ return null;
+ }
+
+ /// <summary>
+ /// Generates an object from its XML representation.
+ /// </summary>
+ /// <param name="reader">The <see cref="T:System.Xml.XmlReader"/> stream from which the object is deserialized.</param>
+ void IXmlSerializable.ReadXml(XmlReader reader)
+ {
+ Value = reader.ReadElementContentAsLong();
+ }
+
+ /// <summary>
+ /// Converts an object into its XML representation.
+ /// </summary>
+ /// <param name="writer">The <see cref="T:System.Xml.XmlWriter"/> stream to which the object is serialized.</param>
+ void IXmlSerializable.WriteXml(XmlWriter writer)
+ {
+ writer.WriteString(Value.ToString());
+ }
+ }
+}

0 comments on commit 3eb7873

Please sign in to comment.