[2] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[12012] | 3 | * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[2] | 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 |
|
---|
| 22 | using System;
|
---|
[3306] | 23 | using System.Drawing;
|
---|
[2] | 24 | using System.Text;
|
---|
[3376] | 25 | using HeuristicLab.Common;
|
---|
[2] | 26 | using HeuristicLab.Core;
|
---|
[1853] | 27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[2] | 28 |
|
---|
| 29 | namespace HeuristicLab.Data {
|
---|
[3048] | 30 | [Item("DoubleValue", "Represents a double value.")]
|
---|
[3017] | 31 | [StorableClass]
|
---|
[3054] | 32 | public class DoubleValue : ValueTypeValue<double>, IComparable, IStringConvertibleValue {
|
---|
[13656] | 33 | public static new Image StaticItemImage
|
---|
| 34 | {
|
---|
| 35 | get { return new Bitmap(25, 25); }
|
---|
[3306] | 36 | }
|
---|
| 37 |
|
---|
[4722] | 38 | [StorableConstructor]
|
---|
| 39 | protected DoubleValue(bool deserializing) : base(deserializing) { }
|
---|
| 40 | protected DoubleValue(DoubleValue original, Cloner cloner)
|
---|
| 41 | : base(original, cloner) {
|
---|
| 42 | }
|
---|
[3048] | 43 | public DoubleValue() : base() { }
|
---|
| 44 | public DoubleValue(double value) : base(value) { }
|
---|
[2] | 45 |
|
---|
[13827] | 46 | public static DoubleValue Parse(string val)
|
---|
| 47 | {
|
---|
| 48 | return new DoubleValue(Double.Parse(val));
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
[4722] | 52 | return new DoubleValue(this, cloner);
|
---|
[2] | 53 | }
|
---|
[2665] | 54 |
|
---|
[2669] | 55 | public override string ToString() {
|
---|
[12432] | 56 | return Value.ToString("r"); // round-trip format
|
---|
[2669] | 57 | }
|
---|
| 58 |
|
---|
[3054] | 59 | public virtual int CompareTo(object obj) {
|
---|
[3048] | 60 | DoubleValue other = obj as DoubleValue;
|
---|
[2757] | 61 | if (other != null)
|
---|
| 62 | return Value.CompareTo(other.Value);
|
---|
| 63 | else
|
---|
| 64 | return Value.CompareTo(obj);
|
---|
| 65 | }
|
---|
| 66 |
|
---|
[3054] | 67 | protected virtual bool Validate(string value, out string errorMessage) {
|
---|
[2694] | 68 | double val;
|
---|
| 69 | bool valid = double.TryParse(value, out val);
|
---|
| 70 | errorMessage = string.Empty;
|
---|
| 71 | if (!valid) {
|
---|
| 72 | StringBuilder sb = new StringBuilder();
|
---|
| 73 | sb.Append("Invalid Value (Valid Value Format: \"");
|
---|
| 74 | sb.Append(FormatPatterns.GetDoubleFormatPattern());
|
---|
| 75 | sb.Append("\")");
|
---|
| 76 | errorMessage = sb.ToString();
|
---|
| 77 | }
|
---|
| 78 | return valid;
|
---|
[2676] | 79 | }
|
---|
[3054] | 80 | protected virtual string GetValue() {
|
---|
[12433] | 81 | return Value.ToString("r"); // round-trip format
|
---|
[2665] | 82 | }
|
---|
[3054] | 83 | protected virtual bool SetValue(string value) {
|
---|
[2694] | 84 | double val;
|
---|
| 85 | if (double.TryParse(value, out val)) {
|
---|
| 86 | Value = val;
|
---|
[2665] | 87 | return true;
|
---|
| 88 | } else {
|
---|
| 89 | return false;
|
---|
| 90 | }
|
---|
| 91 | }
|
---|
[3054] | 92 |
|
---|
| 93 | #region IStringConvertibleValue Members
|
---|
| 94 | bool IStringConvertibleValue.Validate(string value, out string errorMessage) {
|
---|
| 95 | return Validate(value, out errorMessage);
|
---|
| 96 | }
|
---|
| 97 | string IStringConvertibleValue.GetValue() {
|
---|
| 98 | return GetValue();
|
---|
| 99 | }
|
---|
| 100 | bool IStringConvertibleValue.SetValue(string value) {
|
---|
| 101 | return SetValue(value);
|
---|
| 102 | }
|
---|
[2676] | 103 | #endregion
|
---|
[2] | 104 | }
|
---|
| 105 | }
|
---|