Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4447 was 4068, checked in by swagner, 14 years ago

Sorted usings and removed unused usings in entire solution (#1094)

File size: 5.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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 override Image ItemImage {
37      get { return HeuristicLab.Common.Resources.VS2008ImageLibrary.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    public StringArray() {
73      array = new string[0];
74      readOnly = false;
75    }
76    public StringArray(int length) {
77      array = new string[length];
78      for (int i = 0; i < array.Length; i++)
79        array[i] = string.Empty;
80      readOnly = false;
81    }
82    public StringArray(string[] elements) {
83      if (elements == null) throw new ArgumentNullException();
84      array = new string[elements.Length];
85      for (int i = 0; i < array.Length; i++)
86        array[i] = elements[i] == null ? string.Empty : elements[i];
87      readOnly = false;
88    }
89
90    public override IDeepCloneable Clone(Cloner cloner) {
91      StringArray clone = new StringArray();
92      cloner.RegisterClonedObject(this, clone);
93      clone.array = (string[])array.Clone();
94      clone.readOnly = readOnly;
95      return clone;
96    }
97
98    public virtual StringArray AsReadOnly() {
99      StringArray readOnlyStringArray = (StringArray)this.Clone();
100      readOnlyStringArray.readOnly = true;
101      return readOnlyStringArray;
102    }
103
104    public override string ToString() {
105      StringBuilder sb = new StringBuilder();
106      sb.Append("[");
107      if (array.Length > 0) {
108        sb.Append(array[0]);
109        for (int i = 1; i < array.Length; i++)
110          sb.Append(";").Append(array[i]);
111      }
112      sb.Append("]");
113      return sb.ToString();
114    }
115
116    public virtual IEnumerator<string> GetEnumerator() {
117      return array.Cast<string>().GetEnumerator();
118    }
119
120    IEnumerator IEnumerable.GetEnumerator() {
121      return GetEnumerator();
122    }
123
124    protected virtual bool Validate(string value, out string errorMessage) {
125      if (value == null) {
126        errorMessage = "Invalid Value (string must not be null)";
127        return false;
128      } else {
129        errorMessage = string.Empty;
130        return true;
131      }
132    }
133    protected virtual string GetValue(int index) {
134      return this[index];
135    }
136    protected virtual bool SetValue(string value, int index) {
137      if (value != null) {
138        this[index] = value;
139        return true;
140      } else {
141        return false;
142      }
143    }
144
145    public event EventHandler<EventArgs<int>> ItemChanged;
146    protected virtual void OnItemChanged(int index) {
147      if (ItemChanged != null)
148        ItemChanged(this, new EventArgs<int>(index));
149      OnToStringChanged();
150    }
151    public event EventHandler Reset;
152    protected virtual void OnReset() {
153      if (Reset != null)
154        Reset(this, EventArgs.Empty);
155      OnToStringChanged();
156    }
157
158    #region IStringConvertibleArray Members
159    int IStringConvertibleArray.Length {
160      get { return Length; }
161      set { Length = value; }
162    }
163    bool IStringConvertibleArray.Validate(string value, out string errorMessage) {
164      return Validate(value, out errorMessage);
165    }
166    string IStringConvertibleArray.GetValue(int index) {
167      return GetValue(index);
168    }
169    bool IStringConvertibleArray.SetValue(string value, int index) {
170      return SetValue(value, index);
171    }
172    #endregion
173  }
174}
Note: See TracBrowser for help on using the repository browser.