refactor table

refactor table
This commit is contained in:
coderfengyun 2014-05-22 17:15:55 +08:00
parent 0facf86973
commit 04cf8b5273
1 changed files with 70 additions and 9 deletions

View File

@ -1,28 +1,43 @@
package org.bench4q.agent.utils.types;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.bench4q.agent.utils.ParameterConstant;
import org.bench4q.agent.utils.ParameterParser;
import org.bench4q.agent.utils.Type;
import org.bench4q.share.exception.Bench4QRunTimeException;
public class Table extends Type {
public static class Row {
private List<String> cells = new ArrayList<String>();
private List<String> columnLabels = new ArrayList<String>();
public List<String> getCells() {
return cells;
}
private void setCells(List<String> cells) {
this.cells = cells;
public List<String> getColumnLabels() {
return this.columnLabels;
}
public Row(List<String> cells) {
this.setCells(cells);
public Row(List<String> cells, List<String> columnLabels) {
if (cells.size() != columnLabels.size()) {
throw new RuntimeException(
"cell size in a row does not equals to column size");
}
this.cells = cells;
this.columnLabels = columnLabels;
}
public Row(Map<String, String> keyValues) {
for (Entry<String, String> entry : keyValues.entrySet()) {
this.columnLabels.add(entry.getKey());
this.cells.add(entry.getValue());
}
}
public int cellCount() {
@ -35,6 +50,29 @@ public class Table extends Type {
}
return this.getCells().get(index);
}
public String getCell(String label) {
for (int i = 0; i < this.columnLabels.size(); i++) {
if (this.columnLabels.get(i).equalsIgnoreCase(label)) {
return this.cells.get(i);
}
}
throw new Bench4QRunTimeException("Row doesn't has label : "
+ label);
}
@Override
public String toString() {
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < getCells().size(); i++) {
stringBuilder.append(this.columnLabels.get(i));
stringBuilder.append("=");
stringBuilder.append(this.getCells().get(i));
stringBuilder.append(COLUMN_SEPARATOR);
}
stringBuilder.append(ROW_SEPARATOR_TAIL);
return stringBuilder.toString();
}
}
public static final String ROW_SEPARATOR = "|;";
@ -79,9 +117,6 @@ public class Table extends Type {
}
public Row getRow(int index) {
if (index >= this.getRows().size()) {
return new Row(Collections.<String> emptyList());
}
return this.getRows().get(index);
}
@ -92,11 +127,27 @@ public class Table extends Type {
List<List<String>> realContent = buildTableContent(value);
result.setRealContent(realContent);
for (List<String> tableRow : realContent) {
result.getRows().add(new Row(tableRow));
result.getRows().add(new Row(tableRow, columnLablesInSequence));
}
return result;
}
public static Table buildWith(Map<String, String> keyValues,
List<String> columnLabels) {
if (columnLabels.size() != 2) {
throw new Bench4QRunTimeException("column label size is not two!");
}
StringBuilder builder = new StringBuilder();
for (Entry<String, String> entry : keyValues.entrySet()) {
builder.append(columnLabels.get(0) + "=");
builder.append(entry.getKey());
builder.append("|" + columnLabels.get(1) + "=");
builder.append(entry.getValue());
builder.append("|;");
}
return Table.buildTable(builder.toString(), columnLabels);
}
/**
* This method analyzes the value to be set in the table, and remove all
* escape characters
@ -163,4 +214,14 @@ public class Table extends Type {
public int getColumnCount() {
return this.getColumnLables().size();
}
@Override
public String toString() {
StringBuilder stringBuilder = new StringBuilder();
for (Row row : this.getRows()) {
stringBuilder.append(row.toString());
}
return stringBuilder.toString();
}
}