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