| 1 |
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. |
| 2 |
|
| 3 |
using System.Collections.Generic; |
| 4 |
using System.Globalization; |
| 5 |
|
| 6 |
namespace System.Reactive |
| 7 |
{ |
| 8 |
abstract class Either<TLeft, TRight> |
| 9 |
{ |
| 10 |
Either() |
| 11 |
{ |
| 12 |
} |
| 13 |
|
| 14 |
public static Either<TLeft, TRight> CreateLeft(TLeft value) |
| 15 |
{ |
| 16 |
return new Either<TLeft, TRight>.Left(value); |
| 17 |
} |
| 18 |
|
| 19 |
public static Either<TLeft, TRight> CreateRight(TRight value) |
| 20 |
{ |
| 21 |
return new Either<TLeft, TRight>.Right(value); |
| 22 |
} |
| 23 |
|
| 24 |
public abstract TResult Switch<TResult>(Func<TLeft, TResult> caseLeft, Func<TRight, TResult> caseRight); |
| 25 |
public abstract void Switch(Action<TLeft> caseLeft, Action<TRight> caseRight); |
| 26 |
|
| 27 |
public sealed class Left : Either<TLeft, TRight>, IEquatable<Left> |
| 28 |
{ |
| 29 |
public TLeft Value { get; private set; } |
| 30 |
|
| 31 |
public Left(TLeft value) |
| 32 |
{ |
| 33 |
Value = value; |
| 34 |
} |
| 35 |
|
| 36 |
public override TResult Switch<TResult>(Func<TLeft, TResult> caseLeft, Func<TRight, TResult> caseRight) |
| 37 |
{ |
| 38 |
return caseLeft(Value); |
| 39 |
} |
| 40 |
|
| 41 |
public override void Switch(Action<TLeft> caseLeft, Action<TRight> caseRight) |
| 42 |
{ |
| 43 |
caseLeft(Value); |
| 44 |
} |
| 45 |
|
| 46 |
public bool Equals(Left other) |
| 47 |
{ |
| 48 |
if (other == this) |
| 49 |
return true; |
| 50 |
if (other == null) |
| 51 |
return false; |
| 52 |
return EqualityComparer<TLeft>.Default.Equals(Value, other.Value); |
| 53 |
} |
| 54 |
|
| 55 |
public override bool Equals(object obj) |
| 56 |
{ |
| 57 |
return Equals(obj as Left); |
| 58 |
} |
| 59 |
|
| 60 |
public override int GetHashCode() |
| 61 |
{ |
| 62 |
return EqualityComparer<TLeft>.Default.GetHashCode(Value); |
| 63 |
} |
| 64 |
|
| 65 |
public override string ToString() |
| 66 |
{ |
| 67 |
return string.Format(CultureInfo.CurrentCulture, "Left({0})", Value); |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
public sealed class Right : Either<TLeft, TRight>, IEquatable<Right> |
| 72 |
{ |
| 73 |
public TRight Value { get; private set; } |
| 74 |
|
| 75 |
public Right(TRight value) |
| 76 |
{ |
| 77 |
Value = value; |
| 78 |
} |
| 79 |
|
| 80 |
public override TResult Switch<TResult>(Func<TLeft, TResult> caseLeft, Func<TRight, TResult> caseRight) |
| 81 |
{ |
| 82 |
return caseRight(Value); |
| 83 |
} |
| 84 |
|
| 85 |
public override void Switch(Action<TLeft> caseLeft, Action<TRight> caseRight) |
| 86 |
{ |
| 87 |
caseRight(Value); |
| 88 |
} |
| 89 |
|
| 90 |
public bool Equals(Right other) |
| 91 |
{ |
| 92 |
if (other == this) |
| 93 |
return true; |
| 94 |
if (other == null) |
| 95 |
return false; |
| 96 |
return EqualityComparer<TRight>.Default.Equals(Value, other.Value); |
| 97 |
} |
| 98 |
|
| 99 |
public override bool Equals(object obj) |
| 100 |
{ |
| 101 |
return Equals(obj as Right); |
| 102 |
} |
| 103 |
|
| 104 |
public override int GetHashCode() |
| 105 |
{ |
| 106 |
return EqualityComparer<TRight>.Default.GetHashCode(Value); |
| 107 |
} |
| 108 |
|
| 109 |
public override string ToString() |
| 110 |
{ |
| 111 |
return string.Format(CultureInfo.CurrentCulture, "Right({0})", Value); |
| 112 |
} |
| 113 |
} |
| 114 |
} |
| 115 |
} |