Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 6305 was 5445, checked in by swagner, 13 years ago

Updated year of copyrights (#1406)

File size: 5.5 KB
RevLine 
[2694]1#region License Information
2/* HeuristicLab
[5445]3 * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[2694]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;
[4068]24using System.Collections.Generic;
[3306]25using System.Drawing;
[4068]26using System.Linq;
[2694]27using System.Text;
28using HeuristicLab.Common;
29using HeuristicLab.Core;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31
32namespace HeuristicLab.Data {
[3048]33  [Item("StringArray", "Represents an array of strings.")]
[3017]34  [StorableClass]
[3254]35  public class StringArray : Item, IEnumerable<string>, IStringConvertibleArray {
[3306]36    public override Image ItemImage {
[5287]37      get { return HeuristicLab.Common.Resources.VSImageLibrary.Class; }
[3306]38    }
39
[2694]40    [Storable]
[3054]41    protected string[] array;
[2694]42
[3054]43    public virtual int Length {
[2694]44      get { return array.Length; }
[3054]45      protected set {
[3430]46        if (ReadOnly) throw new NotSupportedException("Length cannot be set. StringArray is read-only.");
[2694]47        if (value != Length) {
48          Array.Resize<string>(ref array, value);
49          OnReset();
50        }
51      }
52    }
[3054]53    public virtual string this[int index] {
[2694]54      get { return array[index]; }
55      set {
[3430]56        if (ReadOnly) throw new NotSupportedException("Item cannot be set. StringArray is read-only.");
[2694]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
[3430]66    [Storable]
67    protected bool readOnly;
68    public virtual bool ReadOnly {
69      get { return readOnly; }
70    }
71
[4722]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    }
[3048]79    public StringArray() {
[2694]80      array = new string[0];
[3430]81      readOnly = false;
[2694]82    }
[3048]83    public StringArray(int length) {
[2694]84      array = new string[length];
85      for (int i = 0; i < array.Length; i++)
86        array[i] = string.Empty;
[3430]87      readOnly = false;
[2694]88    }
[3048]89    public StringArray(string[] elements) {
[2694]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];
[3430]94      readOnly = false;
[2694]95    }
96
97    public override IDeepCloneable Clone(Cloner cloner) {
[4722]98      return new StringArray(this, cloner);
[2694]99    }
100
[3430]101    public virtual StringArray AsReadOnly() {
102      StringArray readOnlyStringArray = (StringArray)this.Clone();
103      readOnlyStringArray.readOnly = true;
104      return readOnlyStringArray;
105    }
106
[2694]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
[3430]119    public virtual IEnumerator<string> GetEnumerator() {
[3263]120      return array.Cast<string>().GetEnumerator();
[2694]121    }
122
[3254]123    IEnumerator IEnumerable.GetEnumerator() {
[3430]124      return GetEnumerator();
[3254]125    }
126
[3054]127    protected virtual bool Validate(string value, out string errorMessage) {
[2694]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    }
[3054]136    protected virtual string GetValue(int index) {
[2973]137      return this[index];
[2694]138    }
[3054]139    protected virtual bool SetValue(string value, int index) {
[2694]140      if (value != null) {
[2973]141        this[index] = value;
[2694]142        return true;
143      } else {
144        return false;
145      }
146    }
[3054]147
[2973]148    public event EventHandler<EventArgs<int>> ItemChanged;
[3054]149    protected virtual void OnItemChanged(int index) {
[2694]150      if (ItemChanged != null)
[2973]151        ItemChanged(this, new EventArgs<int>(index));
[2932]152      OnToStringChanged();
[2694]153    }
[2973]154    public event EventHandler Reset;
[3054]155    protected virtual void OnReset() {
[2694]156      if (Reset != null)
157        Reset(this, EventArgs.Empty);
[2932]158      OnToStringChanged();
[2694]159    }
[3054]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    }
[2694]175    #endregion
176  }
177}
Note: See TracBrowser for help on using the repository browser.