Changeset 10448


Ignore:
Timestamp:
02/06/14 16:36:59 (5 years ago)
Author:
mkommend
Message:

#2135: Merged branch changes to PercentValue into the trunk.

Location:
trunk/sources/HeuristicLab.Data/3.3
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Data/3.3

  • trunk/sources/HeuristicLab.Data/3.3/PercentValue.cs

    r9456 r10448  
    2020#endregion
    2121
     22using System;
    2223using HeuristicLab.Common;
    2324using HeuristicLab.Core;
     
    2829  [StorableClass]
    2930  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
    3046    [StorableConstructor]
    3147    protected PercentValue(bool deserializing) : base(deserializing) { }
    3248    protected PercentValue(PercentValue original, Cloner cloner)
    3349      : base(original, cloner) {
     50      restrictToUnitInterval = original.restrictToUnitInterval;
    3451    }
    3552    public PercentValue() : base() { }
    3653    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    }
    3762
    3863    public override IDeepCloneable Clone(Cloner cloner) {
     
    4671    protected override bool Validate(string value, out string errorMessage) {
    4772      value = value.Replace("%", " ");
    48       return base.Validate(value, out errorMessage);
     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;
    4982    }
     83
    5084    protected override string GetValue() {
    5185      return Value.ToString("#0.#################### %");  // percent format
Note: See TracChangeset for help on using the changeset viewer.