Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Data/3.3/TimeSpanValue.cs @ 3430

Last change on this file since 3430 was 3430, checked in by swagner, 14 years ago

Added ReadOnly property to all items of HeuristicLab.Data (#969)

File size: 2.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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.Text;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27
28namespace HeuristicLab.Data {
29  [Item("TimeSpanValue", "Represents a duration of time.")]
30  [StorableClass]
31  public class TimeSpanValue : ValueTypeValue<TimeSpan>, IComparable, IStringConvertibleValue {
32    public TimeSpanValue() : base() { }
33    public TimeSpanValue(TimeSpan value) : base(value) { }
34
35    public override IDeepCloneable Clone(Cloner cloner) {
36      TimeSpanValue clone = new TimeSpanValue(value);
37      cloner.RegisterClonedObject(this, clone);
38      clone.ReadOnlyView = ReadOnlyView;
39      clone.readOnly = readOnly;
40      return clone;
41    }
42
43    public virtual int CompareTo(object obj) {
44      TimeSpanValue other = obj as TimeSpanValue;
45      if (other != null)
46        return Value.CompareTo(other.Value);
47      else
48        return Value.CompareTo(obj);
49    }
50
51    protected virtual bool Validate(string value, out string errorMessage) {
52      TimeSpan val;
53      bool valid = TimeSpan.TryParse(value, out val);
54      errorMessage = string.Empty;
55      if (!valid) {
56        StringBuilder sb = new StringBuilder();
57        sb.Append("Invalid Value (Valid Value Format: \"");
58        sb.Append(FormatPatterns.GetTimeSpanFormatPattern());
59        sb.Append("\")");
60        errorMessage = sb.ToString();
61      }
62      return valid;
63    }
64    protected virtual string GetValue() {
65      return Value.ToString();
66    }
67    protected virtual bool SetValue(string value) {
68      TimeSpan val;
69      if (TimeSpan.TryParse(value, out val)) {
70        Value = val;
71        return true;
72      } else {
73        return false;
74      }
75    }
76
77    #region IStringConvertibleValue Members
78    bool IStringConvertibleValue.Validate(string value, out string errorMessage) {
79      return Validate(value, out errorMessage);
80    }
81    string IStringConvertibleValue.GetValue() {
82      return GetValue();
83    }
84    bool IStringConvertibleValue.SetValue(string value) {
85      return SetValue(value);
86    }
87    #endregion
88  }
89}
Note: See TracBrowser for help on using the repository browser.