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
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
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.Linq;
24using System.Collections;
25using System.Drawing;
26using System.Text;
27using HeuristicLab.Common;
28using HeuristicLab.Core;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30using System.Collections.Generic;
31
32namespace HeuristicLab.Data {
33  [Item("StringArray", "Represents an array of strings.")]
34  [StorableClass]
35  public class StringArray : Item, IEnumerable<string>, IStringConvertibleArray {
36    public override Image ItemImage {
37      get { return HeuristicLab.Common.Resources.VS2008ImageLibrary.Class; }
38    }
39
40    [Storable]
41    protected string[] array;
42
43    public virtual int Length {
44      get { return array.Length; }
45      protected set {
46        if (value != Length) {
47          Array.Resize<string>(ref array, value);
48          OnReset();
49        }
50      }
51    }
52    public virtual string this[int index] {
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
64    public StringArray() {
65      array = new string[0];
66    }
67    public StringArray(int length) {
68      array = new string[length];
69      for (int i = 0; i < array.Length; i++)
70        array[i] = string.Empty;
71    }
72    public StringArray(string[] elements) {
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) {
80      StringArray clone = new StringArray();
81      cloner.RegisterClonedObject(this, clone);
82      clone.ReadOnlyView = ReadOnlyView;
83      clone.array = (string[])array.Clone();
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
99    public IEnumerator<string> GetEnumerator() {
100      return array.Cast<string>().GetEnumerator();
101    }
102
103    IEnumerator IEnumerable.GetEnumerator() {
104      return array.GetEnumerator();
105    }
106
107    protected virtual bool Validate(string value, out string errorMessage) {
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    }
116    protected virtual string GetValue(int index) {
117      return this[index];
118    }
119    protected virtual bool SetValue(string value, int index) {
120      if (value != null) {
121        this[index] = value;
122        return true;
123      } else {
124        return false;
125      }
126    }
127
128    public event EventHandler<EventArgs<int>> ItemChanged;
129    protected virtual void OnItemChanged(int index) {
130      if (ItemChanged != null)
131        ItemChanged(this, new EventArgs<int>(index));
132      OnToStringChanged();
133    }
134    public event EventHandler Reset;
135    protected virtual void OnReset() {
136      if (Reset != null)
137        Reset(this, EventArgs.Empty);
138      OnToStringChanged();
139    }
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    }
155    #endregion
156  }
157}
Note: See TracBrowser for help on using the repository browser.