Either.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. //
  2. // Copyright (c) 2008 Novell, Inc. (http://www.novell.com)
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining
  5. // a copy of this software and associated documentation files (the
  6. // "Software"), to deal in the Software without restriction, including
  7. // without limitation the rights to use, copy, modify, merge, publish,
  8. // distribute, sublicense, and/or sell copies of the Software, and to
  9. // permit persons to whom the Software is furnished to do so, subject to
  10. // the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be
  13. // included in all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  19. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  20. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  21. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. //
  23. #pragma warning disable
  24. namespace Fizzler
  25. {
  26. #region Imports
  27. using System;
  28. using System.Collections.Generic;
  29. #endregion
  30. // Adapted from Mono Rocks
  31. internal abstract class Either<TA, TB>
  32. : IEquatable<Either<TA, TB>>
  33. {
  34. private Either() { }
  35. public static Either<TA, TB> A(TA value)
  36. {
  37. if (value == null) throw new ArgumentNullException("value");
  38. return new AImpl(value);
  39. }
  40. public static Either<TA, TB> B(TB value)
  41. {
  42. if (value == null) throw new ArgumentNullException("value");
  43. return new BImpl(value);
  44. }
  45. public override abstract bool Equals(object obj);
  46. public abstract bool Equals(Either<TA, TB> obj);
  47. public override abstract int GetHashCode();
  48. public override abstract string ToString();
  49. public abstract TResult Fold<TResult>(Func<TA, TResult> a, Func<TB, TResult> b);
  50. private sealed class AImpl : Either<TA, TB>
  51. {
  52. private readonly TA _value;
  53. public AImpl(TA value)
  54. {
  55. _value = value;
  56. }
  57. public override int GetHashCode()
  58. {
  59. return _value.GetHashCode();
  60. }
  61. public override bool Equals(object obj)
  62. {
  63. return Equals(obj as AImpl);
  64. }
  65. public override bool Equals(Either<TA, TB> obj)
  66. {
  67. var a = obj as AImpl;
  68. return a != null
  69. && EqualityComparer<TA>.Default.Equals(_value, a._value);
  70. }
  71. public override TResult Fold<TResult>(Func<TA, TResult> a, Func<TB, TResult> b)
  72. {
  73. if (a == null) throw new ArgumentNullException("a");
  74. if (b == null) throw new ArgumentNullException("b");
  75. return a(_value);
  76. }
  77. public override string ToString()
  78. {
  79. return _value.ToString();
  80. }
  81. }
  82. private sealed class BImpl : Either<TA, TB>
  83. {
  84. private readonly TB _value;
  85. public BImpl(TB value)
  86. {
  87. _value = value;
  88. }
  89. public override int GetHashCode()
  90. {
  91. return _value.GetHashCode();
  92. }
  93. public override bool Equals(object obj)
  94. {
  95. return Equals(obj as BImpl);
  96. }
  97. public override bool Equals(Either<TA, TB> obj)
  98. {
  99. var b = obj as BImpl;
  100. return b != null
  101. && EqualityComparer<TB>.Default.Equals(_value, b._value);
  102. }
  103. public override TResult Fold<TResult>(Func<TA, TResult> a, Func<TB, TResult> b)
  104. {
  105. if (a == null) throw new ArgumentNullException("a");
  106. if (b == null) throw new ArgumentNullException("b");
  107. return b(_value);
  108. }
  109. public override string ToString()
  110. {
  111. return _value.ToString();
  112. }
  113. }
  114. }
  115. }
  116. #pragma warning restore