001: /*
002: * Created on Sep 28, 2004
003: *
004: */
005: package org.jmatlab.linalg;
006:
007: import java.io.Serializable;
008: import org.jmatlab.semantic.Symbol;
009:
010: /**
011: * @author MQWF
012: *
013: */
014: public class DefaultCellImpl implements ICell, Serializable {
015:
016: private int rows = 1;
017: private int cols = 1;
018:
019: private Symbol[][] cells = new Symbol[1][1];
020:
021: public DefaultCellImpl() {
022: }
023:
024: public DefaultCellImpl(Symbol symbol) {
025: cells[0][0] = symbol;
026: }
027:
028: public void setCell(Symbol symbol) {
029: cells[0][0] = symbol;
030: }
031:
032: public void setCell(int n, Symbol symbol) {
033: try {
034: cells[0][n - 1] = symbol;
035:
036: } catch (Exception e) {
037: Symbol[][] temp = new Symbol[1][n];
038: for (int j = 0; j < cols; j++) {
039: temp[0][j] = cells[0][j];
040: }
041: temp[0][n - 1] = symbol;
042: this .cells = temp;
043: this .cols = n;
044: }
045: }
046:
047: public void setCell(int m, int n, Symbol symbol) {
048: try {
049: cells[m - 1][n - 1] = symbol;
050:
051: } catch (Exception e) {
052: if (m > rows && n > cols) {
053: Symbol[][] temp = new Symbol[m][n];
054: for (int i = 0; i < rows; i++) {
055: for (int j = 0; j < cols; j++) {
056: temp[i][j] = cells[i][j];
057: }
058: }
059: temp[m - 1][n - 1] = symbol;
060: this .cells = temp;
061: this .rows = m;
062: this .cols = n;
063: } else if (m > rows) {
064: Symbol[][] temp = new Symbol[m][cols];
065: for (int i = 0; i < rows; i++) {
066: for (int j = 0; j < cols; j++) {
067: temp[i][j] = cells[i][j];
068: }
069: }
070: temp[m - 1][n - 1] = symbol;
071: this .cells = temp;
072: this .rows = m;
073: } else if (n > cols) {
074: Symbol[][] temp = new Symbol[rows][n];
075: for (int i = 0; i < rows; i++) {
076: for (int j = 0; j < cols; j++) {
077: temp[i][j] = cells[i][j];
078: }
079: }
080: temp[m - 1][n - 1] = symbol;
081: this .cells = temp;
082: this .cols = n;
083: }
084:
085: }
086: }
087:
088: public Symbol getCell() {
089: return cells[0][0];
090: }
091:
092: public Symbol getCell(int n) {
093: return cells[0][n - 1];
094: }
095:
096: // public Symbol getCellFixSymbol(int m, int n) {
097: // Symbol symbol = getCell(m, n).getCell().getCell();
098: // return symbol;
099: // }
100: //
101: public Symbol getCell(int m, int n) {
102: return cells[m - 1][n - 1];
103: }
104:
105: public ICell getCells(IMatrix m) {
106: int size = m.getCols();
107: ICell rtn = new DefaultCellImpl();
108: for (int i = 1; i <= size; i++) {
109: rtn.setCell(i, this .getCell(i));
110: }
111: return rtn;
112: }
113:
114: public Symbol getCell(String name) {
115: for (int i = 0; i < rows; i++) {
116: for (int j = 0; j < cols; j++) {
117: Symbol symbol = cells[i][j];
118: if (name.equalsIgnoreCase(symbol.getName())) {
119: return symbol;
120: }
121: }
122: }
123: return null;
124: }
125:
126: public String toString() {
127: StringBuffer buffer = new StringBuffer();
128: for (int i = 0; i < rows; i++) {
129: for (int j = 0; j < cols; j++) {
130: if (rows == 1) {
131: buffer.append("Cell(i = " + (j + 1) + ")");
132: } else {
133: buffer.append("Cell(i = " + (i + 1) + ", j = "
134: + (j + 1) + ")");
135: }
136: buffer.append('\n');
137: Symbol sym = cells[i][j];
138: if (sym == null) {
139: buffer.append("Empty");
140: buffer.append('\n');
141: continue;
142: }
143: // sym = sym.getCell().getCell();
144: buffer.append(sym.toString());
145: buffer.append('\n');
146: }
147: }
148: return buffer.toString();
149: }
150:
151: public static void main(String[] args) {
152: }
153: }
|