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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections;
|
---|
24 | using System.Collections.Generic;
|
---|
25 | using System.Drawing;
|
---|
26 | using System.Linq;
|
---|
27 | using System.Text;
|
---|
28 | using HeuristicLab.Common;
|
---|
29 | using HeuristicLab.Core;
|
---|
30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
31 |
|
---|
32 | namespace HeuristicLab.Data {
|
---|
33 | [Item("StringArray", "Represents an array of strings.")]
|
---|
34 | [StorableClass]
|
---|
35 | public class StringArray : Item, IEnumerable<string>, IStringConvertibleArray {
|
---|
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 string[] 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 | protected set
|
---|
68 | {
|
---|
69 | if (ReadOnly) throw new NotSupportedException("Length cannot be set. StringArray is read-only.");
|
---|
70 | if (value != Length) {
|
---|
71 | Array.Resize<string>(ref array, value);
|
---|
72 | while (elementNames.Count > value)
|
---|
73 | elementNames.RemoveAt(elementNames.Count - 1);
|
---|
74 | OnElementNamesChanged();
|
---|
75 | OnReset();
|
---|
76 | }
|
---|
77 | }
|
---|
78 | }
|
---|
79 | public virtual string this[int index]
|
---|
80 | {
|
---|
81 | get { return array[index]; }
|
---|
82 | set
|
---|
83 | {
|
---|
84 | if (ReadOnly) throw new NotSupportedException("Item cannot be set. StringArray is read-only.");
|
---|
85 | if (value != array[index]) {
|
---|
86 | if ((value != null) || (array[index] != string.Empty)) {
|
---|
87 | array[index] = value != null ? value : string.Empty;
|
---|
88 | OnItemChanged(index);
|
---|
89 | }
|
---|
90 | }
|
---|
91 | }
|
---|
92 | }
|
---|
93 |
|
---|
94 | [Storable]
|
---|
95 | protected bool readOnly;
|
---|
96 | public virtual bool ReadOnly
|
---|
97 | {
|
---|
98 | get { return readOnly; }
|
---|
99 | }
|
---|
100 |
|
---|
101 | [StorableHook(HookType.AfterDeserialization)]
|
---|
102 | private void AfterDeserialization() {
|
---|
103 | if (elementNames == null) { elementNames = new List<string>(); }
|
---|
104 | }
|
---|
105 |
|
---|
106 | [StorableConstructor]
|
---|
107 | protected StringArray(bool deserializing) : base(deserializing) { }
|
---|
108 | protected StringArray(StringArray original, Cloner cloner)
|
---|
109 | : base(original, cloner) {
|
---|
110 | this.array = (string[])original.array.Clone();
|
---|
111 | this.readOnly = original.readOnly;
|
---|
112 | this.elementNames = new List<string>(original.elementNames);
|
---|
113 | }
|
---|
114 | public StringArray() {
|
---|
115 | array = new string[0];
|
---|
116 | readOnly = false;
|
---|
117 | elementNames = new List<string>();
|
---|
118 | }
|
---|
119 | public StringArray(int length) {
|
---|
120 | array = new string[length];
|
---|
121 | for (int i = 0; i < array.Length; i++)
|
---|
122 | array[i] = string.Empty;
|
---|
123 | readOnly = false;
|
---|
124 | elementNames = new List<string>();
|
---|
125 | }
|
---|
126 | public StringArray(string[] elements) {
|
---|
127 | if (elements == null) throw new ArgumentNullException();
|
---|
128 | array = new string[elements.Length];
|
---|
129 | for (int i = 0; i < array.Length; i++)
|
---|
130 | array[i] = elements[i] == null ? string.Empty : elements[i];
|
---|
131 | readOnly = false;
|
---|
132 | elementNames = new List<string>();
|
---|
133 | }
|
---|
134 |
|
---|
135 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
136 | return new StringArray(this, cloner);
|
---|
137 | }
|
---|
138 |
|
---|
139 | public virtual StringArray AsReadOnly() {
|
---|
140 | StringArray readOnlyStringArray = (StringArray)this.Clone();
|
---|
141 | readOnlyStringArray.readOnly = true;
|
---|
142 | return readOnlyStringArray;
|
---|
143 | }
|
---|
144 |
|
---|
145 | public override string ToString() {
|
---|
146 | if (array.Length == 0) return "[]";
|
---|
147 |
|
---|
148 | StringBuilder sb = new StringBuilder();
|
---|
149 | sb.Append("[");
|
---|
150 | sb.Append(array[0]);
|
---|
151 | for (int i = 1; i < array.Length; i++) {
|
---|
152 | sb.Append(";").Append(array[i]);
|
---|
153 | if (sb.Length > maximumToStringLength) {
|
---|
154 | sb.Append("...");
|
---|
155 | break;
|
---|
156 | }
|
---|
157 | }
|
---|
158 | sb.Append("]");
|
---|
159 | return sb.ToString();
|
---|
160 | }
|
---|
161 |
|
---|
162 | public virtual IEnumerator<string> GetEnumerator() {
|
---|
163 | return array.Cast<string>().GetEnumerator();
|
---|
164 | }
|
---|
165 |
|
---|
166 | IEnumerator IEnumerable.GetEnumerator() {
|
---|
167 | return GetEnumerator();
|
---|
168 | }
|
---|
169 |
|
---|
170 | protected virtual bool Validate(string value, out string errorMessage) {
|
---|
171 | if (value == null) {
|
---|
172 | errorMessage = "Invalid Value (string must not be null)";
|
---|
173 | return false;
|
---|
174 | } else {
|
---|
175 | errorMessage = string.Empty;
|
---|
176 | return true;
|
---|
177 | }
|
---|
178 | }
|
---|
179 | protected virtual string GetValue(int index) {
|
---|
180 | return this[index];
|
---|
181 | }
|
---|
182 | protected virtual bool SetValue(string value, int index) {
|
---|
183 | if (value != null) {
|
---|
184 | this[index] = value;
|
---|
185 | return true;
|
---|
186 | } else {
|
---|
187 | return false;
|
---|
188 | }
|
---|
189 | }
|
---|
190 |
|
---|
191 | public event EventHandler ElementNamesChanged;
|
---|
192 | protected virtual void OnElementNamesChanged() {
|
---|
193 | EventHandler handler = ElementNamesChanged;
|
---|
194 | if (handler != null)
|
---|
195 | handler(this, EventArgs.Empty);
|
---|
196 | }
|
---|
197 |
|
---|
198 | public event EventHandler<EventArgs<int>> ItemChanged;
|
---|
199 | protected virtual void OnItemChanged(int index) {
|
---|
200 | if (ItemChanged != null)
|
---|
201 | ItemChanged(this, new EventArgs<int>(index));
|
---|
202 | if (index < maximumToStringLength)
|
---|
203 | OnToStringChanged();
|
---|
204 | }
|
---|
205 | public event EventHandler Reset;
|
---|
206 | protected virtual void OnReset() {
|
---|
207 | if (Reset != null)
|
---|
208 | Reset(this, EventArgs.Empty);
|
---|
209 | OnToStringChanged();
|
---|
210 | }
|
---|
211 |
|
---|
212 | #region IStringConvertibleArray Members
|
---|
213 | int IStringConvertibleArray.Length
|
---|
214 | {
|
---|
215 | get { return Length; }
|
---|
216 | set { Length = value; }
|
---|
217 | }
|
---|
218 | bool IStringConvertibleArray.Validate(string value, out string errorMessage) {
|
---|
219 | return Validate(value, out errorMessage);
|
---|
220 | }
|
---|
221 | string IStringConvertibleArray.GetValue(int index) {
|
---|
222 | return GetValue(index);
|
---|
223 | }
|
---|
224 | bool IStringConvertibleArray.SetValue(string value, int index) {
|
---|
225 | return SetValue(value, index);
|
---|
226 | }
|
---|
227 | #endregion
|
---|
228 | }
|
---|
229 | }
|
---|