Free cookie consent management tool by TermsFeed Policy Generator

source: branches/WebJobManager/HeuristicLab.Data/3.3/PercentArray.cs @ 17399

Last change on this file since 17399 was 13841, checked in by jlodewyc, 9 years ago

#2582 More parameter datatypes, splitting fileopening service, approving users, reopen last file, change name tasks and repetitions

File size: 3.4 KB
Line 
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
22using System.Text;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
26using System.Linq;
27
28namespace HeuristicLab.Data {
29  [Item("PercentArray", "Represents an array of double values in percent.")]
30  [StorableClass]
31  public class PercentArray : DoubleArray {
32    [StorableConstructor]
33    protected PercentArray(bool deserializing) : base(deserializing) { }
34    protected PercentArray(PercentArray original, Cloner cloner)
35      : base(original, cloner) {
36    }
37    public PercentArray() : base() { }
38    public PercentArray(int length) : base(length) { }
39    public PercentArray(double[] elements) : base(elements) { }
40
41    public override IDeepCloneable Clone(Cloner cloner) {
42      return new PercentArray(this, cloner);
43    }
44       
45        public static PercentArray Parse(string val)
46        {
47            try
48            {
49                string[] arr = System.Array.ConvertAll(val.Trim().Split(';'), p => p.Trim());
50                double[] a = arr.Where(s => s != "").ToList().ConvertAll(s => (double.Parse(s)/100)).ToArray();
51                return new PercentArray(a);
52            }
53            catch (System.FormatException e)
54            {
55                return null;
56            }
57        }
58        public override string ToString() {
59      StringBuilder sb = new StringBuilder();
60      sb.Append("[");
61      if (array.Length > 0) {
62        sb.Append(array[0].ToString("#0.#################### %"));  // percent format
63        for (int i = 1; i < array.Length; i++)
64          sb.Append(";").Append(array[i].ToString("#0.#################### %"));  // percent format
65      }
66      sb.Append("]");
67      return sb.ToString();
68    }
69
70    protected override bool Validate(string value, out string errorMessage) {
71      value = value.Replace("%", " ");
72      return base.Validate(value, out errorMessage);
73    }
74    protected override string GetValue(int index) {
75      return this[index].ToString("#0.#################### %");  // percent format
76    }
77    protected override bool SetValue(string value, int index) {
78      bool percent = value.Contains("%");
79      value = value.Replace("%", " ");
80      double val;
81      if (double.TryParse(value, out val)) {
82        if (percent) {
83          if (!(val).IsAlmost(this[index] * 100.0))
84            this[index] = val == 0 ? 0 : val / 100.0;
85        } else {
86          this[index] = val;
87        }
88        return true;
89      } else {
90        return false;
91      }
92    }
93  }
94}
Note: See TracBrowser for help on using the repository browser.