Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Data/3.3/PercentValue.cs @ 11813

Last change on this file since 11813 was 11171, checked in by ascheibe, 10 years ago

#2115 merged r11170 (copyright update) into trunk

File size: 3.5 KB
RevLine 
[3095]1#region License Information
2/* HeuristicLab
[11171]3 * Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[3095]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
[10448]22using System;
[3402]23using HeuristicLab.Common;
[3095]24using HeuristicLab.Core;
25using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
26
27namespace HeuristicLab.Data {
28  [Item("PercentValue", "Represents a double value in percent.")]
29  [StorableClass]
30  public class PercentValue : DoubleValue {
[10448]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
[4722]46    [StorableConstructor]
47    protected PercentValue(bool deserializing) : base(deserializing) { }
48    protected PercentValue(PercentValue original, Cloner cloner)
49      : base(original, cloner) {
[10448]50      restrictToUnitInterval = original.restrictToUnitInterval;
[4722]51    }
[3095]52    public PercentValue() : base() { }
53    public PercentValue(double value) : base(value) { }
54
[10448]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
[3095]63    public override IDeepCloneable Clone(Cloner cloner) {
[4722]64      return new PercentValue(this, cloner);
[3095]65    }
66
67    public override string ToString() {
[3402]68      return Value.ToString("#0.#################### %");  // percent format
[3095]69    }
70
71    protected override bool Validate(string value, out string errorMessage) {
[3402]72      value = value.Replace("%", " ");
[10448]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;
[3095]82    }
[10448]83
[3095]84    protected override string GetValue() {
[3402]85      return Value.ToString("#0.#################### %");  // percent format
[3095]86    }
87    protected override bool SetValue(string value) {
[3402]88      bool percent = value.Contains("%");
89      value = value.Replace("%", " ");
[3095]90      double val;
91      if (double.TryParse(value, out val)) {
[3402]92        if (percent) {
93          if (!(val).IsAlmost(Value * 100.0))
94            Value = val == 0 ? 0 : val / 100.0;
95        } else {
96          Value = val;
97        }
[3095]98        return true;
99      } else {
100        return false;
101      }
102    }
103  }
104}
Note: See TracBrowser for help on using the repository browser.