Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Data/3.3/StringArray.cs @ 15942

Last change on this file since 15942 was 15583, checked in by swagner, 7 years ago

#2640: Updated year of copyrights in license headers

File size: 7.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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    [Storable]
46    protected List<string> elementNames;
47    public virtual IEnumerable<string> ElementNames {
48      get { return this.elementNames; }
49      set {
50        if (ReadOnly) throw new NotSupportedException("ElementNames cannot be set. ValueTypeArray is read-only.");
51        if (value == null || !value.Any())
52          elementNames = new List<string>();
53        else if (value.Count() > Length)
54          throw new ArgumentException("The number of element names must not exceed the array length.");
55        else
56          elementNames = new List<string>(value);
57        OnElementNamesChanged();
58      }
59    }
60
61    public virtual int Length {
62      get { return array.Length; }
63      set {
64        if (ReadOnly) throw new NotSupportedException("Length cannot be set. StringArray is read-only.");
65        if (value != Length) {
66          Array.Resize<string>(ref array, value);
67          while (elementNames.Count > value)
68            elementNames.RemoveAt(elementNames.Count - 1);
69          OnElementNamesChanged();
70          OnReset();
71        }
72      }
73    }
74    [Storable]
75    protected bool resizable = true;
76    public bool Resizable {
77      get { return resizable; }
78      set {
79        if (resizable != value) {
80          resizable = value;
81          OnResizableChanged();
82        }
83      }
84    }
85
86    public virtual string this[int index] {
87      get { return array[index]; }
88      set {
89        if (ReadOnly) throw new NotSupportedException("Item cannot be set. StringArray is read-only.");
90        if (value != array[index]) {
91          if ((value != null) || (array[index] != string.Empty)) {
92            array[index] = value != null ? value : string.Empty;
93            OnItemChanged(index);
94          }
95        }
96      }
97    }
98
99    [Storable]
100    protected bool readOnly;
101    public virtual bool ReadOnly {
102      get { return readOnly; }
103    }
104
105    [StorableHook(HookType.AfterDeserialization)]
106    private void AfterDeserialization() {
107      if (elementNames == null) { elementNames = new List<string>(); }
108    }
109
110    [StorableConstructor]
111    protected StringArray(bool deserializing) : base(deserializing) { }
112    protected StringArray(StringArray original, Cloner cloner)
113      : base(original, cloner) {
114      this.array = (string[])original.array.Clone();
115      this.readOnly = original.readOnly;
116      this.resizable = original.resizable;
117      this.elementNames = new List<string>(original.elementNames);
118    }
119    public StringArray() {
120      array = new string[0];
121      readOnly = false;
122      resizable = true;
123      elementNames = new List<string>();
124    }
125    public StringArray(int length) {
126      array = new string[length];
127      for (int i = 0; i < array.Length; i++)
128        array[i] = string.Empty;
129      readOnly = false;
130      resizable = true;
131      elementNames = new List<string>();
132    }
133    public StringArray(string[] elements) {
134      if (elements == null) throw new ArgumentNullException();
135      array = new string[elements.Length];
136      for (int i = 0; i < array.Length; i++)
137        array[i] = elements[i] == null ? string.Empty : elements[i];
138      readOnly = false;
139      resizable = true;
140      elementNames = new List<string>();
141    }
142
143    public override IDeepCloneable Clone(Cloner cloner) {
144      return new StringArray(this, cloner);
145    }
146
147    public virtual StringArray AsReadOnly() {
148      StringArray readOnlyStringArray = (StringArray)this.Clone();
149      readOnlyStringArray.readOnly = true;
150      return readOnlyStringArray;
151    }
152    IValueTypeArray IValueTypeArray.AsReadOnly() {
153      return AsReadOnly();
154    }
155
156    public override string ToString() {
157      if (array.Length == 0) return "[]";
158
159      StringBuilder sb = new StringBuilder();
160      sb.Append("[");
161      sb.Append(array[0]);
162      for (int i = 1; i < array.Length; i++) {
163        sb.Append(";").Append(array[i]);
164        if (sb.Length > maximumToStringLength) {
165          sb.Append("...");
166          break;
167        }
168      }
169      sb.Append("]");
170      return sb.ToString();
171    }
172
173    public virtual IEnumerator<string> GetEnumerator() {
174      return array.Cast<string>().GetEnumerator();
175    }
176    IEnumerator IEnumerable.GetEnumerator() {
177      return GetEnumerator();
178    }
179
180    protected virtual bool Validate(string value, out string errorMessage) {
181      if (value == null) {
182        errorMessage = "Invalid Value (string must not be null)";
183        return false;
184      } else {
185        errorMessage = string.Empty;
186        return true;
187      }
188    }
189    protected virtual string GetValue(int index) {
190      return this[index];
191    }
192    protected virtual bool SetValue(string value, int index) {
193      if (value != null) {
194        this[index] = value;
195        return true;
196      } else {
197        return false;
198      }
199    }
200
201    public event EventHandler ResizableChanged;
202    protected virtual void OnResizableChanged() {
203      EventHandler handler = ResizableChanged;
204      if (handler != null)
205        handler(this, EventArgs.Empty);
206    }
207    public event EventHandler ElementNamesChanged;
208    protected virtual void OnElementNamesChanged() {
209      EventHandler handler = ElementNamesChanged;
210      if (handler != null)
211        handler(this, EventArgs.Empty);
212    }
213    public event EventHandler<EventArgs<int>> ItemChanged;
214    protected virtual void OnItemChanged(int index) {
215      if (ItemChanged != null)
216        ItemChanged(this, new EventArgs<int>(index));
217      if (index < maximumToStringLength)
218        OnToStringChanged();
219    }
220    public event EventHandler Reset;
221    protected virtual void OnReset() {
222      if (Reset != null)
223        Reset(this, EventArgs.Empty);
224      OnToStringChanged();
225    }
226
227    #region IStringConvertibleArray Members
228    bool IStringConvertibleArray.Validate(string value, out string errorMessage) {
229      return Validate(value, out errorMessage);
230    }
231    string IStringConvertibleArray.GetValue(int index) {
232      return GetValue(index);
233    }
234    bool IStringConvertibleArray.SetValue(string value, int index) {
235      return SetValue(value, index);
236    }
237    #endregion
238  }
239}
Note: See TracBrowser for help on using the repository browser.