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

Last change on this file since 3254 was 3254, checked in by gkronber, 11 years ago

Changed StringArray to implement IEnumerable<string> instead of IEnumerable. #957 (StringArray should implement IEnumerable<string>)

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