|
| 1 | +/* Copyright 2010-present MongoDB 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 | + |
| 16 | +namespace MongoDB.Driver.Search |
| 17 | +{ |
| 18 | + /// <summary> |
| 19 | + /// Object that specifies the boundaries for a range query. |
| 20 | + /// </summary> |
| 21 | + /// <typeparam name="TValue">The type of the range value.</typeparam> |
| 22 | + public struct SearchRangeV2<TValue> |
| 23 | + { |
| 24 | + #region static |
| 25 | + /// <summary>Empty range.</summary> |
| 26 | + internal static SearchRangeV2<TValue> Empty { get; } = new(null, null); |
| 27 | + #endregion |
| 28 | + |
| 29 | + /// <summary> |
| 30 | + /// Initializes a new instance of the <see cref="SearchRangeV2{TValue}"/> class. |
| 31 | + /// </summary> |
| 32 | + /// <param name="min">The lower bound of the range.</param> |
| 33 | + /// <param name="max">The upper bound of the range.</param> |
| 34 | + public SearchRangeV2(Bound<TValue> min, Bound<TValue> max) |
| 35 | + { |
| 36 | + Min = min; |
| 37 | + Max = max; |
| 38 | + } |
| 39 | + |
| 40 | + /// <summary>Gets the upper bound of the range.</summary> |
| 41 | + public Bound<TValue> Max { get; } |
| 42 | + |
| 43 | + /// <summary>Gets the lower bound of the range.</summary> |
| 44 | + public Bound<TValue> Min { get; } |
| 45 | + } |
| 46 | + |
| 47 | + /// <summary> |
| 48 | + /// Represents a bound value. |
| 49 | + /// </summary> |
| 50 | + /// <typeparam name="TValue">The type of the bound value.</typeparam> |
| 51 | + public sealed class Bound<TValue> |
| 52 | + { |
| 53 | + /// <summary> |
| 54 | + /// Gets the bound value. |
| 55 | + /// </summary> |
| 56 | + public TValue Value { get; } |
| 57 | + |
| 58 | + /// <summary> |
| 59 | + /// Gets whether the bound is inclusive or not. |
| 60 | + /// </summary> |
| 61 | + public bool Inclusive { get; } |
| 62 | + |
| 63 | + /// <summary> |
| 64 | + /// Initializes a new instance of the <see cref="Bound{TValue}"/> class. |
| 65 | + /// </summary> |
| 66 | + /// <param name="value">The value of the bound.</param> |
| 67 | + /// <param name="inclusive">Indicates whether the bound is inclusive or not.</param> |
| 68 | + public Bound(TValue value, bool inclusive = false) |
| 69 | + { |
| 70 | + Value = value; |
| 71 | + Inclusive = inclusive; |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + /// <summary> |
| 76 | + /// A builder for a SearchRangeV2. |
| 77 | + /// </summary> |
| 78 | + public static class SearchRangeV2Builder |
| 79 | + { |
| 80 | + /// <summary> |
| 81 | + /// Creates a greater than search range. |
| 82 | + /// </summary> |
| 83 | + /// <typeparam name="TValue">The type of the value.</typeparam> |
| 84 | + /// <param name="value">The value.</param> |
| 85 | + /// <returns>Search range.</returns> |
| 86 | + public static SearchRangeV2<TValue> Gt<TValue>(TValue value) => SearchRangeV2<TValue>.Empty.Gt(value); |
| 87 | + |
| 88 | + /// <summary> |
| 89 | + /// Adds a greater than value to a search range. |
| 90 | + /// </summary> |
| 91 | + /// <typeparam name="TValue">The type of the value.</typeparam> |
| 92 | + /// <param name="searchRange">Search range.</param> |
| 93 | + /// <param name="value">The value.</param> |
| 94 | + /// <returns>Search range.</returns> |
| 95 | + public static SearchRangeV2<TValue> Gt<TValue>(this SearchRangeV2<TValue> searchRange, TValue value) |
| 96 | + => new(min: new (value), searchRange.Max); |
| 97 | + |
| 98 | + /// <summary> |
| 99 | + /// Creates a greater or equal than search range. |
| 100 | + /// </summary> |
| 101 | + /// <typeparam name="TValue">The type of the value.</typeparam> |
| 102 | + /// <param name="value">The value.</param> |
| 103 | + /// <returns>Search range.</returns> |
| 104 | + public static SearchRangeV2<TValue> Gte<TValue>(TValue value) |
| 105 | + => SearchRangeV2<TValue>.Empty.Gte(value); |
| 106 | + |
| 107 | + /// <summary> |
| 108 | + /// Adds a greater or equal than value to a search range. |
| 109 | + /// </summary> |
| 110 | + /// <typeparam name="TValue">The type of the value.</typeparam> |
| 111 | + /// <param name="searchRange">Search range.</param> |
| 112 | + /// <param name="value">The value.</param> |
| 113 | + /// <returns>Search range.</returns> |
| 114 | + public static SearchRangeV2<TValue> Gte<TValue>(this SearchRangeV2<TValue> searchRange, TValue value) |
| 115 | + => new(min: new(value, inclusive: true), searchRange.Max); |
| 116 | + |
| 117 | + /// <summary> |
| 118 | + /// Creates a less than search range. |
| 119 | + /// </summary> |
| 120 | + /// <typeparam name="TValue">The type of the value.</typeparam> |
| 121 | + /// <param name="value">The value.</param> |
| 122 | + /// <returns>Search range.</returns> |
| 123 | + public static SearchRangeV2<TValue> Lt<TValue>(TValue value) |
| 124 | + => SearchRangeV2<TValue>.Empty.Lt(value); |
| 125 | + |
| 126 | + /// <summary> |
| 127 | + /// Adds a less than value to a search range. |
| 128 | + /// </summary> |
| 129 | + /// <typeparam name="TValue">The type of the value.</typeparam> |
| 130 | + /// <param name="searchRange">Search range.</param> |
| 131 | + /// <param name="value">The value.</param> |
| 132 | + /// <returns>Search range.</returns> |
| 133 | + public static SearchRangeV2<TValue> Lt<TValue>(this SearchRangeV2<TValue> searchRange, TValue value) |
| 134 | + => new(searchRange.Min, max: new(value)); |
| 135 | + |
| 136 | + /// <summary> |
| 137 | + /// Creates a less than or equal search range. |
| 138 | + /// </summary> |
| 139 | + /// <typeparam name="TValue">The type of the value.</typeparam> |
| 140 | + /// <param name="value">The value.</param> |
| 141 | + /// <returns>search range.</returns> |
| 142 | + public static SearchRangeV2<TValue> Lte<TValue>(TValue value) |
| 143 | + => SearchRangeV2<TValue>.Empty.Lte(value); |
| 144 | + |
| 145 | + /// <summary> |
| 146 | + /// Adds a less than or equal value to a search range. |
| 147 | + /// </summary> |
| 148 | + /// <typeparam name="TValue">The type of the value.</typeparam> |
| 149 | + /// <param name="searchRange">Search range.</param> |
| 150 | + /// <param name="value">The value.</param> |
| 151 | + /// <returns>search range.</returns> |
| 152 | + public static SearchRangeV2<TValue> Lte<TValue>(this SearchRangeV2<TValue> searchRange, TValue value) |
| 153 | + => new(searchRange.Min, max: new(value, inclusive: true)); |
| 154 | + } |
| 155 | +} |
0 commit comments