Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3160 was 3160, checked in by swagner, 14 years ago

Removed Creatable test attribute (#935).

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