You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

HelpPageSampleKey.cs 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Net.Http.Headers;
  5. namespace CacheControl_test.Areas.HelpPage
  6. {
  7. /// <summary>
  8. /// This is used to identify the place where the sample should be applied.
  9. /// </summary>
  10. public class HelpPageSampleKey
  11. {
  12. /// <summary>
  13. /// Creates a new <see cref="HelpPageSampleKey"/> based on media type.
  14. /// </summary>
  15. /// <param name="mediaType">The media type.</param>
  16. public HelpPageSampleKey(MediaTypeHeaderValue mediaType)
  17. {
  18. if (mediaType == null)
  19. {
  20. throw new ArgumentNullException("mediaType");
  21. }
  22. ActionName = String.Empty;
  23. ControllerName = String.Empty;
  24. MediaType = mediaType;
  25. ParameterNames = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
  26. }
  27. /// <summary>
  28. /// Creates a new <see cref="HelpPageSampleKey"/> based on media type and CLR type.
  29. /// </summary>
  30. /// <param name="mediaType">The media type.</param>
  31. /// <param name="type">The CLR type.</param>
  32. public HelpPageSampleKey(MediaTypeHeaderValue mediaType, Type type)
  33. : this(mediaType)
  34. {
  35. if (type == null)
  36. {
  37. throw new ArgumentNullException("type");
  38. }
  39. ParameterType = type;
  40. }
  41. /// <summary>
  42. /// Creates a new <see cref="HelpPageSampleKey"/> based on <see cref="SampleDirection"/>, controller name, action name and parameter names.
  43. /// </summary>
  44. /// <param name="sampleDirection">The <see cref="SampleDirection"/>.</param>
  45. /// <param name="controllerName">Name of the controller.</param>
  46. /// <param name="actionName">Name of the action.</param>
  47. /// <param name="parameterNames">The parameter names.</param>
  48. public HelpPageSampleKey(SampleDirection sampleDirection, string controllerName, string actionName, IEnumerable<string> parameterNames)
  49. {
  50. if (!Enum.IsDefined(typeof(SampleDirection), sampleDirection))
  51. {
  52. throw new InvalidEnumArgumentException("sampleDirection", (int)sampleDirection, typeof(SampleDirection));
  53. }
  54. if (controllerName == null)
  55. {
  56. throw new ArgumentNullException("controllerName");
  57. }
  58. if (actionName == null)
  59. {
  60. throw new ArgumentNullException("actionName");
  61. }
  62. if (parameterNames == null)
  63. {
  64. throw new ArgumentNullException("parameterNames");
  65. }
  66. ControllerName = controllerName;
  67. ActionName = actionName;
  68. ParameterNames = new HashSet<string>(parameterNames, StringComparer.OrdinalIgnoreCase);
  69. SampleDirection = sampleDirection;
  70. }
  71. /// <summary>
  72. /// Creates a new <see cref="HelpPageSampleKey"/> based on media type, <see cref="SampleDirection"/>, controller name, action name and parameter names.
  73. /// </summary>
  74. /// <param name="mediaType">The media type.</param>
  75. /// <param name="sampleDirection">The <see cref="SampleDirection"/>.</param>
  76. /// <param name="controllerName">Name of the controller.</param>
  77. /// <param name="actionName">Name of the action.</param>
  78. /// <param name="parameterNames">The parameter names.</param>
  79. public HelpPageSampleKey(MediaTypeHeaderValue mediaType, SampleDirection sampleDirection, string controllerName, string actionName, IEnumerable<string> parameterNames)
  80. : this(sampleDirection, controllerName, actionName, parameterNames)
  81. {
  82. if (mediaType == null)
  83. {
  84. throw new ArgumentNullException("mediaType");
  85. }
  86. MediaType = mediaType;
  87. }
  88. /// <summary>
  89. /// Gets the name of the controller.
  90. /// </summary>
  91. /// <value>
  92. /// The name of the controller.
  93. /// </value>
  94. public string ControllerName { get; private set; }
  95. /// <summary>
  96. /// Gets the name of the action.
  97. /// </summary>
  98. /// <value>
  99. /// The name of the action.
  100. /// </value>
  101. public string ActionName { get; private set; }
  102. /// <summary>
  103. /// Gets the media type.
  104. /// </summary>
  105. /// <value>
  106. /// The media type.
  107. /// </value>
  108. public MediaTypeHeaderValue MediaType { get; private set; }
  109. /// <summary>
  110. /// Gets the parameter names.
  111. /// </summary>
  112. public HashSet<string> ParameterNames { get; private set; }
  113. public Type ParameterType { get; private set; }
  114. /// <summary>
  115. /// Gets the <see cref="SampleDirection"/>.
  116. /// </summary>
  117. public SampleDirection? SampleDirection { get; private set; }
  118. public override bool Equals(object obj)
  119. {
  120. HelpPageSampleKey otherKey = obj as HelpPageSampleKey;
  121. if (otherKey == null)
  122. {
  123. return false;
  124. }
  125. return String.Equals(ControllerName, otherKey.ControllerName, StringComparison.OrdinalIgnoreCase) &&
  126. String.Equals(ActionName, otherKey.ActionName, StringComparison.OrdinalIgnoreCase) &&
  127. (MediaType == otherKey.MediaType || (MediaType != null && MediaType.Equals(otherKey.MediaType))) &&
  128. ParameterType == otherKey.ParameterType &&
  129. SampleDirection == otherKey.SampleDirection &&
  130. ParameterNames.SetEquals(otherKey.ParameterNames);
  131. }
  132. public override int GetHashCode()
  133. {
  134. int hashCode = ControllerName.ToUpperInvariant().GetHashCode() ^ ActionName.ToUpperInvariant().GetHashCode();
  135. if (MediaType != null)
  136. {
  137. hashCode ^= MediaType.GetHashCode();
  138. }
  139. if (SampleDirection != null)
  140. {
  141. hashCode ^= SampleDirection.GetHashCode();
  142. }
  143. if (ParameterType != null)
  144. {
  145. hashCode ^= ParameterType.GetHashCode();
  146. }
  147. foreach (string parameterName in ParameterNames)
  148. {
  149. hashCode ^= parameterName.ToUpperInvariant().GetHashCode();
  150. }
  151. return hashCode;
  152. }
  153. }
  154. }