PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/Spss/SpssVariableValueLabels.cs

#
C# | 223 lines | 124 code | 40 blank | 59 comment | 14 complexity | 65d535adfa2f1fa3bb9677159017f4c8 MD5 | raw file
Possible License(s): LGPL-2.1
  1. namespace Spss {
  2. using System;
  3. using System.Diagnostics;
  4. using System.Collections;
  5. using System.Collections.Specialized;
  6. using System.Collections.Generic;
  7. /// <summary>
  8. /// A collection of value labels for a <see cref="SpssVariable"/>.
  9. /// </summary>
  10. public abstract class SpssVariableValueLabelsDictionary<TKey> : IDictionary<TKey, string> {
  11. /// <summary>
  12. /// Tracks whether the value labels have been initialized from a data file yet.
  13. /// </summary>
  14. private bool isLoadedFromFileYet = false;
  15. private bool isLoading = false;
  16. private readonly Dictionary<TKey, string> ValuesLabels;
  17. /// <summary>
  18. /// Creates an instance of the <see cref="SpssVariableValueLabelsDictionary&lt;TKey&gt;"/> class.
  19. /// </summary>
  20. /// <param name="variable">The hosting variable</param>
  21. /// <param name="comparer">The comparer; may be <c>null</c>.</param>
  22. protected SpssVariableValueLabelsDictionary(SpssVariable variable, IEqualityComparer<TKey> comparer) {
  23. if (variable == null) {
  24. throw new ArgumentNullException("variable");
  25. }
  26. this.Variable = variable;
  27. this.ValuesLabels = new Dictionary<TKey, string>(4, comparer);
  28. }
  29. /// <summary>
  30. /// Gets a value indicating whether this list of value labels is read only.
  31. /// </summary>
  32. public bool IsReadOnly {
  33. get {
  34. return Variable.Variables != null && Variable.Variables.IsReadOnly;
  35. }
  36. }
  37. /// <summary>
  38. /// The variable whose labels are being listed.
  39. /// </summary>
  40. protected SpssVariable Variable { get; private set; }
  41. /// <summary>
  42. /// Gets the SPSS file handle of the data document.
  43. /// </summary>
  44. protected int FileHandle {
  45. get {
  46. return Variable.Variables.Document.Handle;
  47. }
  48. }
  49. /// <summary>
  50. /// Gets or sets the response label for some response value.
  51. /// </summary>
  52. public string this[TKey Value] {
  53. get {
  54. this.LoadIfNeeded();
  55. return this.ValuesLabels[Value];
  56. }
  57. set {
  58. this.EnsureNotReadOnly();
  59. this.ValuesLabels[Value] = value;
  60. }
  61. }
  62. #region Operations
  63. /// <summary>
  64. /// Adds a value label.
  65. /// </summary>
  66. /// <param name="value">
  67. /// The response value to associate with the new response label.
  68. /// </param>
  69. /// <param name="label">
  70. /// The new response label.
  71. /// </param>
  72. public virtual void Add(TKey value, string label) {
  73. this.EnsureNotReadOnly();
  74. this.ValuesLabels.Add(value, label);
  75. }
  76. /// <summary>
  77. /// Removes a value label.
  78. /// </summary>
  79. /// <param name="value">
  80. /// The response value to remove.
  81. /// </param>
  82. public bool Remove(TKey value) {
  83. this.EnsureNotReadOnly();
  84. return this.ValuesLabels.Remove(value);
  85. }
  86. /// <summary>
  87. /// Updates the SPSS data file with changes made to the collection.
  88. /// </summary>
  89. protected internal abstract void Update();
  90. /// <summary>
  91. /// Initializes the value labels dictionary from the SPSS data file.
  92. /// </summary>
  93. protected abstract void LoadFromSpssFile();
  94. /// <summary>
  95. /// Throws an <see cref="InvalidOperationException"/> if the list of
  96. /// value labels should not be altered at this state.
  97. /// </summary>
  98. protected void EnsureNotReadOnly() {
  99. if (IsReadOnly && !isLoading)
  100. throw new InvalidOperationException("Cannot perform this operation after dictionary has been committed.");
  101. }
  102. /// <summary>
  103. /// Copies the value labels defined in this list to another variable.
  104. /// </summary>
  105. public void CopyTo(SpssVariableValueLabelsDictionary<TKey> array) {
  106. if (array == null) throw new ArgumentNullException("array");
  107. if (array.GetType() != GetType())
  108. throw new ArgumentException("Copying value labels must be made to the same type of variable (not numeric to string or vice versa).", "array");
  109. foreach (var de in this)
  110. array.ValuesLabels.Add(de.Key, de.Value);
  111. }
  112. #endregion
  113. #region ICollection Members
  114. /// <summary>
  115. /// Gets the number of value labels that are defined.
  116. /// </summary>
  117. public int Count {
  118. get {
  119. this.LoadIfNeeded();
  120. return this.ValuesLabels.Count;
  121. }
  122. }
  123. #endregion
  124. #region IEnumerable Members
  125. /// <summary>
  126. /// Gets the enumerator for the class.
  127. /// </summary>
  128. public IEnumerator<KeyValuePair<TKey, string>> GetEnumerator() {
  129. this.LoadIfNeeded();
  130. return this.ValuesLabels.GetEnumerator();
  131. }
  132. #endregion
  133. private void LoadIfNeeded() {
  134. // If we are working on a loaded file rather than a newly created
  135. // one, and if we have not yet loaded the value labels, load them now.
  136. if (this.IsReadOnly && !this.isLoadedFromFileYet && !this.Variable.CommittedThisSession) {
  137. Debug.Assert(this.ValuesLabels.Count == 0, "Somehow, a loaded file already has labels defined.");
  138. this.isLoading = true;
  139. this.LoadFromSpssFile();
  140. this.isLoading = false;
  141. this.isLoadedFromFileYet = true;
  142. }
  143. }
  144. #region IDictionary<TKey,string> Members
  145. public bool ContainsKey(TKey key) {
  146. return this.ValuesLabels.ContainsKey(key);
  147. }
  148. public ICollection<TKey> Keys {
  149. get { return this.ValuesLabels.Keys; }
  150. }
  151. public bool TryGetValue(TKey key, out string value) {
  152. return this.ValuesLabels.TryGetValue(key, out value);
  153. }
  154. public ICollection<string> Values {
  155. get { return this.ValuesLabels.Values; }
  156. }
  157. #endregion
  158. #region ICollection<KeyValuePair<TKey,string>> Members
  159. public void Add(KeyValuePair<TKey, string> item) {
  160. this.EnsureNotReadOnly();
  161. this.ValuesLabels.Add(item.Key, item.Value);
  162. }
  163. public void Clear() {
  164. this.EnsureNotReadOnly();
  165. this.ValuesLabels.Clear();
  166. }
  167. public bool Contains(KeyValuePair<TKey, string> item) {
  168. return this.ContainsKey(item.Key);
  169. }
  170. public void CopyTo(KeyValuePair<TKey, string>[] array, int arrayIndex) {
  171. throw new NotImplementedException();
  172. }
  173. public bool Remove(KeyValuePair<TKey, string> item) {
  174. return this.Remove(item.Key);
  175. }
  176. #endregion
  177. #region IEnumerable Members
  178. IEnumerator IEnumerable.GetEnumerator() {
  179. return this.GetEnumerator();
  180. }
  181. #endregion
  182. }
  183. }