Either.Generic.cs 3.4 KB

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