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.8KB

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