// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. using System.Collections.Generic; using System.Globalization; namespace System.Reactive { abstract class Either { Either() { } public static Either CreateLeft(TLeft value) { return new Either.Left(value); } public static Either CreateRight(TRight value) { return new Either.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 TLeft Value { get; private set; } public Left(TLeft value) { Value = value; } public override TResult Switch(Func caseLeft, Func caseRight) { return 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) { return Equals(obj as Left); } public override int GetHashCode() { return EqualityComparer.Default.GetHashCode(Value); } public override string ToString() { return string.Format(CultureInfo.CurrentCulture, "Left({0})", Value); } } public sealed class Right : Either, IEquatable { public TRight Value { get; private set; } public Right(TRight value) { Value = value; } public override TResult Switch(Func caseLeft, Func caseRight) { return 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) { return Equals(obj as Right); } public override int GetHashCode() { return EqualityComparer.Default.GetHashCode(Value); } public override string ToString() { return string.Format(CultureInfo.CurrentCulture, "Right({0})", Value); } } } }