PageRenderTime 44ms CodeModel.GetById 19ms app.highlight 18ms RepoModel.GetById 2ms app.codeStats 0ms

/Spss/SpssVariableValueLabels.cs

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