Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 13329 was 12012, checked in by ascheibe, 10 years ago

#2212 merged r12008, r12009, r12010 back into trunk

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