/Driver/Builders/GeoNearOptionsBuilder.cs

https://github.com/tadmarshall/mongo-csharp-driver
C# | 160 lines | 81 code | 13 blank | 66 comment | 2 complexity | 00f753964ba9f1783806d74d27266fad MD5 | raw file
  1. /* Copyright 2010-2011 10gen Inc.
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. using System;
  16. using System.Collections.Generic;
  17. using System.Linq;
  18. using System.Text;
  19. using MongoDB.Bson;
  20. using MongoDB.Bson.IO;
  21. using MongoDB.Bson.Serialization;
  22. using MongoDB.Driver;
  23. namespace MongoDB.Driver.Builders {
  24. /// <summary>
  25. /// A builder for the options of the GeoNear command.
  26. /// </summary>
  27. public static class GeoNearOptions {
  28. #region public static properties
  29. /// <summary>
  30. /// Gets a null value with a type of IMongoGeoNearOptions.
  31. /// </summary>
  32. public static IMongoGeoNearOptions Null {
  33. get { return null; }
  34. }
  35. #endregion
  36. #region public static methods
  37. /// <summary>
  38. /// Sets the distance multiplier.
  39. /// </summary>
  40. /// <param name="value">The distance multiplier.</param>
  41. /// <returns>The builder (so method calls can be chained).</returns>
  42. public static GeoNearOptionsBuilder SetDistanceMultiplier(
  43. double value
  44. ) {
  45. return new GeoNearOptionsBuilder().SetDistanceMultiplier(value);
  46. }
  47. /// <summary>
  48. /// Sets the max distance.
  49. /// </summary>
  50. /// <param name="value">The max distance.</param>
  51. /// <returns>The builder (so method calls can be chained).</returns>
  52. public static GeoNearOptionsBuilder SetMaxDistance(
  53. double value
  54. ) {
  55. return new GeoNearOptionsBuilder().SetMaxDistance(value);
  56. }
  57. /// <summary>
  58. /// Sets whether to use a spherical search.
  59. /// </summary>
  60. /// <param name="value">Whether to use a spherical search.</param>
  61. /// <returns>The builder (so method calls can be chained).</returns>
  62. public static GeoNearOptionsBuilder SetSpherical(
  63. bool value
  64. ) {
  65. return new GeoNearOptionsBuilder().SetSpherical(value);
  66. }
  67. #endregion
  68. }
  69. /// <summary>
  70. /// A builder for the options of the GeoNear command.
  71. /// </summary>
  72. [Serializable]
  73. public class GeoNearOptionsBuilder : BuilderBase, IMongoGeoNearOptions {
  74. #region private fields
  75. private BsonDocument document;
  76. #endregion
  77. #region constructors
  78. /// <summary>
  79. /// Initializes a new instance of the GeoNearOptionsBuilder class.
  80. /// </summary>
  81. public GeoNearOptionsBuilder() {
  82. document = new BsonDocument();
  83. }
  84. #endregion
  85. #region public methods
  86. /// <summary>
  87. /// Sets the distance multiplier.
  88. /// </summary>
  89. /// <param name="value">The distance multiplier.</param>
  90. /// <returns>The builder (so method calls can be chained).</returns>
  91. public GeoNearOptionsBuilder SetDistanceMultiplier(
  92. double value
  93. ) {
  94. document["distanceMultiplier"] = value;
  95. return this;
  96. }
  97. /// <summary>
  98. /// Sets the max distance.
  99. /// </summary>
  100. /// <param name="value">The max distance.</param>
  101. /// <returns>The builder (so method calls can be chained).</returns>
  102. public GeoNearOptionsBuilder SetMaxDistance(
  103. double value
  104. ) {
  105. document["maxDistance"] = value;
  106. return this;
  107. }
  108. /// <summary>
  109. /// Sets whether to use a spherical search.
  110. /// </summary>
  111. /// <param name="value">Whether to use a spherical search.</param>
  112. /// <returns>The builder (so method calls can be chained).</returns>
  113. public GeoNearOptionsBuilder SetSpherical(
  114. bool value
  115. ) {
  116. if (value) {
  117. document["spherical"] = true;
  118. } else {
  119. document.Remove("spherical");
  120. }
  121. return this;
  122. }
  123. /// <summary>
  124. /// Returns the result of the builder as a BsonDocument.
  125. /// </summary>
  126. /// <returns>A BsonDocument.</returns>
  127. public override BsonDocument ToBsonDocument() {
  128. return document;
  129. }
  130. #endregion
  131. #region protected methods
  132. /// <summary>
  133. /// Serializes the result of the builder to a BsonWriter.
  134. /// </summary>
  135. /// <param name="bsonWriter">The writer.</param>
  136. /// <param name="nominalType">The nominal type.</param>
  137. /// <param name="options">The serialization options.</param>
  138. protected override void Serialize(
  139. BsonWriter bsonWriter,
  140. Type nominalType,
  141. IBsonSerializationOptions options
  142. ) {
  143. document.Serialize(bsonWriter, nominalType, options);
  144. }
  145. #endregion
  146. }
  147. }