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