Either.Generic.cs 3.4 KB

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