Free cookie consent management tool by TermsFeed Policy Generator

source: branches/WebJobManager/HeuristicLab.Data/3.3/ValueTypeArray.cs @ 13656

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

#2582 created branch for Hive Web Job Manager

File size: 5.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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    {
40      get { return new Bitmap(25, 25); }
41    }
42
43    [Storable]
44    protected T[] array;
45
46    [Storable]
47    protected List<string> elementNames;
48    public virtual IEnumerable<string> ElementNames
49    {
50      get { return this.elementNames; }
51      set
52      {
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>();
56        else if (value.Count() > Length)
57          throw new ArgumentException("The number of element names must not exceed the array length.");
58        else
59          elementNames = new List<string>(value);
60        OnElementNamesChanged();
61      }
62    }
63
64    public virtual int Length
65    {
66      get { return array.Length; }
67      #region Mono Compatibility
68      // this setter should be protected, but the Mono compiler couldn't handle it
69      set
70      {
71        if (ReadOnly) throw new NotSupportedException("Length cannot be set. ValueTypeArray is read-only.");
72        if (value != Length) {
73          Array.Resize<T>(ref array, value);
74          while (elementNames.Count > value)
75            elementNames.RemoveAt(elementNames.Count - 1);
76          OnElementNamesChanged();
77          OnReset();
78        }
79      }
80      #endregion
81    }
82    public virtual T this[int index]
83    {
84      get { return array[index]; }
85      set
86      {
87        if (ReadOnly) throw new NotSupportedException("Item cannot be set. ValueTypeArray is read-only.");
88        if (!value.Equals(array[index])) {
89          array[index] = value;
90          OnItemChanged(index);
91        }
92      }
93    }
94
95    [Storable]
96    protected bool readOnly;
97    public virtual bool ReadOnly
98    {
99      get { return readOnly; }
100    }
101
102    [StorableHook(HookType.AfterDeserialization)]
103    private void AfterDeserialization() {
104      if (elementNames == null) { elementNames = new List<string>(); }
105    }
106
107    [StorableConstructor]
108    protected ValueTypeArray(bool deserializing) : base(deserializing) { }
109    protected ValueTypeArray(ValueTypeArray<T> original, Cloner cloner)
110      : base(original, cloner) {
111      this.array = (T[])original.array.Clone();
112      this.readOnly = original.readOnly;
113      this.elementNames = new List<string>(original.elementNames);
114    }
115    protected ValueTypeArray() {
116      array = new T[0];
117      readOnly = false;
118      elementNames = new List<string>();
119    }
120    protected ValueTypeArray(int length) {
121      array = new T[length];
122      readOnly = false;
123      elementNames = new List<string>();
124    }
125    protected ValueTypeArray(T[] elements) {
126      if (elements == null) throw new ArgumentNullException();
127      array = (T[])elements.Clone();
128      readOnly = false;
129      elementNames = new List<string>();
130    }
131
132    public virtual ValueTypeArray<T> AsReadOnly() {
133      ValueTypeArray<T> readOnlyValueTypeArray = (ValueTypeArray<T>)this.Clone();
134      readOnlyValueTypeArray.readOnly = true;
135      return readOnlyValueTypeArray;
136    }
137
138    public override string ToString() {
139      if (array.Length == 0) return "[]";
140
141      StringBuilder sb = new StringBuilder();
142      sb.Append("[");
143      sb.Append(array[0].ToString());
144      for (int i = 1; i < array.Length; i++) {
145        sb.Append(";").Append(array[i].ToString());
146        if (sb.Length > maximumToStringLength) {
147          sb.Append("...");
148          break;
149        }
150      }
151      sb.Append("]");
152      return sb.ToString();
153    }
154
155    public virtual IEnumerator<T> GetEnumerator() {
156      return array.Cast<T>().GetEnumerator();
157    }
158
159    IEnumerator IEnumerable.GetEnumerator() {
160      return GetEnumerator();
161    }
162
163    public event EventHandler ElementNamesChanged;
164    protected virtual void OnElementNamesChanged() {
165      EventHandler handler = ElementNamesChanged;
166      if (handler != null)
167        handler(this, EventArgs.Empty);
168    }
169
170    public event EventHandler<EventArgs<int>> ItemChanged;
171    protected virtual void OnItemChanged(int index) {
172      if (ItemChanged != null)
173        ItemChanged(this, new EventArgs<int>(index));
174      if (index < maximumToStringLength)
175        OnToStringChanged();
176    }
177    public event EventHandler Reset;
178    protected virtual void OnReset() {
179      if (Reset != null)
180        Reset(this, EventArgs.Empty);
181      OnToStringChanged();
182    }
183  }
184}
Note: See TracBrowser for help on using the repository browser.