Free cookie consent management tool by TermsFeed Policy Generator

source: branches/WebJobManager/HeuristicLab.Data/3.3/IntArray.cs @ 17709

Last change on this file since 17709 was 13834, checked in by jlodewyc, 9 years ago

#2582 Parameterizing arrays start

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