Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Data/3.3/StringArray.cs @ 3146

Last change on this file since 3146 was 3054, checked in by swagner, 15 years ago

Refactored classes of HeuristicLab.Data and implemented encoding specific data classes (#909)

File size: 4.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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;
23using System.Collections;
24using System.Text;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace HeuristicLab.Data {
30  [Item("StringArray", "Represents an array of strings.")]
31  [Creatable("Test")]
32  [StorableClass]
33  public class StringArray : Item, IEnumerable, IStringConvertibleArray {
34    [Storable]
35    protected string[] array;
36
37    public virtual int Length {
38      get { return array.Length; }
39      protected set {
40        if (value != Length) {
41          Array.Resize<string>(ref array, value);
42          OnReset();
43        }
44      }
45    }
46    public virtual string this[int index] {
47      get { return array[index]; }
48      set {
49        if (value != array[index]) {
50          if ((value != null) || (array[index] != string.Empty)) {
51            array[index] = value != null ? value : string.Empty;
52            OnItemChanged(index);
53          }
54        }
55      }
56    }
57
58    public StringArray() {
59      array = new string[0];
60    }
61    public StringArray(int length) {
62      array = new string[length];
63      for (int i = 0; i < array.Length; i++)
64        array[i] = string.Empty;
65    }
66    public StringArray(string[] elements) {
67      if (elements == null) throw new ArgumentNullException();
68      array = new string[elements.Length];
69      for (int i = 0; i < array.Length; i++)
70        array[i] = elements[i] == null ? string.Empty : elements[i];
71    }
72
73    public override IDeepCloneable Clone(Cloner cloner) {
74      StringArray clone = new StringArray();
75      cloner.RegisterClonedObject(this, clone);
76      clone.array = (string[])array.Clone();
77      return clone;
78    }
79
80    public override string ToString() {
81      StringBuilder sb = new StringBuilder();
82      sb.Append("[");
83      if (array.Length > 0) {
84        sb.Append(array[0]);
85        for (int i = 1; i < array.Length; i++)
86          sb.Append(";").Append(array[i]);
87      }
88      sb.Append("]");
89      return sb.ToString();
90    }
91
92    public virtual IEnumerator GetEnumerator() {
93      return array.GetEnumerator();
94    }
95
96    protected virtual bool Validate(string value, out string errorMessage) {
97      if (value == null) {
98        errorMessage = "Invalid Value (string must not be null)";
99        return false;
100      } else {
101        errorMessage = string.Empty;
102        return true;
103      }
104    }
105    protected virtual string GetValue(int index) {
106      return this[index];
107    }
108    protected virtual bool SetValue(string value, int index) {
109      if (value != null) {
110        this[index] = value;
111        return true;
112      } else {
113        return false;
114      }
115    }
116
117    public event EventHandler<EventArgs<int>> ItemChanged;
118    protected virtual void OnItemChanged(int index) {
119      if (ItemChanged != null)
120        ItemChanged(this, new EventArgs<int>(index));
121      OnToStringChanged();
122    }
123    public event EventHandler Reset;
124    protected virtual void OnReset() {
125      if (Reset != null)
126        Reset(this, EventArgs.Empty);
127      OnToStringChanged();
128    }
129
130    #region IStringConvertibleArray Members
131    int IStringConvertibleArray.Length {
132      get { return Length; }
133      set { Length = value; }
134    }
135    bool IStringConvertibleArray.Validate(string value, out string errorMessage) {
136      return Validate(value, out errorMessage);
137    }
138    string IStringConvertibleArray.GetValue(int index) {
139      return GetValue(index);
140    }
141    bool IStringConvertibleArray.SetValue(string value, int index) {
142      return SetValue(value, index);
143    }
144    #endregion
145  }
146}
Note: See TracBrowser for help on using the repository browser.