Free cookie consent management tool by TermsFeed Policy Generator

source: branches/WebJobManager/HeuristicLab.Data/3.3/StringArray.cs @ 13782

Last change on this file since 13782 was 13656, checked in by ascheibe, 9 years ago

#2582 created branch for Hive Web Job Manager

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