Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 7911 was 7259, checked in by swagner, 13 years ago

Updated year of copyrights to 2012 (#1716)

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