Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 9657 was 9657, checked in by mkommend, 11 years ago

#2075: Added element names for the ValueType- and StringArray classes.

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