using System; using System.Collections.Generic; using System.ComponentModel; using System.Net.Http.Headers; namespace WebAPI.Areas.HelpPage { /// /// This is used to identify the place where the sample should be applied. /// public class HelpPageSampleKey { /// /// Creates a new based on media type. /// /// The media type. public HelpPageSampleKey(MediaTypeHeaderValue mediaType) { if (mediaType == null) { throw new ArgumentNullException("mediaType"); } ActionName = String.Empty; ControllerName = String.Empty; MediaType = mediaType; ParameterNames = new HashSet(StringComparer.OrdinalIgnoreCase); } /// /// Creates a new based on media type and CLR type. /// /// The media type. /// The CLR type. public HelpPageSampleKey(MediaTypeHeaderValue mediaType, Type type) : this(mediaType) { if (type == null) { throw new ArgumentNullException("type"); } ParameterType = type; } /// /// Creates a new based on , controller name, action name and parameter names. /// /// The . /// Name of the controller. /// Name of the action. /// The parameter names. public HelpPageSampleKey(SampleDirection sampleDirection, string controllerName, string actionName, IEnumerable parameterNames) { if (!Enum.IsDefined(typeof(SampleDirection), sampleDirection)) { throw new InvalidEnumArgumentException("sampleDirection", (int)sampleDirection, typeof(SampleDirection)); } if (controllerName == null) { throw new ArgumentNullException("controllerName"); } if (actionName == null) { throw new ArgumentNullException("actionName"); } if (parameterNames == null) { throw new ArgumentNullException("parameterNames"); } ControllerName = controllerName; ActionName = actionName; ParameterNames = new HashSet(parameterNames, StringComparer.OrdinalIgnoreCase); SampleDirection = sampleDirection; } /// /// Creates a new based on media type, , controller name, action name and parameter names. /// /// The media type. /// The . /// Name of the controller. /// Name of the action. /// The parameter names. public HelpPageSampleKey(MediaTypeHeaderValue mediaType, SampleDirection sampleDirection, string controllerName, string actionName, IEnumerable parameterNames) : this(sampleDirection, controllerName, actionName, parameterNames) { if (mediaType == null) { throw new ArgumentNullException("mediaType"); } MediaType = mediaType; } /// /// Gets the name of the controller. /// /// /// The name of the controller. /// public string ControllerName { get; private set; } /// /// Gets the name of the action. /// /// /// The name of the action. /// public string ActionName { get; private set; } /// /// Gets the media type. /// /// /// The media type. /// public MediaTypeHeaderValue MediaType { get; private set; } /// /// Gets the parameter names. /// public HashSet ParameterNames { get; private set; } public Type ParameterType { get; private set; } /// /// Gets the . /// public SampleDirection? SampleDirection { get; private set; } public override bool Equals(object obj) { HelpPageSampleKey otherKey = obj as HelpPageSampleKey; if (otherKey == null) { return false; } return String.Equals(ControllerName, otherKey.ControllerName, StringComparison.OrdinalIgnoreCase) && String.Equals(ActionName, otherKey.ActionName, StringComparison.OrdinalIgnoreCase) && (MediaType == otherKey.MediaType || (MediaType != null && MediaType.Equals(otherKey.MediaType))) && ParameterType == otherKey.ParameterType && SampleDirection == otherKey.SampleDirection && ParameterNames.SetEquals(otherKey.ParameterNames); } public override int GetHashCode() { int hashCode = ControllerName.ToUpperInvariant().GetHashCode() ^ ActionName.ToUpperInvariant().GetHashCode(); if (MediaType != null) { hashCode ^= MediaType.GetHashCode(); } if (SampleDirection != null) { hashCode ^= SampleDirection.GetHashCode(); } if (ParameterType != null) { hashCode ^= ParameterType.GetHashCode(); } foreach (string parameterName in ParameterNames) { hashCode ^= parameterName.ToUpperInvariant().GetHashCode(); } return hashCode; } } }