Free cookie consent management tool by TermsFeed Policy Generator

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

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

Renamed files of HeuristicLab.Data (#909)

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