Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 9657 was 9657, checked in by mkommend, 10 years ago

#2075: Added element names for the ValueType- and StringArray classes.

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