AggregateFacetResult.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /* Copyright 2016-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. using System.Collections.Generic;
  16. using System.Linq;
  17. using MongoDB.Driver.Core.Misc;
  18. namespace MongoDB.Driver
  19. {
  20. /// <summary>
  21. /// Represents an abstract AggregateFacetResult with an arbitrary TOutput type.
  22. /// </summary>
  23. public abstract class AggregateFacetResult
  24. {
  25. /// <summary>
  26. /// Initializes a new instance of the <see cref="AggregateFacetResult" /> class.
  27. /// </summary>
  28. /// <param name="name">The name of the facet.</param>
  29. internal AggregateFacetResult(string name)
  30. {
  31. Name = Ensure.IsNotNull(name, nameof(name));
  32. }
  33. /// <summary>
  34. /// Gets the name of the facet.
  35. /// </summary>
  36. public string Name { get; private set; }
  37. /// <summary>
  38. /// Gets the output of the facet.
  39. /// </summary>
  40. /// <typeparam name="TOutput">The type of the output documents.</typeparam>
  41. /// <returns>The output of the facet.</returns>
  42. public IReadOnlyList<TOutput> Output<TOutput>()
  43. {
  44. return ((AggregateFacetResult<TOutput>)this).Output;
  45. }
  46. }
  47. /// <summary>
  48. /// Represents the result of a single facet.
  49. /// </summary>
  50. /// <typeparam name="TOutput">The type of the output.</typeparam>
  51. public sealed class AggregateFacetResult<TOutput> : AggregateFacetResult
  52. {
  53. /// <summary>
  54. /// Initializes a new instance of the <see cref="AggregateFacetResult{TOutput}"/> class.
  55. /// </summary>
  56. /// <param name="name">The name.</param>
  57. /// <param name="output">The output.</param>
  58. public AggregateFacetResult(string name, IEnumerable<TOutput> output)
  59. : base(name)
  60. {
  61. Ensure.IsNotNull(output, nameof(output));
  62. var readOnlyList = output as IReadOnlyList<TOutput>;
  63. if (readOnlyList != null)
  64. {
  65. Output = readOnlyList;
  66. }
  67. else
  68. {
  69. Output = output.ToArray();
  70. }
  71. }
  72. /// <summary>
  73. /// Gets or sets the output.
  74. /// </summary>
  75. /// <value>
  76. /// The output.
  77. /// </value>
  78. public IReadOnlyList<TOutput> Output { get; set; }
  79. }
  80. }