-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
BsonNull.cs
167 lines (151 loc) · 5.56 KB
/
BsonNull.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/* Copyright 2010-present MongoDB Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using MongoDB.Shared;
namespace MongoDB.Bson
{
/// <summary>
/// Represents the BSON Null value.
/// </summary>
[Serializable]
public class BsonNull : BsonValue, IComparable<BsonNull>, IEquatable<BsonNull>
{
// private static fields
private static BsonNull __value = new BsonNull();
// constructors
// private so only the singleton instance can be created
private BsonNull()
{
}
// public operators
/// <summary>
/// Compares two BsonNull values.
/// </summary>
/// <param name="lhs">The first BsonNull.</param>
/// <param name="rhs">The other BsonNull.</param>
/// <returns>True if the two BsonNull values are not equal according to ==.</returns>
public static bool operator !=(BsonNull lhs, BsonNull rhs)
{
return !(lhs == rhs);
}
/// <summary>
/// Compares two BsonNull values.
/// </summary>
/// <param name="lhs">The first BsonNull.</param>
/// <param name="rhs">The other BsonNull.</param>
/// <returns>True if the two BsonNull values are equal according to ==.</returns>
public static bool operator ==(BsonNull lhs, BsonNull rhs)
{
if (object.ReferenceEquals(lhs, null)) { return object.ReferenceEquals(rhs, null); }
return lhs.Equals(rhs);
}
// public static properties
/// <summary>
/// Gets the singleton instance of BsonNull.
/// </summary>
public static BsonNull Value { get { return __value; } }
// public properties
/// <summary>
/// Gets the BsonType of this BsonValue.
/// </summary>
public override BsonType BsonType
{
get { return BsonType.Null; }
}
// public methods
/// <summary>
/// Compares this BsonNull to another BsonNull.
/// </summary>
/// <param name="other">The other BsonNull.</param>
/// <returns>A 32-bit signed integer that indicates whether this BsonNull is less than, equal to, or greather than the other.</returns>
public int CompareTo(BsonNull other)
{
if (other == null) { return 1; }
return 0; // it's a singleton
}
/// <summary>
/// Compares the BsonNull to another BsonValue.
/// </summary>
/// <param name="other">The other BsonValue.</param>
/// <returns>A 32-bit signed integer that indicates whether this BsonNull is less than, equal to, or greather than the other BsonValue.</returns>
public override int CompareTo(BsonValue other)
{
if (other == null) { return 1; }
if (other is BsonMinKey) { return 1; }
if (other is BsonUndefined) { return 1; }
if (other is BsonNull) { return 0; }
return -1;
}
/// <summary>
/// Compares this BsonNull to another BsonNull.
/// </summary>
/// <param name="rhs">The other BsonNull.</param>
/// <returns>True if the two BsonNull values are equal.</returns>
public bool Equals(BsonNull rhs)
{
if (object.ReferenceEquals(rhs, null) || GetType() != rhs.GetType()) { return false; }
return true; // it's a singleton
}
/// <summary>
/// Compares this BsonNull to another object.
/// </summary>
/// <param name="obj">The other object.</param>
/// <returns>True if the other object is a BsonNull and equal to this one.</returns>
public override bool Equals(object obj)
{
return Equals(obj as BsonNull); // works even if obj is null or of a different type
}
/// <summary>
/// Gets the hash code.
/// </summary>
/// <returns>The hash code.</returns>
public override int GetHashCode()
{
return Hasher.GetHashCode(BsonType);
}
/// <summary>
/// Converts this BsonValue to a Boolean (using the JavaScript definition of truthiness).
/// </summary>
/// <returns>A Boolean.</returns>
public override bool ToBoolean()
{
return false;
}
/// <summary>
/// Converts this BsonValue to a DateTime? in local time.
/// </summary>
/// <returns>A DateTime?.</returns>
public override DateTime? ToNullableLocalTime()
{
return null;
}
/// <summary>
/// Converts this BsonValue to a DateTime? in UTC.
/// </summary>
/// <returns>A DateTime?.</returns>
public override DateTime? ToNullableUniversalTime()
{
return null;
}
/// <summary>
/// Returns a string representation of the value.
/// </summary>
/// <returns>A string representation of the value.</returns>
public override string ToString()
{
return "BsonNull";
}
}
}