[3095] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[12012] | 3 | * Copyright (C) 2002-2015 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] | 22 | using System;
|
---|
[3402] | 23 | using HeuristicLab.Common;
|
---|
[3095] | 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 {
|
---|
[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 |
|
---|
[13827] | 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)
|
---|
[10448] | 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 |
|
---|
[3095] | 68 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
[4722] | 69 | return new PercentValue(this, cloner);
|
---|
[3095] | 70 | }
|
---|
| 71 |
|
---|
| 72 | public override string ToString() {
|
---|
[3402] | 73 | return Value.ToString("#0.#################### %"); // percent format
|
---|
[3095] | 74 | }
|
---|
| 75 |
|
---|
| 76 | protected override bool Validate(string value, out string errorMessage) {
|
---|
[3402] | 77 | value = value.Replace("%", " ");
|
---|
[10448] | 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;
|
---|
[3095] | 87 | }
|
---|
[10448] | 88 |
|
---|
[3095] | 89 | protected override string GetValue() {
|
---|
[3402] | 90 | return Value.ToString("#0.#################### %"); // percent format
|
---|
[3095] | 91 | }
|
---|
| 92 | protected override bool SetValue(string value) {
|
---|
[3402] | 93 | bool percent = value.Contains("%");
|
---|
| 94 | value = value.Replace("%", " ");
|
---|
[3095] | 95 | double val;
|
---|
| 96 | if (double.TryParse(value, out val)) {
|
---|
[3402] | 97 | if (percent) {
|
---|
| 98 | if (!(val).IsAlmost(Value * 100.0))
|
---|
| 99 | Value = val == 0 ? 0 : val / 100.0;
|
---|
| 100 | } else {
|
---|
| 101 | Value = val;
|
---|
| 102 | }
|
---|
[3095] | 103 | return true;
|
---|
| 104 | } else {
|
---|
| 105 | return false;
|
---|
| 106 | }
|
---|
| 107 | }
|
---|
| 108 | }
|
---|
| 109 | }
|
---|