// 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 { /// /// Represents a .NET event invocation consisting of the weakly typed object that raised the event and the data that was generated by the event. /// /// The type of the event data generated by the event. public class EventPattern : EventPattern { /// /// Creates a new data representation instance of a .NET event invocation with the given sender and event data. /// /// The sender object that raised the event. /// The event data that was generated by the event. #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) { } } /// /// Represents a .NET event invocation consisting of the strongly typed object that raised the event and the data that was generated by the event. /// /// The type of the sender that raised the event. /// The type of the event data generated by the event. public class EventPattern : IEquatable>, IEventPattern { /// /// Creates a new data representation instance of a .NET event invocation with the given sender and event data. /// /// The sender object that raised the event. /// The event data that was generated by the event. public EventPattern(TSender? sender, TEventArgs e) { Sender = sender; EventArgs = e; } /// /// Gets the sender object that raised the event. /// public TSender? Sender { get; } /// /// Gets the event data that was generated by the event. /// public TEventArgs EventArgs { get; } /// /// Deconstructs the event pattern value into a sender and event data. /// /// The sender object that raised the event. /// The event data that was generated by the event. public void Deconstruct(out TSender? sender, out TEventArgs e) => (sender, e) = (Sender, EventArgs); /// /// Determines whether the current object represents the same event as a specified object. /// /// An object to compare to the current object. /// true if both objects represent the same event; otherwise, false. public bool Equals(EventPattern? other) { if (other is null) { return false; } if (ReferenceEquals(this, other)) { return true; } return EqualityComparer.Default.Equals(Sender, other.Sender) && EqualityComparer.Default.Equals(EventArgs, other.EventArgs); } /// /// Determines whether the specified System.Object is equal to the current . /// /// The System.Object to compare with the current . /// true if the specified System.Object is equal to the current ; otherwise, false. public override bool Equals(object? obj) => Equals(obj as EventPattern); /// /// Returns the hash code for the current instance. /// /// A hash code for the current instance. public override int GetHashCode() { var x = Sender?.GetHashCode() ?? 0; var y = EventArgs?.GetHashCode() ?? 0; return (x << 5) + (x ^ y); } /// /// Determines whether two specified objects represent the same event. /// /// The first to compare, or null. /// The second to compare, or null. /// true if both objects represent the same event; otherwise, false. public static bool operator ==(EventPattern first, EventPattern second) => Equals(first, second); /// /// Determines whether two specified objects represent a different event. /// /// The first to compare, or null. /// The second to compare, or null. /// true if both objects don't represent the same event; otherwise, false. public static bool operator !=(EventPattern first, EventPattern second) => !Equals(first, second); } }