Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 9657 was 9657, checked in by mkommend, 11 years ago

#2075: Added element names for the ValueType- and StringArray classes.

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