com.jamonapi.utils.BufferListDetailData Maven / Gradle / Ivy
package com.jamonapi.utils;
/** This class builds the returned array data based on info from header and first row.
*
* - If header has more columns than data then each data is padded with nulls.
*
- If first rows data has more columns than header then the header is padded with headers named 'colN' where N is the column number.
*
- If any rows of data have fewer columns than either the data in the first row or the header
* they are shrunken or grown as neccesary filling any excess columns with nulls, and truncating any excess columns.
*
*/
public class BufferListDetailData implements DetailData {
private String[] header;
private Object[][] data;
private Object[] firstRow;
private int colsInHeader;
private int colsInFirstRow;
public BufferListDetailData(BufferList bufferList) {
initalize(bufferList);
}
public String[] getHeader() {
return header;
}
public Object[][] getData() {
return data;
}
public int getRowCount() {
if (isEmpty())
return 0;
else
return data.length;
}
public boolean hasData() {
return !isEmpty();
}
public boolean isEmpty() {
return (data==null || data.length==0);
}
private void initalize(BufferList bufferList) {
/* object is value object.
* if header is bigger than data of first row then return header up to column width
* if header is smaller than data then PAD HEADER with colN
* if first row is smaller than subsequent rows then use colsize of first row and exclude other cols
* if first row is bigger than subsequent rows then pad row with nulls
*/
Object[] rows=getAllRows(bufferList);
if (rows!=null) {
firstRow=getRow(rows[0]);
colsInFirstRow=firstRow.length;
}
colsInHeader=(bufferList.getHeader()==null) ? 0 : bufferList.getHeader().length;
header=buildHeader(bufferList.getHeader());
data=buildData(rows);
}
private Object[] getAllRows(BufferList bufferList) {
Object[] bufferListArray=null;
synchronized(bufferList) {
if (bufferList.getRowCount()>0) {
bufferListArray=bufferList.getCollection().toArray();
}
}
return bufferListArray;
}
private String[] buildHeader(String[] h) {
// if header is bigger than data of first row then return header up to column width
// if header is smaller than data then PAD HEADER with colN
Object[] head=resize(h, getColCount());
if (head==null)
return null;
String[] headerStr=new String[head.length];
for (int i=0;isize) ? size : originalData.length;
for (int i=0;icolsInFirstRow) ? colsInHeader : colsInFirstRow;
}
}