PageRenderTime 42ms CodeModel.GetById 1ms RepoModel.GetById 1ms app.codeStats 0ms

/Spss/SpssNumericVariable.cs

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