PageRenderTime 26ms CodeModel.GetById 10ms app.highlight 12ms RepoModel.GetById 1ms app.codeStats 0ms

/Spss/SpssNumericVariable.cs

#
C# | 251 lines | 170 code | 35 blank | 46 comment | 13 complexity | 9d4f09eafdace6dc623b98657c3a5cd9 MD5 | raw file
  1namespace Spss {
  2	using System;
  3	using System.Diagnostics;
  4	using System.Collections.Generic;
  5	using System.Linq;
  6
  7	/// <summary>
  8	/// Represents an SPSS data variable that stores numeric information.
  9	/// </summary>
 10	/// <remarks>
 11	/// Both integer and floating point numbers are handled through this
 12	/// class.
 13	/// </remarks>
 14	public class SpssNumericVariable : SpssVariable {
 15		private const int DecimalPlacesDefault = 0;
 16		private readonly SpssNumericVariableValueLabelsDictionary valueLabels;
 17		private FormatTypeCode writeFormat;
 18		private FormatTypeCode printFormat;
 19		private int writeWidth;
 20		private int printWidth;
 21		private int writeDecimal;
 22		private int printDecimal;
 23
 24		/// <summary>
 25		/// Creates an instance of the <see cref="SpssNumericVariable"/> class,
 26		/// for use when defining a new variable.
 27		/// </summary>
 28		public SpssNumericVariable() {
 29			this.valueLabels = new SpssNumericVariableValueLabelsDictionary(this);
 30			this.WriteFormat = this.PrintFormat = FormatTypeCode.SPSS_FMT_F;
 31			this.WriteDecimal = this.PrintDecimal = DecimalPlacesDefault;
 32			this.WriteWidth = this.PrintWidth = ColumnWidthDefault;
 33			this.MissingValues = new List<double>(3);
 34		}
 35
 36		/// <summary>
 37		/// Creates an instance of the <see cref="SpssNumericVariable"/> class,
 38		/// for use in loading variables from an existing SPSS data file.
 39		/// </summary>
 40		/// <param name="variables">The containing collection.</param>
 41		/// <param name="varName">The name of the variable.</param>
 42		/// <param name="writeFormat">The write format.  The default is <see cref="FormatTypeCode.SPSS_FMT_F"/></param>
 43		/// <param name="writeDecimal">The write decimal.</param>
 44		/// <param name="writeWidth">Width of the write.</param>
 45		/// <param name="printFormat">The print format.  The default is <see cref="FormatTypeCode.SPSS_FMT_F"/></param>
 46		/// <param name="printDecimal">The print decimal.</param>
 47		/// <param name="printWidth">Width of the print.</param>
 48		protected internal SpssNumericVariable(SpssVariablesCollection variables, string varName, FormatTypeCode writeFormat, int writeDecimal, int writeWidth, FormatTypeCode printFormat, int printDecimal, int printWidth)
 49			: base(variables, varName) {
 50			this.valueLabels = new SpssNumericVariableValueLabelsDictionary(this);
 51
 52			MissingValueFormatCode formatCode;
 53			double[] missingValues = new double[3];
 54			ReturnCode result = SpssException.ThrowOnFailure(SpssSafeWrapper.spssGetVarNMissingValues(this.FileHandle, this.Name, out formatCode, out missingValues[0], out missingValues[1], out missingValues[2]), "spssGetVarNMissingValues");
 55			this.MissingValueFormat = formatCode;
 56			this.MissingValues = new List<double>(missingValues.Take(Math.Abs((int)formatCode)));
 57			this.writeDecimal = writeDecimal;
 58			this.writeWidth = writeWidth;
 59			this.writeFormat = writeFormat;
 60			this.printDecimal = printDecimal;
 61			this.printWidth = printWidth;
 62			this.printFormat = printFormat;
 63		}
 64
 65		/// <summary>
 66		/// Gets or sets the missing values for this variable.
 67		/// </summary>
 68		/// <value>The missing values.</value>
 69		/// <remarks>
 70		/// A maximum of three maximum values may be supplied.
 71		/// </remarks>
 72		public IList<double> MissingValues { get; private set; }
 73
 74		public MissingValueFormatCode MissingValueFormat { get; set; }
 75
 76		/// <summary>
 77		/// Gets the SPSS type for the variable.
 78		/// </summary>
 79		public override int SpssType {
 80			get {
 81				return 0; // 0 = numeric to SPSS
 82			}
 83		}
 84
 85		public virtual FormatTypeCode WriteFormat {
 86			get {
 87				return this.writeFormat;
 88			}
 89
 90			set {
 91				if (!this.IsApplicableFormatTypeCode(value)) {
 92					throw new ArgumentOutOfRangeException("value", "This value does not apply to this type of SPSS variable.");
 93				}
 94
 95				this.writeFormat = value;
 96			}
 97		}
 98
 99		public virtual FormatTypeCode PrintFormat {
100			get {
101				return this.printFormat;
102			}
103
104			set {
105				if (!this.IsApplicableFormatTypeCode(value)) {
106					throw new ArgumentOutOfRangeException("value", "This value does not apply to this type of SPSS variable.");
107				}
108
109				this.printFormat = value;
110			}
111		}
112
113		public int WriteWidth {
114			get {
115				return this.writeWidth;
116			}
117
118			set {
119				if (value < 0) {
120					throw new ArgumentOutOfRangeException("value");
121				}
122
123				this.writeWidth = value;
124			}
125		}
126
127		public int PrintWidth {
128			get {
129				return this.printWidth;
130			}
131
132			set {
133				if (value < 0) {
134					throw new ArgumentOutOfRangeException("value");
135				}
136
137				this.printWidth = value;
138			}
139		}
140
141		public int WriteDecimal {
142			get {
143				return this.writeDecimal;
144			}
145
146			set {
147				if (value < 0) {
148					throw new ArgumentOutOfRangeException("value");
149				}
150
151				this.writeDecimal = value;
152			}
153		}
154
155		public int PrintDecimal {
156			get {
157				return this.printDecimal;
158			}
159
160			set {
161				if (value < 0) {
162					throw new ArgumentOutOfRangeException("value");
163				}
164
165				this.printDecimal = value;
166			}
167		}
168
169		/// <summary>
170		/// The set of value labels (response values and labels) that are defined.
171		/// </summary>
172		public IDictionary<double, string> ValueLabels {
173			get { return this.valueLabels; }
174		}
175
176		/// <summary>
177		/// Gets or sets the data value of this variable within a specific case.
178		/// </summary>
179		/// <remarks>
180		/// Null values are translated to and from 
181		/// <see cref="SpssDataDocument.SystemMissingValue"/> transparently.
182		/// </remarks>
183		internal new double? Value {
184			get {
185				double v;
186				SpssException.ThrowOnFailure(SpssSafeWrapper.spssGetValueNumericImpl(FileHandle, Handle, out v), "spssGetValueNumeric");
187				if (v == SpssDataDocument.SystemMissingValue)
188					return null;
189				return v;
190			}
191
192			set {
193				if (!value.HasValue) value = SpssDataDocument.SystemMissingValue;
194				SpssException.ThrowOnFailure(SpssSafeWrapper.spssSetValueNumeric(FileHandle, Handle, value.Value), "spssSetValueNumeric");
195			}
196		}
197
198		/// <summary>
199		/// Updates details of the variable.
200		/// </summary>
201		protected override void Update() {
202			base.Update();
203
204			if (!IsInCollection) {
205				return; // we'll get to do this later
206			}
207
208			this.valueLabels.Update();
209			double[] missingValues = new double[3];
210			this.MissingValues.Take(missingValues.Length).ToArray().CopyTo(missingValues, 0);
211			SpssException.ThrowOnFailure(SpssSafeWrapper.spssSetVarNMissingValues(
212				this.FileHandle,
213				this.Name,
214				this.MissingValueFormat,
215				missingValues[0],
216				missingValues[1],
217				missingValues[2]), "spssSetVarNMissingValues");
218
219			SpssException.ThrowOnFailure(SpssSafeWrapper.spssSetVarPrintFormat(FileHandle, Name, this.PrintFormat, this.PrintDecimal, this.PrintWidth), "spssSetVarPrintFormat");
220			SpssException.ThrowOnFailure(SpssSafeWrapper.spssSetVarWriteFormat(FileHandle, Name, this.WriteFormat, this.WriteDecimal, this.WriteWidth), "spssSetVarWriteFormat");
221		}
222
223		public override SpssVariable Clone() {
224			SpssNumericVariable other = new SpssNumericVariable();
225			CloneTo(other);
226			return other;
227		}
228
229		protected override void CloneTo(SpssVariable spssVar) {
230			base.CloneTo(spssVar);
231			SpssNumericVariable other = spssVar as SpssNumericVariable;
232			if (other == null) {
233				throw new ArgumentException("Must be of type " + GetType().Name + ".", "other");
234			}
235			other.PrintDecimal = this.PrintDecimal;
236			other.PrintFormat = this.PrintFormat;
237			other.PrintWidth = this.PrintWidth;
238			other.WriteDecimal = this.WriteDecimal;
239			other.WriteFormat = this.WriteFormat;
240			other.WriteWidth = this.WriteWidth;
241			other.MissingValueFormat = this.MissingValueFormat;
242			other.MissingValues = new List<double>(this.MissingValues);
243			this.valueLabels.CopyTo(other.valueLabels);
244		}
245
246		protected override bool IsApplicableFormatTypeCode(FormatTypeCode formatType) {
247			return formatType != FormatTypeCode.SPSS_FMT_A &&
248				!SpssDateVariable.IsDateVariable(formatType);
249		}
250	}
251}