PageRenderTime 38ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/Source/ResourceManagement.ObjectModel/RmAttributeValue.cs

#
C# | 169 lines | 156 code | 12 blank | 1 comment | 24 complexity | 724a6250a1daef8c17d9ccb48ac25199 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 RmAttributeValue : ICloneable
  8. {
  9. List<IComparable> values;
  10. bool isMultiValue;
  11. public RmAttributeValue()
  12. {
  13. this.values = new List<IComparable>();
  14. }
  15. public RmAttributeValue(IComparable value)
  16. {
  17. this.values = new List<IComparable>();
  18. if (value != null)
  19. this.values.Add(value);
  20. }
  21. public RmAttributeValue(IEnumerable<IComparable> values)
  22. {
  23. this.values = new List<IComparable>();
  24. this.values.AddRange(values);
  25. this.isMultiValue = true;
  26. }
  27. public List<IComparable> Values
  28. {
  29. get
  30. {
  31. return this.values;
  32. }
  33. }
  34. public IComparable Value
  35. {
  36. get
  37. {
  38. if (this.values.Count > 0)
  39. {
  40. return this.values[this.values.Count-1];
  41. }
  42. else
  43. {
  44. return null;
  45. }
  46. }
  47. set
  48. {
  49. lock (this.values)
  50. {
  51. if (this.values.Count > 0)
  52. {
  53. if (value == null)
  54. {
  55. this.values.Clear();
  56. }
  57. else
  58. {
  59. this.values.Add(value);
  60. }
  61. }
  62. else
  63. {
  64. this.values.Add(value);
  65. }
  66. }
  67. }
  68. }
  69. public bool IsMultiValue
  70. {
  71. get
  72. {
  73. return this.isMultiValue || this.values.Count > 1;
  74. }
  75. }
  76. #region ICloneable Members
  77. public object Clone()
  78. {
  79. RmAttributeValue newValue = new RmAttributeValue();
  80. newValue.values = new List<IComparable>();
  81. foreach (IComparable value in this.values)
  82. {
  83. ICloneable cloneValue = value as ICloneable;
  84. if (cloneValue == null)
  85. {
  86. newValue.values.Add(value);
  87. }
  88. else
  89. {
  90. IComparable cloneInsert = cloneValue.Clone() as IComparable;
  91. if (cloneInsert == null)
  92. throw new InvalidOperationException("A comparable, when cloned, returned a non-comparable: " + cloneValue.ToString());
  93. newValue.values.Add(cloneInsert);
  94. }
  95. }
  96. return newValue;
  97. }
  98. #endregion
  99. public override bool Equals(object obj)
  100. {
  101. RmAttributeValue other = obj as RmAttributeValue;
  102. if (other == null)
  103. return false;
  104. lock (this.values)
  105. {
  106. lock (other.values)
  107. {
  108. if (this.values.Count != other.values.Count)
  109. return false;
  110. this.values.Sort();
  111. other.values.Sort();
  112. for(int i=0;i<this.values.Count;i++)
  113. {
  114. if (this.values[i].Equals(other.values[i]) == false)
  115. return false;
  116. }
  117. }
  118. }
  119. return true;
  120. }
  121. public override int GetHashCode()
  122. {
  123. return this.ToString().GetHashCode();
  124. }
  125. public override string ToString()
  126. {
  127. lock (this.values)
  128. {
  129. if (this.values.Count == 0)
  130. {
  131. return @"RmAttributeValue.Null";
  132. }
  133. else if (this.values.Count == 1)
  134. {
  135. return this.values[0].ToString();
  136. }
  137. else
  138. {
  139. StringBuilder sb = new StringBuilder(this.values.Count);
  140. foreach (Object v in this.values)
  141. {
  142. sb.Append("{");
  143. sb.Append(v);
  144. sb.Append("}");
  145. sb.Append(@" ");
  146. }
  147. // take off the last space
  148. if (sb.Length > 0)
  149. {
  150. sb.Remove(sb.Length - 1, 1);
  151. }
  152. return sb.ToString();
  153. }
  154. }
  155. }
  156. }
  157. }