Free cookie consent management tool by TermsFeed Policy Generator

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

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

Removed Creatable test attribute (#935).

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