Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Data/3.3/StringMatrix.cs @ 3047

Last change on this file since 3047 was 3047, checked in by swagner, 14 years ago

Renamed files of HeuristicLab.Data (#909)

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