001: /******************************************************************************
002: * Copyright (C) Lars Ivar Almli. All rights reserved. *
003: * ---------------------------------------------------------------------------*
004: * This file is part of MActor. *
005: * *
006: * MActor is free software; you can redistribute it and/or modify *
007: * it under the terms of the GNU General Public License as published by *
008: * the Free Software Foundation; either version 2 of the License, or *
009: * (at your option) any later version. *
010: * *
011: * MActor is distributed in the hope that it will be useful, *
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of *
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
014: * GNU General Public License for more details. *
015: * *
016: * You should have received a copy of the GNU General Public License *
017: * along with MActor; if not, write to the Free Software *
018: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *
019: ******************************************************************************/package org.mactor.framework;
020:
021: import java.util.ArrayList;
022: import java.util.HashMap;
023: import java.util.Iterator;
024: import java.util.LinkedList;
025: import java.util.List;
026: import java.util.Map;
027:
028: public class TestData {
029: private List<String> columns = new ArrayList<String>();
030: private List<DataRow> rows = new ArrayList<DataRow>();
031: private Map<String, Integer> columnIndexMap = new HashMap<String, Integer>();
032:
033: public void setColumns(List<String> columns) {
034: this .columns = new ArrayList<String>();
035: columnIndexMap.clear();
036: for (String col : columns) {
037: addColumn(col);
038: }
039: }
040:
041: public void addColumn(String column) {
042: columnIndexMap.put(column, new Integer(columns.size()));
043: columns.add(column);
044: }
045:
046: public int getColumnCount() {
047: return columns.size();
048: }
049:
050: public String getColumn(int index) {
051: return columns.get(index);
052: }
053:
054: public Map<String, String> getRowData(int rowIndex) {
055: Map<String, String> map = new HashMap<String, String>();
056: int colCount = getColumnCount();
057: for (int j = 0; j < colCount; j++) {
058: String col = getColumn(j);
059: map.put(col, getValue(rowIndex, col));
060: }
061: return map;
062: }
063:
064: public void addRow(List<String> cellValues) {
065: rows.add(new DataRow(cellValues));
066: }
067:
068: public int getRowCount() {
069: return rows.size();
070: }
071:
072: public String getValue(int row, String columnName) {
073: Integer index = columnIndexMap.get(columnName);
074: if (index == null)
075: return null;
076: return getValue(row, index.intValue());
077: }
078:
079: public String getValue(int row, int column) {
080: if (row >= rows.size())
081: return null;
082: return rows.get(row).getValue(column);
083: }
084:
085: public static class DataRow {
086: private List<String> values = new ArrayList<String>();
087:
088: public DataRow() {
089: }
090:
091: public DataRow(List<String> values) {
092: this .values = new ArrayList<String>(values);
093: }
094:
095: public String getValue(int colIndex) {
096: if (colIndex >= values.size())
097: return null;
098: return values.get(colIndex);
099: }
100: }
101:
102: public String toString() {
103: StringBuffer sb = new StringBuffer();
104: for (Iterator iter = columns.iterator(); iter.hasNext();) {
105: sb.append(iter.next()).append("\t");
106: }
107: sb.append("\n");
108: for (Iterator iter = rows.iterator(); iter.hasNext();) {
109: Iterator<String> valueIter = ((DataRow) iter.next()).values
110: .iterator();
111: for (; valueIter.hasNext();) {
112: sb.append(valueIter.next()).append("\t");
113: }
114: sb.append("\n");
115: }
116: return sb.toString();
117: }
118: }
|