All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.xxdb.data.AbstractMatrix Maven / Gradle / Ivy

Go to download

The messaging and data conversion protocol between Java and DolphinDB server

There is a newer version: 1.0.27
Show newest version
package com.xxdb.data;

import java.io.IOException;

import com.xxdb.io.ExtendedDataInput;
import com.xxdb.io.ExtendedDataOutput;

public abstract class AbstractMatrix extends AbstractEntity implements Matrix{
	private Vector rowLabels = null;
	private Vector columnLabels = null;
	protected int rows;
	protected int columns;
	protected static final int BUF_SIZE = 4096;
	protected byte[] buf = new byte[BUF_SIZE];
	
	protected abstract void readMatrixFromInputStream(int rows, int columns, ExtendedDataInput in) throws IOException;
	protected abstract void writeVectorToOutputStream(ExtendedDataOutput out) throws IOException;
	
	protected AbstractMatrix(int rows, int columns){
		this.rows = rows;
		this.columns = columns;
	}
	
	protected AbstractMatrix(ExtendedDataInput in) throws IOException{
		byte hasLabels = in.readByte();
		
		BasicEntityFactory factory = null;
		DATA_TYPE[] types = DATA_TYPE.values();
		if(hasLabels > 0)
			factory = new BasicEntityFactory();
		
		if((hasLabels & 1) == 1){
			//contain row labels
			short flag = in.readShort();
			int form = flag>>8;
			int type = flag & 0xff;
			if(form != DATA_FORM.DF_VECTOR.ordinal())
				throw new IOException("The form of matrix row labels must be vector");
			if(type <0 || type >= types.length)
				throw new IOException("Invalid data type for matrix row labels: " + type);
			rowLabels = (Vector)factory.createEntity(DATA_FORM.DF_VECTOR, types[type], in);
		}
		
		if((hasLabels & 2) == 2){
			//contain columns labels
			short flag = in.readShort();
			int form = flag>>8;
			int type = flag & 0xff;
			if(form != DATA_FORM.DF_VECTOR.ordinal())
				throw new IOException("The form of matrix columns labels must be vector");
			if(type <0 || type >= types.length)
				throw new IOException("Invalid data type for matrix column labels: " + type);
			columnLabels = (Vector)factory.createEntity(DATA_FORM.DF_VECTOR, types[type], in);
		}
		
		short flag = in.readShort();
		int type = flag & 0xff;
		if(type <0 || type >= types.length)
			throw new IOException("Invalid data type for matrix: " + type);
		rows = in.readInt();
		columns = in.readInt(); 
		readMatrixFromInputStream(rows, columns, in);
	}
	
	protected int getIndex(int row, int column){
		return column * rows + row;
	}
	
	public boolean hasRowLabel(){
		return rowLabels != null;
	}
	
	public boolean hasColumnLabel(){
		return columnLabels != null;
	}
	
	public Scalar getRowLabel(int index){
		return rowLabels.get(index);
	}
	
	public Scalar getColumnLabel(int index){
		return columnLabels.get(index);
	}
	
	public Vector getRowLabels() { 
		return rowLabels;
	}
	
	public void setRowLabels(Vector vector){
		if(vector.rows() != rows)
			throw new IllegalArgumentException("the row label size doesn't equal to the row number of the matrix.");
		rowLabels = vector;
	}
	
	public Vector getColumnLabels(){
		return columnLabels;
	}
	
	public void setColumnLabels(Vector vector){
		if(vector.rows() != columns)
			throw new IllegalArgumentException("the column label size doesn't equal to the column number of the matrix.");
		columnLabels = vector;
	}
	
	public String getString(){
		int rows = Math.min(Utils.DISPLAY_ROWS,rows());
		int limitColMaxWidth=25;
	    int length=0;
	    int curCol=0;
	    int maxColWidth;
		StringBuilder[] list = new StringBuilder[rows+1];
		String[] listTmp = new String[rows+1];
	    int i,curSize;

	    for(i=0; imaxColWidth)
					maxColWidth=listTmp[i+1].length();
			}
			maxColWidth++;
			for(i=0;i<=rows;i++){
				curSize=listTmp[i].length();
				if(curSize<=maxColWidth){
					list[i].append(listTmp[i]);
					if(curSize3)
						list[i].append(listTmp[i].substring(0,maxColWidth-3));
					list[i].append("...");
				}
			}
			length+=maxColWidth;
	    }

	    while(lengthmaxColWidth)
	    			maxColWidth=listTmp[i+1].length();
	    	}
	    	if(maxColWidth>limitColMaxWidth)
	    		maxColWidth=limitColMaxWidth;
	    	if((int)listTmp[0].length()>maxColWidth)
	    		maxColWidth=Math.min(limitColMaxWidth, listTmp[0].length());
	    	if(curColUtils.DISPLAY_WIDTH && curCol+13)
	    				list[i].append(listTmp[i].substring(0,maxColWidth-3));
	    			list[i].append("...");
	    		}
	    	}
	    	length+=maxColWidth;
	    	curCol++;
	    }

	    if(curCol




© 2015 - 2024 Weber Informatics LLC | Privacy Policy