Free cookie consent management tool by TermsFeed Policy Generator

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

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

Refactored classes of HeuristicLab.Data and implemented encoding specific data classes (#909)

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