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 |
|
---|
22 | using System;
|
---|
23 | using HeuristicLab.Common;
|
---|
24 | using HeuristicLab.Core;
|
---|
25 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
26 |
|
---|
27 | namespace HeuristicLab.Data {
|
---|
28 | [Item("PercentValue", "Represents a double value in percent.")]
|
---|
29 | [StorableClass]
|
---|
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(bool deserializing) : base(deserializing) { }
|
---|
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 static PercentValue Parse(string val)
|
---|
56 | {
|
---|
57 | return new PercentValue((Double.Parse(val) / 100));
|
---|
58 | }
|
---|
59 |
|
---|
60 | public PercentValue(double value, bool restrictToUnitInterval)
|
---|
61 | : base() {
|
---|
62 | this.restrictToUnitInterval = restrictToUnitInterval;
|
---|
63 | if (restrictToUnitInterval && (value < 0 || value > 1))
|
---|
64 | throw new ArgumentException("Value must lie in the interval [0,1].");
|
---|
65 | this.value = value;
|
---|
66 | }
|
---|
67 |
|
---|
68 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
69 | return new PercentValue(this, cloner);
|
---|
70 | }
|
---|
71 |
|
---|
72 | public override string ToString() {
|
---|
73 | return Value.ToString("#0.#################### %"); // percent format
|
---|
74 | }
|
---|
75 |
|
---|
76 | protected override bool Validate(string value, out string errorMessage) {
|
---|
77 | value = value.Replace("%", " ");
|
---|
78 | bool valid = base.Validate(value, out errorMessage);
|
---|
79 | if (!restrictToUnitInterval || !valid) return valid;
|
---|
80 |
|
---|
81 | double val = double.Parse(value);
|
---|
82 | if (val < 0 || val > 1) {
|
---|
83 | errorMessage = "Value must lie in the interval [0,1].";
|
---|
84 | return false;
|
---|
85 | }
|
---|
86 | return true;
|
---|
87 | }
|
---|
88 |
|
---|
89 | protected override string GetValue() {
|
---|
90 | return Value.ToString("#0.#################### %"); // percent format
|
---|
91 | }
|
---|
92 | protected override bool SetValue(string value) {
|
---|
93 | bool percent = value.Contains("%");
|
---|
94 | value = value.Replace("%", " ");
|
---|
95 | double val;
|
---|
96 | if (double.TryParse(value, out val)) {
|
---|
97 | if (percent) {
|
---|
98 | if (!(val).IsAlmost(Value * 100.0))
|
---|
99 | Value = val == 0 ? 0 : val / 100.0;
|
---|
100 | } else {
|
---|
101 | Value = val;
|
---|
102 | }
|
---|
103 | return true;
|
---|
104 | } else {
|
---|
105 | return false;
|
---|
106 | }
|
---|
107 | }
|
---|
108 | }
|
---|
109 | }
|
---|