Free cookie consent management tool by TermsFeed Policy Generator

source: branches/WebJobManager/HeuristicLab.Data/3.3/DoubleValue.cs @ 13660

Last change on this file since 13660 was 13656, checked in by ascheibe, 9 years ago

#2582 created branch for Hive Web Job Manager

File size: 3.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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 static new Image StaticItemImage
34    {
35      get { return new Bitmap(25, 25); }
36    }
37
38    [StorableConstructor]
39    protected DoubleValue(bool deserializing) : base(deserializing) { }
40    protected DoubleValue(DoubleValue original, Cloner cloner)
41      : base(original, cloner) {
42    }
43    public DoubleValue() : base() { }
44    public DoubleValue(double value) : base(value) { }
45
46    public override IDeepCloneable Clone(Cloner cloner) {
47      return new DoubleValue(this, cloner);
48    }
49
50    public override string ToString() {
51      return Value.ToString("r");  // round-trip format
52    }
53
54    public virtual int CompareTo(object obj) {
55      DoubleValue other = obj as DoubleValue;
56      if (other != null)
57        return Value.CompareTo(other.Value);
58      else
59        return Value.CompareTo(obj);
60    }
61
62    protected virtual bool Validate(string value, out string errorMessage) {
63      double val;
64      bool valid = double.TryParse(value, out val);
65      errorMessage = string.Empty;
66      if (!valid) {
67        StringBuilder sb = new StringBuilder();
68        sb.Append("Invalid Value (Valid Value Format: \"");
69        sb.Append(FormatPatterns.GetDoubleFormatPattern());
70        sb.Append("\")");
71        errorMessage = sb.ToString();
72      }
73      return valid;
74    }
75    protected virtual string GetValue() {
76      return Value.ToString("r");  // round-trip format
77    }
78    protected virtual bool SetValue(string value) {
79      double val;
80      if (double.TryParse(value, out val)) {
81        Value = val;
82        return true;
83      } else {
84        return false;
85      }
86    }
87
88    #region IStringConvertibleValue Members
89    bool IStringConvertibleValue.Validate(string value, out string errorMessage) {
90      return Validate(value, out errorMessage);
91    }
92    string IStringConvertibleValue.GetValue() {
93      return GetValue();
94    }
95    bool IStringConvertibleValue.SetValue(string value) {
96      return SetValue(value);
97    }
98    #endregion
99  }
100}
Note: See TracBrowser for help on using the repository browser.