Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ImprovingStringConvertibleMatrix/HeuristicLab.Data/3.3/StringArray.cs @ 9401

Last change on this file since 9401 was 9401, checked in by sforsten, 11 years ago

#2018: renamed "column" to "element" in array types

File size: 8.1 KB
RevLine 
[2694]1#region License Information
2/* HeuristicLab
[7259]3 * Copyright (C) 2002-2012 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 {
[7201]36    public static new Image StaticItemImage {
[5287]37      get { return HeuristicLab.Common.Resources.VSImageLibrary.Class; }
[3306]38    }
39
[2694]40    [Storable]
[3054]41    protected string[] array;
[2694]42
[9308]43    [Storable]
[9401]44    protected List<string> elementNames;
45    public virtual IEnumerable<string> ElementNames {
46      get { return this.elementNames; }
[9308]47      set {
[9401]48        if (ReadOnly) throw new NotSupportedException("ElementNames cannot be set. ValueTypeArray is read-only.");
[9308]49        if (value == null || value.Count() == 0)
[9401]50          elementNames = new List<string>();
[9308]51        else if (value.Count() != Length)
[9401]52          throw new ArgumentException("An element name must be specified for each element.");
[9308]53        else
[9401]54          elementNames = new List<string>(value);
55        OnElementNamesChanged();
[9308]56      }
57    }
58
[3054]59    public virtual int Length {
[2694]60      get { return array.Length; }
[3054]61      protected set {
[3430]62        if (ReadOnly) throw new NotSupportedException("Length cannot be set. StringArray is read-only.");
[2694]63        if (value != Length) {
64          Array.Resize<string>(ref array, value);
[9401]65          while (elementNames.Count > value)
66            elementNames.RemoveAt(elementNames.Count - 1);
67          while (elementNames.Count < value)
68            elementNames.Add("Element " + elementNames.Count);
69          OnElementNamesChanged();
[2694]70          OnReset();
71        }
72      }
73    }
[3054]74    public virtual string this[int index] {
[2694]75      get { return array[index]; }
76      set {
[3430]77        if (ReadOnly) throw new NotSupportedException("Item cannot be set. StringArray is read-only.");
[2694]78        if (value != array[index]) {
79          if ((value != null) || (array[index] != string.Empty)) {
80            array[index] = value != null ? value : string.Empty;
[9308]81            OnItemsChanged(new List<int>(1) { index });
[2694]82          }
83        }
84      }
85    }
86
[3430]87    [Storable]
88    protected bool readOnly;
89    public virtual bool ReadOnly {
90      get { return readOnly; }
91    }
92
[9308]93    [StorableHook(HookType.AfterDeserialization)]
94    private void AfterDeserialization() {
[9401]95      if (elementNames == null) {
96        elementNames = new List<string>(array.Length);
97        while (elementNames.Count < array.Length)
98          elementNames.Add("Element " + elementNames.Count);
[9308]99      }
100    }
101
[4722]102    [StorableConstructor]
103    protected StringArray(bool deserializing) : base(deserializing) { }
104    protected StringArray(StringArray original, Cloner cloner)
105      : base(original, cloner) {
106      this.array = (string[])original.array.Clone();
[9401]107      this.elementNames = new List<string>(original.elementNames);
[4722]108      this.readOnly = original.readOnly;
109    }
[3048]110    public StringArray() {
[2694]111      array = new string[0];
[9401]112      elementNames = new List<string>();
[3430]113      readOnly = false;
[2694]114    }
[3048]115    public StringArray(int length) {
[2694]116      array = new string[length];
117      for (int i = 0; i < array.Length; i++)
118        array[i] = string.Empty;
[9401]119      elementNames = new List<string>();
[3430]120      readOnly = false;
[2694]121    }
[3048]122    public StringArray(string[] elements) {
[2694]123      if (elements == null) throw new ArgumentNullException();
124      array = new string[elements.Length];
125      for (int i = 0; i < array.Length; i++)
126        array[i] = elements[i] == null ? string.Empty : elements[i];
[9401]127      elementNames = new List<string>();
[3430]128      readOnly = false;
[2694]129    }
130
131    public override IDeepCloneable Clone(Cloner cloner) {
[4722]132      return new StringArray(this, cloner);
[2694]133    }
134
[3430]135    public virtual StringArray AsReadOnly() {
136      StringArray readOnlyStringArray = (StringArray)this.Clone();
137      readOnlyStringArray.readOnly = true;
138      return readOnlyStringArray;
139    }
140
[2694]141    public override string ToString() {
142      StringBuilder sb = new StringBuilder();
143      sb.Append("[");
144      if (array.Length > 0) {
145        sb.Append(array[0]);
146        for (int i = 1; i < array.Length; i++)
147          sb.Append(";").Append(array[i]);
148      }
149      sb.Append("]");
150      return sb.ToString();
151    }
152
[3430]153    public virtual IEnumerator<string> GetEnumerator() {
[3263]154      return array.Cast<string>().GetEnumerator();
[2694]155    }
156
[3254]157    IEnumerator IEnumerable.GetEnumerator() {
[3430]158      return GetEnumerator();
[3254]159    }
160
[3054]161    protected virtual bool Validate(string value, out string errorMessage) {
[2694]162      if (value == null) {
163        errorMessage = "Invalid Value (string must not be null)";
164        return false;
165      } else {
166        errorMessage = string.Empty;
167        return true;
168      }
169    }
[3054]170    protected virtual string GetValue(int index) {
[2973]171      return this[index];
[2694]172    }
[3054]173    protected virtual bool SetValue(string value, int index) {
[9308]174      if (ReadOnly) throw new NotSupportedException("Item cannot be set. StringArray is read-only.");
[2694]175      if (value != null) {
[2973]176        this[index] = value;
[2694]177        return true;
178      } else {
179        return false;
180      }
181    }
[9308]182    public virtual bool SetValues(IEnumerable<ArrayValue<string>> arrayValues) {
183      if (ReadOnly) throw new NotSupportedException("Item cannot be set. StringArray is read-only.");
184      if (!arrayValues.All(v => v.Value != null)) return false;
185      List<int> positions = new List<int>();
186      foreach (var v in arrayValues) {
187        if (array[v.Index] != v.Value) {
188          array[v.Index] = v.Value;
189          positions.Add(v.Index);
190        }
191      }
192      OnItemsChanged(positions);
193      return true;
194    }
[3054]195
[9401]196    public event EventHandler ElementNamesChanged;
197    protected virtual void OnElementNamesChanged() {
198      EventHandler handler = ElementNamesChanged;
[9308]199      if (handler != null)
200        handler(this, EventArgs.Empty);
201    }
202    public event EventHandler<ArrayValuesChangedEventArgs> ItemsChanged;
203    protected virtual void OnItemsChanged(IEnumerable<int> indices) {
204      if (ItemsChanged != null)
205        ItemsChanged(this, new ArrayValuesChangedEventArgs(indices));
[2932]206      OnToStringChanged();
[2694]207    }
[2973]208    public event EventHandler Reset;
[3054]209    protected virtual void OnReset() {
[2694]210      if (Reset != null)
211        Reset(this, EventArgs.Empty);
[2932]212      OnToStringChanged();
[2694]213    }
[3054]214
215    #region IStringConvertibleArray Members
216    int IStringConvertibleArray.Length {
217      get { return Length; }
218      set { Length = value; }
219    }
220    bool IStringConvertibleArray.Validate(string value, out string errorMessage) {
221      return Validate(value, out errorMessage);
222    }
223    string IStringConvertibleArray.GetValue(int index) {
224      return GetValue(index);
225    }
226    bool IStringConvertibleArray.SetValue(string value, int index) {
227      return SetValue(value, index);
228    }
[9308]229    bool IStringConvertibleArray.SetValue(ArrayValue<string> arrayValue) {
230      return SetValue(arrayValue.Value, arrayValue.Index);
231    }
232    bool IStringConvertibleArray.SetValues(IEnumerable<ArrayValue<string>> arrayValues) {
233      return SetValues(arrayValues);
234    }
[2694]235    #endregion
236  }
237}
Note: See TracBrowser for help on using the repository browser.