Free cookie consent management tool by TermsFeed Policy Generator

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

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

Implemented reviewers' comments (#893).

File size: 4.7 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.Linq;
24using System.Collections;
25using System.Drawing;
26using System.Text;
27using HeuristicLab.Common;
28using HeuristicLab.Core;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30using System.Collections.Generic;
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 (value != Length) {
47          Array.Resize<string>(ref array, value);
48          OnReset();
49        }
50      }
51    }
52    public virtual string this[int index] {
53      get { return array[index]; }
54      set {
55        if (value != array[index]) {
56          if ((value != null) || (array[index] != string.Empty)) {
57            array[index] = value != null ? value : string.Empty;
58            OnItemChanged(index);
59          }
60        }
61      }
62    }
63
64    public StringArray() {
65      array = new string[0];
66    }
67    public StringArray(int length) {
68      array = new string[length];
69      for (int i = 0; i < array.Length; i++)
70        array[i] = string.Empty;
71    }
72    public StringArray(string[] elements) {
73      if (elements == null) throw new ArgumentNullException();
74      array = new string[elements.Length];
75      for (int i = 0; i < array.Length; i++)
76        array[i] = elements[i] == null ? string.Empty : elements[i];
77    }
78
79    public override IDeepCloneable Clone(Cloner cloner) {
80      StringArray clone = new StringArray();
81      cloner.RegisterClonedObject(this, clone);
82      clone.array = (string[])array.Clone();
83      return clone;
84    }
85
86    public override string ToString() {
87      StringBuilder sb = new StringBuilder();
88      sb.Append("[");
89      if (array.Length > 0) {
90        sb.Append(array[0]);
91        for (int i = 1; i < array.Length; i++)
92          sb.Append(";").Append(array[i]);
93      }
94      sb.Append("]");
95      return sb.ToString();
96    }
97
98    public IEnumerator<string> GetEnumerator() {
99      return array.Cast<string>().GetEnumerator();
100    }
101
102    IEnumerator IEnumerable.GetEnumerator() {
103      return array.GetEnumerator();
104    }
105
106    protected virtual bool Validate(string value, out string errorMessage) {
107      if (value == null) {
108        errorMessage = "Invalid Value (string must not be null)";
109        return false;
110      } else {
111        errorMessage = string.Empty;
112        return true;
113      }
114    }
115    protected virtual string GetValue(int index) {
116      return this[index];
117    }
118    protected virtual bool SetValue(string value, int index) {
119      if (value != null) {
120        this[index] = value;
121        return true;
122      } else {
123        return false;
124      }
125    }
126
127    public event EventHandler<EventArgs<int>> ItemChanged;
128    protected virtual void OnItemChanged(int index) {
129      if (ItemChanged != null)
130        ItemChanged(this, new EventArgs<int>(index));
131      OnToStringChanged();
132    }
133    public event EventHandler Reset;
134    protected virtual void OnReset() {
135      if (Reset != null)
136        Reset(this, EventArgs.Empty);
137      OnToStringChanged();
138    }
139
140    #region IStringConvertibleArray Members
141    int IStringConvertibleArray.Length {
142      get { return Length; }
143      set { Length = value; }
144    }
145    bool IStringConvertibleArray.Validate(string value, out string errorMessage) {
146      return Validate(value, out errorMessage);
147    }
148    string IStringConvertibleArray.GetValue(int index) {
149      return GetValue(index);
150    }
151    bool IStringConvertibleArray.SetValue(string value, int index) {
152      return SetValue(value, index);
153    }
154    #endregion
155  }
156}
Note: See TracBrowser for help on using the repository browser.