Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Data/3.3/PercentValue.cs

Last change on this file was 17253, checked in by abeham, 5 years ago

#2521: worked on refactoring PTSP

File size: 3.7 KB
Line 
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
22using System;
23using HEAL.Attic;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26
27namespace 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, bool @readonly = false) : base(value, @readonly) { }
54    // TODO: suggest to remove the following constructor [ABE]
55    public PercentValue(double value, bool restrictToUnitInterval, bool @readonly = false)
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      this.readOnly = @readonly;
62    }
63
64    public override IDeepCloneable Clone(Cloner cloner) {
65      return new PercentValue(this, cloner);
66    }
67
68    public override string ToString() {
69      return Value.ToString("#0.#################### %");  // percent format
70    }
71
72    protected override bool Validate(string value, out string errorMessage) {
73      value = value.Replace("%", " ");
74      bool valid = base.Validate(value, out errorMessage);
75      if (!restrictToUnitInterval || !valid) return valid;
76
77      double val = double.Parse(value);
78      if (val < 0 || val > 1) {
79        errorMessage = "Value must lie in the interval [0,1].";
80        return false;
81      }
82      return true;
83    }
84
85    protected override string GetValue() {
86      return Value.ToString("#0.#################### %");  // percent format
87    }
88    protected override bool SetValue(string value) {
89      bool percent = value.Contains("%");
90      value = value.Replace("%", " ");
91      double val;
92      if (double.TryParse(value, out val)) {
93        if (percent) {
94          if (!(val).IsAlmost(Value * 100.0))
95            Value = val == 0 ? 0 : val / 100.0;
96        } else {
97          Value = val;
98        }
99        return true;
100      } else {
101        return false;
102      }
103    }
104
105    public new PercentValue AsReadOnly() {
106      return (PercentValue)base.AsReadOnly();
107    }
108  }
109}
Note: See TracBrowser for help on using the repository browser.