Free cookie consent management tool by TermsFeed Policy Generator

source: addons/HeuristicLab.DataImporter/HeuristicLab.DataImporter.Data/Model/DateTimeColumn.cs @ 16567

Last change on this file since 16567 was 16567, checked in by gkronber, 5 years ago

#2520: changed StorableConstructors and added StorableType attributes in HeuristicLab.DataImporter addon

File size: 3.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Globalization;
25using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
26using HEAL.Attic;
27
28namespace HeuristicLab.DataImporter.Data.Model {
29  [StorableType("7C0A4BD8-45A1-4710-AC42-1BCBB4DF03D5")]
30  public class DateTimeColumn : ColumnBase {
31    [Storable]
32    private List<DateTime?> values;
33
34    [StorableConstructor]
35    protected DateTimeColumn(StorableConstructorFlag _) : base(_) { }
36
37    public DateTimeColumn(string columnName)
38      : base(columnName) {
39      this.values = new List<DateTime?>();
40    }
41
42    public DateTimeColumn(string columnName, int capacity)
43      : this(columnName) {
44      this.values.Capacity = capacity;
45    }
46
47    public override Type DataType {
48      get { return typeof(DateTime?); }
49    }
50
51    public override string ToString() {
52      return base.ToString() + " <Date>";
53    }
54
55    protected override System.Collections.IList Values {
56      get { return this.values; }
57    }
58
59    public override void AddValue(IComparable value) {
60      this.values.Add((DateTime?)value);
61    }
62
63    public override void AddValueOrNull(IComparable value) {
64      this.values.Add(ConvertToDateTime(value));
65    }
66
67    public override void ChangeValue(int position, IComparable value) {
68      this.values[position] = (DateTime?)value;
69    }
70
71    public override void ChangeValueOrNull(int position, IComparable value) {
72      this.values[position] = ConvertToDateTime(value);
73    }
74
75    public override void ChangeValueOrLeaveOldValue(int position, IComparable value) {
76      if (value == null)
77        this.values[position] = null;
78      else {
79        DateTime? val = ConvertToDateTime(value);
80        if (val != null)
81          this.values[position] = val;
82      }
83    }
84
85    public override void InsertValue(int position, IComparable value) {
86      this.values.Insert(position, (DateTime?)value);
87    }
88
89    private DateTime? ConvertToDateTime(IComparable value) {
90      DateTime val;
91
92      if (value == null)
93        return null;
94
95      if (DateTime.TryParse(value.ToString(), out val))
96        return val;
97
98      if (DateTime.TryParseExact(value.ToString(), "yyyyMMddHHmmss", CultureInfo.InvariantCulture, DateTimeStyles.None, out val))
99        return val;
100
101      return null;
102    }
103
104    public override ColumnBase CreateCopyOfColumnWithoutValues() {
105      return CreateCopyOfColumnWithoutValues(this.values.Capacity);
106    }
107
108    public override ColumnBase CreateCopyOfColumnWithoutValues(int capacity) {
109      return new DateTimeColumn(this.Name, capacity);
110    }
111  }
112}
Note: See TracBrowser for help on using the repository browser.