Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2839_HiveProjectManagement/HeuristicLab.Data/3.3/StringArray.cs @ 17692

Last change on this file since 17692 was 16057, checked in by jkarder, 6 years ago

#2839:

File size: 7.6 KB
RevLine 
[2694]1#region License Information
2/* HeuristicLab
[16057]3 * Copyright (C) 2002-2018 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>();
[9695]53        else if (value.Count() > Length)
54          throw new ArgumentException("The number of element names must not exceed the array length.");
[9657]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; }
[13695]63      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          OnElementNamesChanged();
[2694]70          OnReset();
71        }
72      }
73    }
[13695]74    [Storable]
75    protected bool resizable = true;
76    public bool Resizable {
77      get { return resizable; }
78      set {
79        if (resizable != value) {
80          resizable = value;
81          OnResizableChanged();
82        }
83      }
84    }
85
[3054]86    public virtual string this[int index] {
[2694]87      get { return array[index]; }
88      set {
[3430]89        if (ReadOnly) throw new NotSupportedException("Item cannot be set. StringArray is read-only.");
[2694]90        if (value != array[index]) {
91          if ((value != null) || (array[index] != string.Empty)) {
92            array[index] = value != null ? value : string.Empty;
93            OnItemChanged(index);
94          }
95        }
96      }
97    }
98
[3430]99    [Storable]
100    protected bool readOnly;
101    public virtual bool ReadOnly {
102      get { return readOnly; }
103    }
104
[9657]105    [StorableHook(HookType.AfterDeserialization)]
106    private void AfterDeserialization() {
107      if (elementNames == null) { elementNames = new List<string>(); }
108    }
109
[4722]110    [StorableConstructor]
111    protected StringArray(bool deserializing) : base(deserializing) { }
112    protected StringArray(StringArray original, Cloner cloner)
113      : base(original, cloner) {
114      this.array = (string[])original.array.Clone();
115      this.readOnly = original.readOnly;
[13695]116      this.resizable = original.resizable;
[9657]117      this.elementNames = new List<string>(original.elementNames);
[4722]118    }
[3048]119    public StringArray() {
[2694]120      array = new string[0];
[3430]121      readOnly = false;
[13695]122      resizable = true;
[9657]123      elementNames = new List<string>();
[2694]124    }
[3048]125    public StringArray(int length) {
[2694]126      array = new string[length];
127      for (int i = 0; i < array.Length; i++)
128        array[i] = string.Empty;
[3430]129      readOnly = false;
[13695]130      resizable = true;
[9657]131      elementNames = new List<string>();
[2694]132    }
[3048]133    public StringArray(string[] elements) {
[2694]134      if (elements == null) throw new ArgumentNullException();
135      array = new string[elements.Length];
136      for (int i = 0; i < array.Length; i++)
137        array[i] = elements[i] == null ? string.Empty : elements[i];
[3430]138      readOnly = false;
[13695]139      resizable = true;
[9657]140      elementNames = new List<string>();
[2694]141    }
142
143    public override IDeepCloneable Clone(Cloner cloner) {
[4722]144      return new StringArray(this, cloner);
[2694]145    }
146
[3430]147    public virtual StringArray AsReadOnly() {
148      StringArray readOnlyStringArray = (StringArray)this.Clone();
149      readOnlyStringArray.readOnly = true;
150      return readOnlyStringArray;
151    }
[13695]152    IValueTypeArray IValueTypeArray.AsReadOnly() {
153      return AsReadOnly();
154    }
[3430]155
[2694]156    public override string ToString() {
[9433]157      if (array.Length == 0) return "[]";
158
[2694]159      StringBuilder sb = new StringBuilder();
160      sb.Append("[");
[9433]161      sb.Append(array[0]);
162      for (int i = 1; i < array.Length; i++) {
163        sb.Append(";").Append(array[i]);
164        if (sb.Length > maximumToStringLength) {
165          sb.Append("...");
166          break;
167        }
[2694]168      }
169      sb.Append("]");
170      return sb.ToString();
171    }
172
[3430]173    public virtual IEnumerator<string> GetEnumerator() {
[3263]174      return array.Cast<string>().GetEnumerator();
[2694]175    }
[3254]176    IEnumerator IEnumerable.GetEnumerator() {
[3430]177      return GetEnumerator();
[3254]178    }
179
[3054]180    protected virtual bool Validate(string value, out string errorMessage) {
[2694]181      if (value == null) {
182        errorMessage = "Invalid Value (string must not be null)";
183        return false;
184      } else {
185        errorMessage = string.Empty;
186        return true;
187      }
188    }
[3054]189    protected virtual string GetValue(int index) {
[2973]190      return this[index];
[2694]191    }
[3054]192    protected virtual bool SetValue(string value, int index) {
[2694]193      if (value != null) {
[2973]194        this[index] = value;
[2694]195        return true;
196      } else {
197        return false;
198      }
199    }
[3054]200
[13695]201    public event EventHandler ResizableChanged;
202    protected virtual void OnResizableChanged() {
203      EventHandler handler = ResizableChanged;
204      if (handler != null)
205        handler(this, EventArgs.Empty);
206    }
[9657]207    public event EventHandler ElementNamesChanged;
208    protected virtual void OnElementNamesChanged() {
209      EventHandler handler = ElementNamesChanged;
210      if (handler != null)
211        handler(this, EventArgs.Empty);
212    }
[2973]213    public event EventHandler<EventArgs<int>> ItemChanged;
[3054]214    protected virtual void OnItemChanged(int index) {
[2694]215      if (ItemChanged != null)
[2973]216        ItemChanged(this, new EventArgs<int>(index));
[9433]217      if (index < maximumToStringLength)
218        OnToStringChanged();
[2694]219    }
[2973]220    public event EventHandler Reset;
[3054]221    protected virtual void OnReset() {
[2694]222      if (Reset != null)
223        Reset(this, EventArgs.Empty);
[2932]224      OnToStringChanged();
[2694]225    }
[3054]226
227    #region IStringConvertibleArray Members
228    bool IStringConvertibleArray.Validate(string value, out string errorMessage) {
229      return Validate(value, out errorMessage);
230    }
231    string IStringConvertibleArray.GetValue(int index) {
232      return GetValue(index);
233    }
234    bool IStringConvertibleArray.SetValue(string value, int index) {
235      return SetValue(value, index);
236    }
[2694]237    #endregion
238  }
239}
Note: See TracBrowser for help on using the repository browser.