Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3403 was 3317, checked in by swagner, 14 years ago

Implemented ReadOnlyView property for items (#969).

File size: 4.8 KB
RevLine 
[2694]1#region License Information
2/* HeuristicLab
[2790]3 * Copyright (C) 2002-2010 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;
[3254]23using System.Linq;
[2694]24using System.Collections;
[3306]25using System.Drawing;
[2694]26using System.Text;
27using HeuristicLab.Common;
28using HeuristicLab.Core;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
[3254]30using System.Collections.Generic;
[2694]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 {
[3306]36    public override Image ItemImage {
37      get { return HeuristicLab.Common.Resources.VS2008ImageLibrary.Class; }
38    }
39
[2694]40    [Storable]
[3054]41    protected string[] array;
[2694]42
[3054]43    public virtual int Length {
[2694]44      get { return array.Length; }
[3054]45      protected set {
[2694]46        if (value != Length) {
47          Array.Resize<string>(ref array, value);
48          OnReset();
49        }
50      }
51    }
[3054]52    public virtual string this[int index] {
[2694]53      get { return array[index]; }
54      set {
55        if (value != array[index]) {
56          if ((value != null) || (array[index] != string.Empty)) {
57            array[index] = value != null ? value : string.Empty;
58            OnItemChanged(index);
59          }
60        }
61      }
62    }
63
[3048]64    public StringArray() {
[2694]65      array = new string[0];
66    }
[3048]67    public StringArray(int length) {
[2694]68      array = new string[length];
69      for (int i = 0; i < array.Length; i++)
70        array[i] = string.Empty;
71    }
[3048]72    public StringArray(string[] elements) {
[2694]73      if (elements == null) throw new ArgumentNullException();
74      array = new string[elements.Length];
75      for (int i = 0; i < array.Length; i++)
76        array[i] = elements[i] == null ? string.Empty : elements[i];
77    }
78
79    public override IDeepCloneable Clone(Cloner cloner) {
[3054]80      StringArray clone = new StringArray();
[2694]81      cloner.RegisterClonedObject(this, clone);
[3317]82      clone.ReadOnlyView = ReadOnlyView;
[3054]83      clone.array = (string[])array.Clone();
[2694]84      return clone;
85    }
86
87    public override string ToString() {
88      StringBuilder sb = new StringBuilder();
89      sb.Append("[");
90      if (array.Length > 0) {
91        sb.Append(array[0]);
92        for (int i = 1; i < array.Length; i++)
93          sb.Append(";").Append(array[i]);
94      }
95      sb.Append("]");
96      return sb.ToString();
97    }
98
[3254]99    public IEnumerator<string> GetEnumerator() {
[3263]100      return array.Cast<string>().GetEnumerator();
[2694]101    }
102
[3254]103    IEnumerator IEnumerable.GetEnumerator() {
[3263]104      return array.GetEnumerator();
[3254]105    }
106
[3054]107    protected virtual bool Validate(string value, out string errorMessage) {
[2694]108      if (value == null) {
109        errorMessage = "Invalid Value (string must not be null)";
110        return false;
111      } else {
112        errorMessage = string.Empty;
113        return true;
114      }
115    }
[3054]116    protected virtual string GetValue(int index) {
[2973]117      return this[index];
[2694]118    }
[3054]119    protected virtual bool SetValue(string value, int index) {
[2694]120      if (value != null) {
[2973]121        this[index] = value;
[2694]122        return true;
123      } else {
124        return false;
125      }
126    }
[3054]127
[2973]128    public event EventHandler<EventArgs<int>> ItemChanged;
[3054]129    protected virtual void OnItemChanged(int index) {
[2694]130      if (ItemChanged != null)
[2973]131        ItemChanged(this, new EventArgs<int>(index));
[2932]132      OnToStringChanged();
[2694]133    }
[2973]134    public event EventHandler Reset;
[3054]135    protected virtual void OnReset() {
[2694]136      if (Reset != null)
137        Reset(this, EventArgs.Empty);
[2932]138      OnToStringChanged();
[2694]139    }
[3054]140
141    #region IStringConvertibleArray Members
142    int IStringConvertibleArray.Length {
143      get { return Length; }
144      set { Length = value; }
145    }
146    bool IStringConvertibleArray.Validate(string value, out string errorMessage) {
147      return Validate(value, out errorMessage);
148    }
149    string IStringConvertibleArray.GetValue(int index) {
150      return GetValue(index);
151    }
152    bool IStringConvertibleArray.SetValue(string value, int index) {
153      return SetValue(value, index);
154    }
[2694]155    #endregion
156  }
157}
Note: See TracBrowser for help on using the repository browser.