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

Last change on this file since 9456 was 9456, checked in by swagner, 9 years ago

Updated copyright year and added some missing license headers (#1889)

File size: 5.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 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.Collections.Generic;
25using System.Drawing;
26using System.Linq;
27using System.Text;
28using HeuristicLab.Common;
29using HeuristicLab.Core;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31
32namespace HeuristicLab.Data {
33  [Item("StringArray", "Represents an array of strings.")]
34  [StorableClass]
35  public class StringArray : Item, IEnumerable<string>, IStringConvertibleArray {
36    private const int maximumToStringLength = 100;
37
38    public static new Image StaticItemImage {
39      get { return HeuristicLab.Common.Resources.VSImageLibrary.Class; }
40    }
41
42    [Storable]
43    protected string[] array;
44
45    public virtual int Length {
46      get { return array.Length; }
47      protected set {
48        if (ReadOnly) throw new NotSupportedException("Length cannot be set. StringArray is read-only.");
49        if (value != Length) {
50          Array.Resize<string>(ref array, value);
51          OnReset();
52        }
53      }
54    }
55    public virtual string this[int index] {
56      get { return array[index]; }
57      set {
58        if (ReadOnly) throw new NotSupportedException("Item cannot be set. StringArray is read-only.");
59        if (value != array[index]) {
60          if ((value != null) || (array[index] != string.Empty)) {
61            array[index] = value != null ? value : string.Empty;
62            OnItemChanged(index);
63          }
64        }
65      }
66    }
67
68    [Storable]
69    protected bool readOnly;
70    public virtual bool ReadOnly {
71      get { return readOnly; }
72    }
73
74    [StorableConstructor]
75    protected StringArray(bool deserializing) : base(deserializing) { }
76    protected StringArray(StringArray original, Cloner cloner)
77      : base(original, cloner) {
78      this.array = (string[])original.array.Clone();
79      this.readOnly = original.readOnly;
80    }
81    public StringArray() {
82      array = new string[0];
83      readOnly = false;
84    }
85    public StringArray(int length) {
86      array = new string[length];
87      for (int i = 0; i < array.Length; i++)
88        array[i] = string.Empty;
89      readOnly = false;
90    }
91    public StringArray(string[] elements) {
92      if (elements == null) throw new ArgumentNullException();
93      array = new string[elements.Length];
94      for (int i = 0; i < array.Length; i++)
95        array[i] = elements[i] == null ? string.Empty : elements[i];
96      readOnly = false;
97    }
98
99    public override IDeepCloneable Clone(Cloner cloner) {
100      return new StringArray(this, cloner);
101    }
102
103    public virtual StringArray AsReadOnly() {
104      StringArray readOnlyStringArray = (StringArray)this.Clone();
105      readOnlyStringArray.readOnly = true;
106      return readOnlyStringArray;
107    }
108
109    public override string ToString() {
110      if (array.Length == 0) return "[]";
111
112      StringBuilder sb = new StringBuilder();
113      sb.Append("[");
114      sb.Append(array[0]);
115      for (int i = 1; i < array.Length; i++) {
116        sb.Append(";").Append(array[i]);
117        if (sb.Length > maximumToStringLength) {
118          sb.Append("...");
119          break;
120        }
121      }
122      sb.Append("]");
123      return sb.ToString();
124    }
125
126    public virtual IEnumerator<string> GetEnumerator() {
127      return array.Cast<string>().GetEnumerator();
128    }
129
130    IEnumerator IEnumerable.GetEnumerator() {
131      return GetEnumerator();
132    }
133
134    protected virtual bool Validate(string value, out string errorMessage) {
135      if (value == null) {
136        errorMessage = "Invalid Value (string must not be null)";
137        return false;
138      } else {
139        errorMessage = string.Empty;
140        return true;
141      }
142    }
143    protected virtual string GetValue(int index) {
144      return this[index];
145    }
146    protected virtual bool SetValue(string value, int index) {
147      if (value != null) {
148        this[index] = value;
149        return true;
150      } else {
151        return false;
152      }
153    }
154
155    public event EventHandler<EventArgs<int>> ItemChanged;
156    protected virtual void OnItemChanged(int index) {
157      if (ItemChanged != null)
158        ItemChanged(this, new EventArgs<int>(index));
159      if (index < maximumToStringLength)
160        OnToStringChanged();
161    }
162    public event EventHandler Reset;
163    protected virtual void OnReset() {
164      if (Reset != null)
165        Reset(this, EventArgs.Empty);
166      OnToStringChanged();
167    }
168
169    #region IStringConvertibleArray Members
170    int IStringConvertibleArray.Length {
171      get { return Length; }
172      set { Length = value; }
173    }
174    bool IStringConvertibleArray.Validate(string value, out string errorMessage) {
175      return Validate(value, out errorMessage);
176    }
177    string IStringConvertibleArray.GetValue(int index) {
178      return GetValue(index);
179    }
180    bool IStringConvertibleArray.SetValue(string value, int index) {
181      return SetValue(value, index);
182    }
183    #endregion
184  }
185}
Note: See TracBrowser for help on using the repository browser.