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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections;
|
---|
24 | using System.Text;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
28 |
|
---|
29 | namespace HeuristicLab.Data {
|
---|
30 | [Item("StringArrayData", "Represents an array of strings.")]
|
---|
31 | [Creatable("Test")]
|
---|
32 | public sealed class StringArrayData : Item, IEnumerable, IStringConvertibleArrayData {
|
---|
33 | [Storable]
|
---|
34 | private string[] array;
|
---|
35 |
|
---|
36 | public int Length {
|
---|
37 | get { return array.Length; }
|
---|
38 | private set {
|
---|
39 | if (value != Length) {
|
---|
40 | Array.Resize<string>(ref array, value);
|
---|
41 | OnReset();
|
---|
42 | }
|
---|
43 | }
|
---|
44 | }
|
---|
45 | public string this[int index] {
|
---|
46 | get { return array[index]; }
|
---|
47 | set {
|
---|
48 | if (value != array[index]) {
|
---|
49 | if ((value != null) || (array[index] != string.Empty)) {
|
---|
50 | array[index] = value != null ? value : string.Empty;
|
---|
51 | OnItemChanged(index);
|
---|
52 | }
|
---|
53 | }
|
---|
54 | }
|
---|
55 | }
|
---|
56 |
|
---|
57 | public StringArrayData() {
|
---|
58 | array = new string[0];
|
---|
59 | }
|
---|
60 | public StringArrayData(int length) {
|
---|
61 | array = new string[length];
|
---|
62 | for (int i = 0; i < array.Length; i++)
|
---|
63 | array[i] = string.Empty;
|
---|
64 | }
|
---|
65 | public StringArrayData(string[] elements) {
|
---|
66 | if (elements == null) throw new ArgumentNullException();
|
---|
67 | array = new string[elements.Length];
|
---|
68 | for (int i = 0; i < array.Length; i++)
|
---|
69 | array[i] = elements[i] == null ? string.Empty : elements[i];
|
---|
70 | }
|
---|
71 | private StringArrayData(StringArrayData elements) {
|
---|
72 | if (elements == null) throw new ArgumentNullException();
|
---|
73 | array = (string[])elements.array.Clone();
|
---|
74 | }
|
---|
75 |
|
---|
76 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
77 | StringArrayData clone = new StringArrayData(this);
|
---|
78 | cloner.RegisterClonedObject(this, clone);
|
---|
79 | return clone;
|
---|
80 | }
|
---|
81 |
|
---|
82 | public override string ToString() {
|
---|
83 | StringBuilder sb = new StringBuilder();
|
---|
84 | sb.Append("[");
|
---|
85 | if (array.Length > 0) {
|
---|
86 | sb.Append(array[0]);
|
---|
87 | for (int i = 1; i < array.Length; i++)
|
---|
88 | sb.Append(";").Append(array[i]);
|
---|
89 | }
|
---|
90 | sb.Append("]");
|
---|
91 | return sb.ToString();
|
---|
92 | }
|
---|
93 |
|
---|
94 | public IEnumerator GetEnumerator() {
|
---|
95 | return array.GetEnumerator();
|
---|
96 | }
|
---|
97 |
|
---|
98 | #region IStringConvertibleArrayData Members
|
---|
99 | int IStringConvertibleArrayData.Rows {
|
---|
100 | get { return Length; }
|
---|
101 | set { Length = value; }
|
---|
102 | }
|
---|
103 |
|
---|
104 | bool IStringConvertibleArrayData.Validate(string value, out string errorMessage) {
|
---|
105 | if (value == null) {
|
---|
106 | errorMessage = "Invalid Value (string must not be null)";
|
---|
107 | return false;
|
---|
108 | } else {
|
---|
109 | errorMessage = string.Empty;
|
---|
110 | return true;
|
---|
111 | }
|
---|
112 | }
|
---|
113 | string IStringConvertibleArrayData.GetValue(int index) {
|
---|
114 | return this[index];
|
---|
115 | }
|
---|
116 | bool IStringConvertibleArrayData.SetValue(string value, int index) {
|
---|
117 | if (value != null) {
|
---|
118 | this[index] = value;
|
---|
119 | return true;
|
---|
120 | } else {
|
---|
121 | return false;
|
---|
122 | }
|
---|
123 | }
|
---|
124 | public event EventHandler<EventArgs<int>> ItemChanged;
|
---|
125 | private void OnItemChanged(int index) {
|
---|
126 | if (ItemChanged != null)
|
---|
127 | ItemChanged(this, new EventArgs<int>(index));
|
---|
128 | OnToStringChanged();
|
---|
129 | }
|
---|
130 | public event EventHandler Reset;
|
---|
131 | private void OnReset() {
|
---|
132 | if (Reset != null)
|
---|
133 | Reset(this, EventArgs.Empty);
|
---|
134 | OnToStringChanged();
|
---|
135 | }
|
---|
136 | #endregion
|
---|
137 | }
|
---|
138 | }
|
---|