Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Data/3.3/ValueTypeArray.cs @ 9692

Last change on this file since 9692 was 9692, checked in by mkommend, 10 years ago

#2075: Corrected changing of element names if a ValueType- or StringArray is resized.

File size: 5.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 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.Collections;
24using System.Collections.Generic;
25using System.Drawing;
26using System.Linq;
27using System.Text;
28using HeuristicLab.Common;
29using HeuristicLab.Core;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31
32namespace HeuristicLab.Data {
33  [Item("ValueTypeArray", "An abstract base class for representing arrays of value types.")]
34  [StorableClass]
35  public abstract class ValueTypeArray<T> : Item, IEnumerable<T> where T : struct {
36    private const int maximumToStringLength = 100;
37
38    public static new Image StaticItemImage {
39      get { return HeuristicLab.Common.Resources.VSImageLibrary.Class; }
40    }
41
42    [Storable]
43    protected T[] array;
44
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>();
53        else if (value.Count() != Length)
54          throw new ArgumentException("An element name must be specified for each element.");
55        else
56          elementNames = new List<string>(value);
57        OnElementNamesChanged();
58      }
59    }
60
61    public virtual int Length {
62      get { return array.Length; }
63      #region Mono Compatibility
64      // this setter should be protected, but the Mono compiler couldn't handle it
65      set {
66        if (ReadOnly) throw new NotSupportedException("Length cannot be set. ValueTypeArray is read-only.");
67        if (value != Length) {
68          Array.Resize<T>(ref array, value);
69          while (elementNames.Count > value)
70            elementNames.RemoveAt(elementNames.Count - 1);
71          if (elementNames.Any()) {
72            while (elementNames.Count < value)
73              elementNames.Add("Element " + elementNames.Count);
74          }
75          OnElementNamesChanged();
76          OnReset();
77        }
78      }
79      #endregion
80    }
81    public virtual T this[int index] {
82      get { return array[index]; }
83      set {
84        if (ReadOnly) throw new NotSupportedException("Item cannot be set. ValueTypeArray is read-only.");
85        if (!value.Equals(array[index])) {
86          array[index] = value;
87          OnItemChanged(index);
88        }
89      }
90    }
91
92    [Storable]
93    protected bool readOnly;
94    public virtual bool ReadOnly {
95      get { return readOnly; }
96    }
97
98    [StorableHook(HookType.AfterDeserialization)]
99    private void AfterDeserialization() {
100      if (elementNames == null) { elementNames = new List<string>(); }
101    }
102
103    [StorableConstructor]
104    protected ValueTypeArray(bool deserializing) : base(deserializing) { }
105    protected ValueTypeArray(ValueTypeArray<T> original, Cloner cloner)
106      : base(original, cloner) {
107      this.array = (T[])original.array.Clone();
108      this.readOnly = original.readOnly;
109      this.elementNames = new List<string>(original.elementNames);
110    }
111    protected ValueTypeArray() {
112      array = new T[0];
113      readOnly = false;
114      elementNames = new List<string>();
115    }
116    protected ValueTypeArray(int length) {
117      array = new T[length];
118      readOnly = false;
119      elementNames = new List<string>();
120    }
121    protected ValueTypeArray(T[] elements) {
122      if (elements == null) throw new ArgumentNullException();
123      array = (T[])elements.Clone();
124      readOnly = false;
125      elementNames = new List<string>();
126    }
127
128    public virtual ValueTypeArray<T> AsReadOnly() {
129      ValueTypeArray<T> readOnlyValueTypeArray = (ValueTypeArray<T>)this.Clone();
130      readOnlyValueTypeArray.readOnly = true;
131      return readOnlyValueTypeArray;
132    }
133
134    public override string ToString() {
135      if (array.Length == 0) return "[]";
136
137      StringBuilder sb = new StringBuilder();
138      sb.Append("[");
139      sb.Append(array[0].ToString());
140      for (int i = 1; i < array.Length; i++) {
141        sb.Append(";").Append(array[i].ToString());
142        if (sb.Length > maximumToStringLength) {
143          sb.Append("...");
144          break;
145        }
146      }
147      sb.Append("]");
148      return sb.ToString();
149    }
150
151    public virtual IEnumerator<T> GetEnumerator() {
152      return array.Cast<T>().GetEnumerator();
153    }
154
155    IEnumerator IEnumerable.GetEnumerator() {
156      return GetEnumerator();
157    }
158
159    public event EventHandler ElementNamesChanged;
160    protected virtual void OnElementNamesChanged() {
161      EventHandler handler = ElementNamesChanged;
162      if (handler != null)
163        handler(this, EventArgs.Empty);
164    }
165
166    public event EventHandler<EventArgs<int>> ItemChanged;
167    protected virtual void OnItemChanged(int index) {
168      if (ItemChanged != null)
169        ItemChanged(this, new EventArgs<int>(index));
170      if (index < maximumToStringLength)
171        OnToStringChanged();
172    }
173    public event EventHandler Reset;
174    protected virtual void OnReset() {
175      if (Reset != null)
176        Reset(this, EventArgs.Empty);
177      OnToStringChanged();
178    }
179  }
180}
Note: See TracBrowser for help on using the repository browser.