com.xxdb.data.BasicTable Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of api-java Show documentation
Show all versions of api-java Show documentation
The messaging and data conversion protocol between Java and DolphinDB server
package com.xxdb.data;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.xxdb.io.ExtendedDataInput;
import com.xxdb.io.ExtendedDataOutput;
/**
*
* Corresponds to DolphinDB table object
*
*/
public class BasicTable extends AbstractEntity implements Table{
private List columns_ = new ArrayList();
private List names_ = new ArrayList();
private Map name2index_ = new HashMap();
public BasicTable(ExtendedDataInput in) throws IOException{
int rows = in.readInt();
int cols = in.readInt();
String tableName = in.readString();
//read column names
for(int i=0; i>8;
int type = flag & 0xff;
DATA_FORM df = DATA_FORM.values()[form];
DATA_TYPE dt = DATA_TYPE.values()[type];
if(df != DATA_FORM.DF_VECTOR)
throw new IOException("Invalid form for column [" + names_.get(i) + "] for table " + tableName);
Vector vector = (Vector)factory.createEntity(df, dt, in);
if(vector.rows() != rows && vector.rows()!= 1)
throw new IOException("The number of rows for column " +names_.get(i) + " is not consistent with other columns");
columns_.add(vector);
}
}
public BasicTable(final List colNames, final List cols) {
this.setColName(colNames);
this.setColumns(cols);
}
public void setColName (final List colNames) {
names_.clear();
name2index_.clear();
for (String name : colNames){
names_.add(name);
name2index_.put(name, name2index_.size());
}
}
public void setColumns (final List cols) {
columns_.clear();
// this is a shallow copy!
for (Vector vector : cols)
columns_.add(vector);
}
@Override
public DATA_CATEGORY getDataCategory() {
return DATA_CATEGORY.MIXED;
}
@Override
public DATA_TYPE getDataType() {
return DATA_TYPE.DT_DICTIONARY;
}
@Override
public DATA_FORM getDataForm() {
return DATA_FORM.DF_TABLE;
}
@Override
public int rows() {
if(columns()<=0)
return 0;
else
return columns_.get(0).rows();
}
@Override
public int columns() {
return columns_.size();
}
@Override
public Vector getColumn(int index) {
return columns_.get(index);
}
@Override
public Vector getColumn(String name) {
Integer index = name2index_.get(name);
if(index == null)
return null;
else
return getColumn(index);
}
public String getColumnName(int index){
return names_.get(index);
}
public String getString(){
int rows = Math.min(Utils.DISPLAY_ROWS,rows());
int strColMaxWidth = Utils.DISPLAY_WIDTH/Math.min(columns(),Utils.DISPLAY_COLS)+5;
int length=0;
int curCol=0;
int maxColWidth;
StringBuilder[] list = new StringBuilder[rows+1];
StringBuilder separator = new StringBuilder();
String[] listTmp = new String[rows+1];
int i,curSize;
for(i=0; imaxColWidth)
maxColWidth=listTmp[i+1].length();
}
if(maxColWidth>strColMaxWidth && getColumn(curCol).getDataCategory()==DATA_CATEGORY.LITERAL)
maxColWidth=strColMaxWidth;
if((int)listTmp[0].length()>maxColWidth)
maxColWidth=Math.min(strColMaxWidth,(int)listTmp[0].length());
if(length+maxColWidth>Utils.DISPLAY_WIDTH && curCol+13)
list[i].append(listTmp[i].substring(0,maxColWidth-3));
list[i].append("...");
separator.append("---");
}
}
length+=maxColWidth;
curCol++;
}
if(curCol