Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Data/3.3/DoubleValue.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: 3.2 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.Drawing;
24using System.Text;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace HeuristicLab.Data {
30  [Item("DoubleValue", "Represents a double value.")]
31  [StorableClass]
32  public class DoubleValue : ValueTypeValue<double>, IComparable, IStringConvertibleValue {
33    public override Image ItemImage {
34      get { return HeuristicLab.Common.Resources.VS2008ImageLibrary.Field; }
35    }
36
37    public DoubleValue() : base() { }
38    public DoubleValue(double value) : base(value) { }
39
40    public override IDeepCloneable Clone(Cloner cloner) {
41      DoubleValue clone = new DoubleValue(value);
42      cloner.RegisterClonedObject(this, clone);
43      clone.ReadOnlyView = ReadOnlyView;
44      clone.readOnly = readOnly;
45      return clone;
46    }
47
48    public override string ToString() {
49      return Value.ToString("r");  // round-trip format
50    }
51
52    public virtual int CompareTo(object obj) {
53      DoubleValue other = obj as DoubleValue;
54      if (other != null)
55        return Value.CompareTo(other.Value);
56      else
57        return Value.CompareTo(obj);
58    }
59
60    protected virtual bool Validate(string value, out string errorMessage) {
61      double val;
62      bool valid = double.TryParse(value, out val);
63      errorMessage = string.Empty;
64      if (!valid) {
65        StringBuilder sb = new StringBuilder();
66        sb.Append("Invalid Value (Valid Value Format: \"");
67        sb.Append(FormatPatterns.GetDoubleFormatPattern());
68        sb.Append("\")");
69        errorMessage = sb.ToString();
70      }
71      return valid;
72    }
73    protected virtual string GetValue() {
74      return Value.ToString("r");  // round-trip format
75    }
76    protected virtual bool SetValue(string value) {
77      double val;
78      if (double.TryParse(value, out val)) {
79        Value = val;
80        return true;
81      } else {
82        return false;
83      }
84    }
85
86    #region IStringConvertibleValue Members
87    bool IStringConvertibleValue.Validate(string value, out string errorMessage) {
88      return Validate(value, out errorMessage);
89    }
90    string IStringConvertibleValue.GetValue() {
91      return GetValue();
92    }
93    bool IStringConvertibleValue.SetValue(string value) {
94      return SetValue(value);
95    }
96    #endregion
97  }
98}
Note: See TracBrowser for help on using the repository browser.