Free cookie consent management tool by TermsFeed Policy Generator

source: branches/WebJobManager/HeuristicLab.Data/3.3/BoolArray.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
RevLine 
[2676]1#region License Information
2/* HeuristicLab
[12012]3 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[2676]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;
[3376]23using HeuristicLab.Common;
[2676]24using HeuristicLab.Core;
25using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
[13841]26using System.Linq;
[2676]27
28namespace HeuristicLab.Data {
[3048]29  [Item("BoolArray", "Represents an array of boolean values.")]
[3017]30  [StorableClass]
[3054]31  public class BoolArray : ValueTypeArray<bool>, IStringConvertibleArray {
[4722]32    [StorableConstructor]
33    protected BoolArray(bool deserializing) : base(deserializing) { }
34    protected BoolArray(BoolArray original, Cloner cloner)
35      : base(original, cloner) {
36    }
[3048]37    public BoolArray() : base() { }
38    public BoolArray(int length) : base(length) { }
39    public BoolArray(bool[] elements) : base(elements) { }
[2676]40
41    public override IDeepCloneable Clone(Cloner cloner) {
[4722]42      return new BoolArray(this, cloner);
[2676]43    }
[13841]44        public static BoolArray Parse(string val)
45        {
46            try
47            {
48                string[] arr = System.Array.ConvertAll(val.Trim().Split(';'), p => p.Trim());
49                bool[] a = arr.Where(s => s != "").ToList().ConvertAll(s => bool.Parse(s)).ToArray();
50                return new BoolArray(a);
51            }
52            catch (System.FormatException e)
53            {
54                return null;
55            }
56        }
57        protected virtual bool Validate(string value, out string errorMessage) {
[2694]58      bool val;
59      bool valid = bool.TryParse(value, out val);
60      errorMessage = string.Empty;
61      if (!valid) {
62        StringBuilder sb = new StringBuilder();
63        sb.Append("Invalid Value (Valid Value Format: \"");
64        sb.Append(FormatPatterns.GetBoolFormatPattern());
65        sb.Append("\")");
66        errorMessage = sb.ToString();
67      }
68      return valid;
[2676]69    }
[3054]70    protected virtual string GetValue(int index) {
[2973]71      return this[index].ToString();
[2677]72    }
[3054]73    protected virtual bool SetValue(string value, int index) {
[2694]74      bool val;
75      if (bool.TryParse(value, out val)) {
[2973]76        this[index] = val;
[2676]77        return true;
78      } else {
79        return false;
80      }
81    }
[3054]82
83    #region IStringConvertibleArray Members
84    int IStringConvertibleArray.Length {
85      get { return Length; }
86      set { Length = value; }
87    }
88    bool IStringConvertibleArray.Validate(string value, out string errorMessage) {
89      return Validate(value, out errorMessage);
90    }
91    string IStringConvertibleArray.GetValue(int index) {
92      return GetValue(index);
93    }
94    bool IStringConvertibleArray.SetValue(string value, int index) {
95      return SetValue(value, index);
96    }
[2676]97    #endregion
98  }
99}
Note: See TracBrowser for help on using the repository browser.