PageRenderTime 61ms CodeModel.GetById 34ms RepoModel.GetById 1ms app.codeStats 0ms

/Source/ResourceManagement.ObjectModel/RmReference.cs

#
C# | 146 lines | 127 code | 18 blank | 1 comment | 28 complexity | 34fd1fcfe8de0f90ec7bb683199125e4 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace Microsoft.ResourceManagement.ObjectModel
  6. {
  7. public class RmReference : IComparable, IComparable<RmReference>
  8. {
  9. Guid guidValue;
  10. String stringValue;
  11. public RmReference()
  12. {
  13. this.Value = String.Empty;
  14. }
  15. public RmReference(String value)
  16. {
  17. this.Value = value;
  18. }
  19. static String RemoveReference(String input)
  20. {
  21. if (String.IsNullOrEmpty(input))
  22. return Guid.Empty.ToString();
  23. else
  24. return input.Replace(@"urn:uuid:", String.Empty);
  25. }
  26. static String AddReference(String input)
  27. {
  28. if (String.IsNullOrEmpty(input))
  29. return null;
  30. else
  31. //return String.Format(@"urn:uuid:{0}", this.value.ToString());
  32. return input;
  33. }
  34. public String Value
  35. {
  36. get
  37. {
  38. return this.stringValue;
  39. }
  40. set
  41. {
  42. try
  43. {
  44. this.guidValue = new Guid(RmReference.RemoveReference(value));
  45. this.stringValue = RmReference.AddReference(this.guidValue.ToString());
  46. }
  47. catch (FormatException)
  48. {
  49. throw new ArgumentException("The provided value did not match the reference format of urn:uuid:{guid}");
  50. }
  51. }
  52. }
  53. public override string ToString()
  54. {
  55. return this.stringValue ;
  56. }
  57. public override bool Equals(object obj)
  58. {
  59. RmReference other = obj as RmReference;
  60. if (other as Object == null || other.guidValue == null)
  61. return false;
  62. else
  63. return other.guidValue.Equals(this.guidValue);
  64. }
  65. public override int GetHashCode()
  66. {
  67. return this.guidValue.GetHashCode();
  68. }
  69. #region IComparable Members
  70. public int CompareTo(object obj)
  71. {
  72. if (obj as Object == null)
  73. throw new ArgumentNullException("obj");
  74. RmReference reference = obj as RmReference;
  75. if(reference as Object == null)
  76. throw new ArgumentNullException("obj");
  77. return this.CompareTo(reference);
  78. }
  79. public static bool operator ==(RmReference attrib1, RmReference attrib2)
  80. {
  81. if (attrib1 as Object == null)
  82. return false;
  83. if (attrib2 as Object == null)
  84. return false;
  85. return attrib1.CompareTo(attrib2) == 0;
  86. }
  87. public static bool operator !=(RmReference attrib1, RmReference attrib2)
  88. {
  89. if (attrib1 == null)
  90. return false;
  91. return attrib1.CompareTo(attrib2) != 0;
  92. }
  93. public static bool operator <(RmReference attrib1, RmReference attrib2)
  94. {
  95. if (attrib1 == null)
  96. return false;
  97. return attrib1.CompareTo(attrib2) < 0;
  98. }
  99. public static bool operator >(RmReference attrib1, RmReference attrib2)
  100. {
  101. if (attrib1 == null)
  102. return false;
  103. return attrib1.CompareTo(attrib2) > 0;
  104. }
  105. public static bool operator <=(RmReference attrib1, RmReference attrib2)
  106. {
  107. if (attrib1 == null)
  108. return false;
  109. return attrib1.CompareTo(attrib2) <= 0;
  110. }
  111. public static bool operator >=(RmReference attrib1, RmReference attrib2)
  112. {
  113. if (attrib1 == null)
  114. return false;
  115. return attrib1.CompareTo(attrib2) >= 0;
  116. }
  117. #endregion
  118. #region IComparable<RmReference> Members
  119. public int CompareTo(RmReference other)
  120. {
  121. if (other as object == null)
  122. throw new ArgumentNullException("other");
  123. else
  124. return this.guidValue.CompareTo(other.guidValue);
  125. }
  126. #endregion
  127. }
  128. }