1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 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 |
|
---|
22 | using System;
|
---|
23 | using HeuristicLab.Common;
|
---|
24 | using HeuristicLab.Core;
|
---|
25 | using HEAL.Attic;
|
---|
26 |
|
---|
27 | namespace HeuristicLab.Data {
|
---|
28 | [Item("PercentValue", "Represents a double value in percent.")]
|
---|
29 | [StorableType("FC4406A2-54A1-4D9D-AE6A-AB98895ECE64")]
|
---|
30 | public class PercentValue : DoubleValue {
|
---|
31 | [Storable(DefaultValue = false)]
|
---|
32 | private bool restrictToUnitInterval = false;
|
---|
33 | public bool RestrictToUnitInterval {
|
---|
34 | get { return restrictToUnitInterval; }
|
---|
35 | }
|
---|
36 |
|
---|
37 | public override double Value {
|
---|
38 | get { return base.Value; }
|
---|
39 | set {
|
---|
40 | if (restrictToUnitInterval && (value < 0 || value > 1))
|
---|
41 | throw new ArgumentException("Value must lie in the interval [0,1].");
|
---|
42 | base.Value = value;
|
---|
43 | }
|
---|
44 | }
|
---|
45 |
|
---|
46 | [StorableConstructor]
|
---|
47 | protected PercentValue(StorableConstructorFlag _) : base(_) { }
|
---|
48 | protected PercentValue(PercentValue original, Cloner cloner)
|
---|
49 | : base(original, cloner) {
|
---|
50 | restrictToUnitInterval = original.restrictToUnitInterval;
|
---|
51 | }
|
---|
52 | public PercentValue() : base() { }
|
---|
53 | public PercentValue(double value) : base(value) { }
|
---|
54 |
|
---|
55 | public PercentValue(double value, bool restrictToUnitInterval)
|
---|
56 | : base() {
|
---|
57 | this.restrictToUnitInterval = restrictToUnitInterval;
|
---|
58 | if (restrictToUnitInterval && (value < 0 || value > 1))
|
---|
59 | throw new ArgumentException("Value must lie in the interval [0,1].");
|
---|
60 | this.value = value;
|
---|
61 | }
|
---|
62 |
|
---|
63 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
64 | return new PercentValue(this, cloner);
|
---|
65 | }
|
---|
66 |
|
---|
67 | public override string ToString() {
|
---|
68 | return Value.ToString("#0.#################### %"); // percent format
|
---|
69 | }
|
---|
70 |
|
---|
71 | protected override bool Validate(string value, out string errorMessage) {
|
---|
72 | value = value.Replace("%", " ");
|
---|
73 | bool valid = base.Validate(value, out errorMessage);
|
---|
74 | if (!restrictToUnitInterval || !valid) return valid;
|
---|
75 |
|
---|
76 | double val = double.Parse(value);
|
---|
77 | if (val < 0 || val > 1) {
|
---|
78 | errorMessage = "Value must lie in the interval [0,1].";
|
---|
79 | return false;
|
---|
80 | }
|
---|
81 | return true;
|
---|
82 | }
|
---|
83 |
|
---|
84 | protected override string GetValue() {
|
---|
85 | return Value.ToString("#0.#################### %"); // percent format
|
---|
86 | }
|
---|
87 | protected override bool SetValue(string value) {
|
---|
88 | bool percent = value.Contains("%");
|
---|
89 | value = value.Replace("%", " ");
|
---|
90 | double val;
|
---|
91 | if (double.TryParse(value, out val)) {
|
---|
92 | if (percent) {
|
---|
93 | if (!(val).IsAlmost(Value * 100.0))
|
---|
94 | Value = val == 0 ? 0 : val / 100.0;
|
---|
95 | } else {
|
---|
96 | Value = val;
|
---|
97 | }
|
---|
98 | return true;
|
---|
99 | } else {
|
---|
100 | return false;
|
---|
101 | }
|
---|
102 | }
|
---|
103 | }
|
---|
104 | }
|
---|