2021-10-01 09:09:20 +00:00
using System ;
using System.Collections.Generic ;
using System.ComponentModel ;
using System.Net.Http.Headers ;
namespace Service.MeterProcessState.Areas.HelpPage
{
/// <summary>
/// This is used to identify the place where the sample should be applied.
/// </summary>
public class HelpPageSampleKey
{
/// <summary>
/// Creates a new <see cref="HelpPageSampleKey"/> based on media type.
/// </summary>
/// <param name="mediaType">The media type.</param>
public HelpPageSampleKey ( MediaTypeHeaderValue mediaType )
{
if ( mediaType = = null )
{
throw new ArgumentNullException ( "mediaType" ) ;
}
2022-10-19 10:58:08 +00:00
ActionName = string . Empty ;
ControllerName = string . Empty ;
2021-10-01 09:09:20 +00:00
MediaType = mediaType ;
2022-10-19 10:58:08 +00:00
ParameterNames = new HashSet < String > ( StringComparer . OrdinalIgnoreCase ) ;
2021-10-01 09:09:20 +00:00
}
/// <summary>
/// Creates a new <see cref="HelpPageSampleKey"/> based on media type and CLR type.
/// </summary>
/// <param name="mediaType">The media type.</param>
/// <param name="type">The CLR type.</param>
public HelpPageSampleKey ( MediaTypeHeaderValue mediaType , Type type )
: this ( mediaType )
{
if ( type = = null )
{
throw new ArgumentNullException ( "type" ) ;
}
ParameterType = type ;
}
/// <summary>
/// Creates a new <see cref="HelpPageSampleKey"/> based on <see cref="SampleDirection"/>, controller name, action name and parameter names.
/// </summary>
/// <param name="sampleDirection">The <see cref="SampleDirection"/>.</param>
/// <param name="controllerName">Name of the controller.</param>
/// <param name="actionName">Name of the action.</param>
/// <param name="parameterNames">The parameter names.</param>
2022-10-19 10:58:08 +00:00
public HelpPageSampleKey ( SampleDirection sampleDirection , String controllerName , String actionName , IEnumerable < String > parameterNames )
2021-10-01 09:09:20 +00:00
{
if ( ! Enum . IsDefined ( typeof ( SampleDirection ) , sampleDirection ) )
{
2022-10-19 10:58:08 +00:00
throw new InvalidEnumArgumentException ( "sampleDirection" , ( Int32 ) sampleDirection , typeof ( SampleDirection ) ) ;
2021-10-01 09:09:20 +00:00
}
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 ;
2022-10-19 10:58:08 +00:00
ParameterNames = new HashSet < String > ( parameterNames , StringComparer . OrdinalIgnoreCase ) ;
2021-10-01 09:09:20 +00:00
SampleDirection = sampleDirection ;
}
/// <summary>
/// Creates a new <see cref="HelpPageSampleKey"/> based on media type, <see cref="SampleDirection"/>, controller name, action name and parameter names.
/// </summary>
/// <param name="mediaType">The media type.</param>
/// <param name="sampleDirection">The <see cref="SampleDirection"/>.</param>
/// <param name="controllerName">Name of the controller.</param>
/// <param name="actionName">Name of the action.</param>
/// <param name="parameterNames">The parameter names.</param>
2022-10-19 10:58:08 +00:00
public HelpPageSampleKey ( MediaTypeHeaderValue mediaType , SampleDirection sampleDirection , String controllerName , String actionName , IEnumerable < String > parameterNames )
2021-10-01 09:09:20 +00:00
: this ( sampleDirection , controllerName , actionName , parameterNames )
{
if ( mediaType = = null )
{
throw new ArgumentNullException ( "mediaType" ) ;
}
MediaType = mediaType ;
}
/// <summary>
/// Gets the name of the controller.
/// </summary>
/// <value>
/// The name of the controller.
/// </value>
2022-10-19 10:58:08 +00:00
public String ControllerName { get ; private set ; }
2021-10-01 09:09:20 +00:00
/// <summary>
/// Gets the name of the action.
/// </summary>
/// <value>
/// The name of the action.
/// </value>
2022-10-19 10:58:08 +00:00
public String ActionName { get ; private set ; }
2021-10-01 09:09:20 +00:00
/// <summary>
/// Gets the media type.
/// </summary>
/// <value>
/// The media type.
/// </value>
public MediaTypeHeaderValue MediaType { get ; private set ; }
/// <summary>
/// Gets the parameter names.
/// </summary>
2022-10-19 10:58:08 +00:00
public HashSet < String > ParameterNames { get ; private set ; }
2021-10-01 09:09:20 +00:00
public Type ParameterType { get ; private set ; }
/// <summary>
/// Gets the <see cref="SampleDirection"/>.
/// </summary>
public SampleDirection ? SampleDirection { get ; private set ; }
2022-10-19 10:58:08 +00:00
public override Boolean Equals ( Object obj )
2021-10-01 09:09:20 +00:00
{
HelpPageSampleKey otherKey = obj as HelpPageSampleKey ;
if ( otherKey = = null )
{
return false ;
}
2022-10-19 10:58:08 +00:00
return string . Equals ( ControllerName , otherKey . ControllerName , StringComparison . OrdinalIgnoreCase ) & &
string . Equals ( ActionName , otherKey . ActionName , StringComparison . OrdinalIgnoreCase ) & &
2021-10-01 09:09:20 +00:00
( MediaType = = otherKey . MediaType | | ( MediaType ! = null & & MediaType . Equals ( otherKey . MediaType ) ) ) & &
ParameterType = = otherKey . ParameterType & &
SampleDirection = = otherKey . SampleDirection & &
ParameterNames . SetEquals ( otherKey . ParameterNames ) ;
}
2022-10-19 10:58:08 +00:00
public override Int32 GetHashCode ( )
2021-10-01 09:09:20 +00:00
{
2022-10-19 10:58:08 +00:00
Int32 hashCode = ControllerName . ToUpperInvariant ( ) . GetHashCode ( ) ^ ActionName . ToUpperInvariant ( ) . GetHashCode ( ) ;
2021-10-01 09:09:20 +00:00
if ( MediaType ! = null )
{
hashCode ^ = MediaType . GetHashCode ( ) ;
}
if ( SampleDirection ! = null )
{
hashCode ^ = SampleDirection . GetHashCode ( ) ;
}
if ( ParameterType ! = null )
{
hashCode ^ = ParameterType . GetHashCode ( ) ;
}
2022-10-19 10:58:08 +00:00
foreach ( String parameterName in ParameterNames )
2021-10-01 09:09:20 +00:00
{
hashCode ^ = parameterName . ToUpperInvariant ( ) . GetHashCode ( ) ;
}
return hashCode ;
}
}
}