Kit.Core/LibExternal/System.Reactive/EventPattern.cs

118 lines
6.3 KiB
C#

// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT License.
// See the LICENSE file in the project root for more information.
using System.Collections.Generic;
namespace System.Reactive
{
/// <summary>
/// Represents a .NET event invocation consisting of the weakly typed object that raised the event and the data that was generated by the event.
/// </summary>
/// <typeparam name="TEventArgs">The type of the event data generated by the event.</typeparam>
public class EventPattern<TEventArgs> : EventPattern<object, TEventArgs>
{
/// <summary>
/// Creates a new data representation instance of a .NET event invocation with the given sender and event data.
/// </summary>
/// <param name="sender">The sender object that raised the event.</param>
/// <param name="e">The event data that was generated by the event.</param>
#pragma warning disable CA2109 // (Consider making non-public.) This has long been part of the public API so we couldn't change this even if we wanted to.
public EventPattern(object? sender, TEventArgs e)
#pragma warning restore CA2109
: base(sender, e)
{
}
}
/// <summary>
/// Represents a .NET event invocation consisting of the strongly typed object that raised the event and the data that was generated by the event.
/// </summary>
/// <typeparam name="TSender">The type of the sender that raised the event.</typeparam>
/// <typeparam name="TEventArgs">The type of the event data generated by the event.</typeparam>
public class EventPattern<TSender, TEventArgs> : IEquatable<EventPattern<TSender, TEventArgs>>, IEventPattern<TSender, TEventArgs>
{
/// <summary>
/// Creates a new data representation instance of a .NET event invocation with the given sender and event data.
/// </summary>
/// <param name="sender">The sender object that raised the event.</param>
/// <param name="e">The event data that was generated by the event.</param>
public EventPattern(TSender? sender, TEventArgs e)
{
Sender = sender;
EventArgs = e;
}
/// <summary>
/// Gets the sender object that raised the event.
/// </summary>
public TSender? Sender { get; }
/// <summary>
/// Gets the event data that was generated by the event.
/// </summary>
public TEventArgs EventArgs { get; }
/// <summary>
/// Deconstructs the event pattern value into a sender and event data.
/// </summary>
/// <param name="sender">The sender object that raised the event.</param>
/// <param name="e">The event data that was generated by the event.</param>
public void Deconstruct(out TSender? sender, out TEventArgs e) => (sender, e) = (Sender, EventArgs);
/// <summary>
/// Determines whether the current <see cref="EventPattern{TSender, TEventArgs}"/> object represents the same event as a specified <see cref="EventPattern{TSender, TEventArgs}"/> object.
/// </summary>
/// <param name="other">An object to compare to the current <see cref="EventPattern{TSender, TEventArgs}"/> object.</param>
/// <returns><c>true</c> if both <see cref="EventPattern{TSender, TEventArgs}"/> objects represent the same event; otherwise, <c>false</c>.</returns>
public bool Equals(EventPattern<TSender, TEventArgs>? other)
{
if (other is null)
{
return false;
}
if (ReferenceEquals(this, other))
{
return true;
}
return EqualityComparer<TSender?>.Default.Equals(Sender, other.Sender) && EqualityComparer<TEventArgs>.Default.Equals(EventArgs, other.EventArgs);
}
/// <summary>
/// Determines whether the specified System.Object is equal to the current <see cref="EventPattern{TSender, TEventArgs}"/>.
/// </summary>
/// <param name="obj">The System.Object to compare with the current <see cref="EventPattern{TSender, TEventArgs}"/>.</param>
/// <returns><c>true</c> if the specified System.Object is equal to the current <see cref="EventPattern{TSender, TEventArgs}"/>; otherwise, <c>false</c>.</returns>
public override bool Equals(object? obj) => Equals(obj as EventPattern<TSender, TEventArgs>);
/// <summary>
/// Returns the hash code for the current <see cref="EventPattern{TSender, TEventArgs}"/> instance.
/// </summary>
/// <returns>A hash code for the current <see cref="EventPattern{TSender, TEventArgs}"/> instance.</returns>
public override int GetHashCode()
{
var x = Sender?.GetHashCode() ?? 0;
var y = EventArgs?.GetHashCode() ?? 0;
return (x << 5) + (x ^ y);
}
/// <summary>
/// Determines whether two specified <see cref="EventPattern{TSender, TEventArgs}"/> objects represent the same event.
/// </summary>
/// <param name="first">The first <see cref="EventPattern{TSender, TEventArgs}"/> to compare, or <c>null</c>.</param>
/// <param name="second">The second <see cref="EventPattern{TSender, TEventArgs}"/> to compare, or <c>null</c>.</param>
/// <returns><c>true</c> if both <see cref="EventPattern{TSender, TEventArgs}"/> objects represent the same event; otherwise, <c>false</c>.</returns>
public static bool operator ==(EventPattern<TSender, TEventArgs> first, EventPattern<TSender, TEventArgs> second) => Equals(first, second);
/// <summary>
/// Determines whether two specified <see cref="EventPattern{TSender, TEventArgs}"/> objects represent a different event.
/// </summary>
/// <param name="first">The first <see cref="EventPattern{TSender, TEventArgs}"/> to compare, or <c>null</c>.</param>
/// <param name="second">The second <see cref="EventPattern{TSender, TEventArgs}"/> to compare, or <c>null</c>.</param>
/// <returns><c>true</c> if both <see cref="EventPattern{TSender, TEventArgs}"/> objects don't represent the same event; otherwise, <c>false</c>.</returns>
public static bool operator !=(EventPattern<TSender, TEventArgs> first, EventPattern<TSender, TEventArgs> second) => !Equals(first, second);
}
}