Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 9562 was 9456, checked in by swagner, 12 years ago

Updated copyright year and added some missing license headers (#1889)

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