Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Data/3.3/DoubleValue.cs @ 3317

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

Implemented ReadOnlyView property for items (#969).

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