// 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; using System.Globalization; namespace System.Reactive { internal abstract class Either { private Either() { } public static Either CreateLeft(TLeft value) => new Left(value); public static Either CreateRight(TRight value) => new Right(value); public abstract TResult Switch(Func caseLeft, Func caseRight); public abstract void Switch(Action caseLeft, Action caseRight); public sealed class Left : Either, IEquatable { public Left(TLeft value) { Value = value; } public TLeft Value { get; } public override TResult Switch(Func caseLeft, Func caseRight) => caseLeft(Value); public override void Switch(Action caseLeft, Action caseRight) => caseLeft(Value); public bool Equals(Left? other) { if (other == this) { return true; } if (other == null) { return false; } return EqualityComparer.Default.Equals(Value, other.Value); } public override bool Equals(object? obj) => Equals(obj as Left); public override int GetHashCode() => Value?.GetHashCode() ?? 0; public override string ToString() { return string.Format(CultureInfo.CurrentCulture, "Left({0})", Value); } } public sealed class Right : Either, IEquatable { public Right(TRight value) { Value = value; } public TRight Value { get; } public override TResult Switch(Func caseLeft, Func caseRight) => caseRight(Value); public override void Switch(Action caseLeft, Action caseRight) => caseRight(Value); public bool Equals(Right? other) { if (other == this) { return true; } if (other == null) { return false; } return EqualityComparer.Default.Equals(Value, other.Value); } public override bool Equals(object? obj) => Equals(obj as Right); public override int GetHashCode() => Value?.GetHashCode() ?? 0; public override string ToString() => string.Format(CultureInfo.CurrentCulture, "Right({0})", Value); } } }