Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Data/3.3/StringArray.cs @ 18086

Last change on this file since 18086 was 17253, checked in by abeham, 5 years ago

#2521: worked on refactoring PTSP

File size: 7.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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 HEAL.Attic;
29using HeuristicLab.Common;
30using HeuristicLab.Core;
31
32namespace HeuristicLab.Data {
33  [Item("StringArray", "Represents an array of strings.")]
34  [StorableType("E226B187-2D74-4112-A91E-830B0408D1BF")]
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(StorableConstructorFlag _) : base(_) { }
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, bool @readonly = false) {
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 = @readonly;
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      if (ReadOnly) return this;
149      var clone = (StringArray)this.Clone();
150      clone.readOnly = true;
151      return clone;
152    }
153    IValueTypeArray IValueTypeArray.AsReadOnly() {
154      return AsReadOnly();
155    }
156
157    public override string ToString() {
158      if (array.Length == 0) return "[]";
159
160      StringBuilder sb = new StringBuilder();
161      sb.Append("[");
162      sb.Append(array[0]);
163      for (int i = 1; i < array.Length; i++) {
164        sb.Append(";").Append(array[i]);
165        if (sb.Length > maximumToStringLength) {
166          sb.Append("...");
167          break;
168        }
169      }
170      sb.Append("]");
171      return sb.ToString();
172    }
173
174    public virtual IEnumerator<string> GetEnumerator() {
175      return array.Cast<string>().GetEnumerator();
176    }
177    IEnumerator IEnumerable.GetEnumerator() {
178      return GetEnumerator();
179    }
180
181    protected virtual bool Validate(string value, out string errorMessage) {
182      if (value == null) {
183        errorMessage = "Invalid Value (string must not be null)";
184        return false;
185      } else {
186        errorMessage = string.Empty;
187        return true;
188      }
189    }
190    protected virtual string GetValue(int index) {
191      return this[index];
192    }
193    protected virtual bool SetValue(string value, int index) {
194      if (value != null) {
195        this[index] = value;
196        return true;
197      } else {
198        return false;
199      }
200    }
201
202    public event EventHandler ResizableChanged;
203    protected virtual void OnResizableChanged() {
204      EventHandler handler = ResizableChanged;
205      if (handler != null)
206        handler(this, EventArgs.Empty);
207    }
208    public event EventHandler ElementNamesChanged;
209    protected virtual void OnElementNamesChanged() {
210      EventHandler handler = ElementNamesChanged;
211      if (handler != null)
212        handler(this, EventArgs.Empty);
213    }
214    public event EventHandler<EventArgs<int>> ItemChanged;
215    protected virtual void OnItemChanged(int index) {
216      if (ItemChanged != null)
217        ItemChanged(this, new EventArgs<int>(index));
218      if (index < maximumToStringLength)
219        OnToStringChanged();
220    }
221    public event EventHandler Reset;
222    protected virtual void OnReset() {
223      if (Reset != null)
224        Reset(this, EventArgs.Empty);
225      OnToStringChanged();
226    }
227
228    #region IStringConvertibleArray Members
229    bool IStringConvertibleArray.Validate(string value, out string errorMessage) {
230      return Validate(value, out errorMessage);
231    }
232    string IStringConvertibleArray.GetValue(int index) {
233      return GetValue(index);
234    }
235    bool IStringConvertibleArray.SetValue(string value, int index) {
236      return SetValue(value, index);
237    }
238    #endregion
239  }
240}
Note: See TracBrowser for help on using the repository browser.