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

com.actelion.research.util.datamodel.table.TableModelString Maven / Gradle / Ivy

There is a newer version: 2024.11.2
Show newest version
package com.actelion.research.util.datamodel.table;

import com.actelion.research.calc.Matrix;
import com.actelion.research.util.StringFunctions;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * TableModelText
 * Data model for an only text containing table
 * @author Modest von Korff
 * Apr 14, 2015 MvK Start implementation
 */
public class TableModelString {


	private static final String SEP_FIELD = "\t";
	private static final String SEP_LINE = "\n";

	private static final String SEP_FIELD_LATEX = " &\t";
	private static final String SEP_LINE_LATEX = " \\\\\n";

	private List liColName;

	private List liRowName;

	private List> liliData;

	private HashMap hmRowName_Index;
	private HashMap hmColName_Index;


	/**
	 * 
	 */
	public TableModelString() {
		init();
	}

	public TableModelString(int rows, int cols) {
		init();
		init(rows, cols);
	}

	public TableModelString(List liRowName, List liColName) {
		init();
		init(liRowName.size(), liColName.size());
		for (int i = 0; i < liRowName.size(); i++) {
			setRowName(i, liRowName.get(i));
		}
		for (int i = 0; i < liColName.size(); i++) {
			setColName(i, liColName.get(i));
		}
	}

	public boolean containsColumn(String name){
		return (hmColName_Index.get(name)!=null)?true:false;
	}

	private void init(){
		liColName = new ArrayList<>();

		liRowName = new ArrayList<>();

		liliData = new ArrayList<>();

		hmRowName_Index = new HashMap<>();
		hmColName_Index = new HashMap<>();
	}

	private void init(int rows, int cols) {

		liliData.clear();

		liColName.clear();
		liRowName.clear();

		hmRowName_Index.clear();
		hmColName_Index.clear();

		for (int i = 0; i < rows; i++) {
			List li = new ArrayList<>();

			for (int j = 0; j < cols; j++) {
				li.add(null);
			}
			liliData.add(li);

			liRowName.add("");
		}

		for (int i = 0; i < cols; i++) {
			liColName.add("");
		}
	}

	public String getColName(int col) {
		return liColName.get(col);
	}

	public void setColName(int col, String s) {
		liColName.set(col, s);
		hmColName_Index.put(s, col);

	}

	public String getRowName(int row) {
		return liRowName.get(row);
	}

	public void setRowName(int row, String s) {
		liRowName.set(row, s);
		hmRowName_Index.put(s, row);
	}

	public void set(int row, int col, String s) {
		liliData.get(row).set(col, s);
	}

	public void set(Matrix m, int digits) {

		int r = liRowName.size();
		int c = liColName.size();

		String sFormat="";
		if(digits > 0)
			sFormat += ".";

		for (int i = 0; i < digits; i++) {
			sFormat += "0";
		}

		DecimalFormat nf = new DecimalFormat(sFormat);

		for (int i = 0; i < r; i++) {

			for (int j = 0; j < c; j++) {
				set(i,j,nf.format(m.get(i,j)));
			}
		}
	}

	public void set(String rowName, String colName, String s) {
		int r = hmRowName_Index.get(rowName);
		int c = hmColName_Index.get(colName);
		liliData.get(r).set(c, s);
	}

	public String get(int row, int col) {
		return liliData.get(row).get(col);
	}
	
	
	public int getRows(){
		return liliData.size();
	}
	
	public int getCols(){
		
		if(getRows()==0) return 0;
		
		return liliData.get(0).size();
	}


	@Override
	public String toString() {

		final StringBuilder sb = new StringBuilder();

		int [] arrLengthMax = new int [liColName.size()];

		int lengthMaxRowName = 0;

		for (int i = 0; i < liRowName.size(); i++) {
			lengthMaxRowName = Math.max(lengthMaxRowName, liRowName.get(i).length());
		}

		for (int i = 0; i < liColName.size(); i++) {
			arrLengthMax[i]=Math.max(arrLengthMax[i],liColName.get(i).length());
			for (int j = 0; j < liRowName.size(); j++) {
				String s = get(j,i);
				if(s!=null){
					arrLengthMax[i]=Math.max(arrLengthMax[i],s.length());
				} else {
					System.out.println("TableModelString toString() no value for field: row ["+j+"] " + liRowName.get(j) + ", and col ["+i+"] " + liColName.get(i) + ".");
				}
			}
		}

		// Upper left corner is empty
		for (int i = 0; i < lengthMaxRowName; i++) {
			sb.append(" ");
		}

		sb.append(SEP_FIELD);

		// Column names
		for (int i = 0; i < liColName.size(); i++) {

			String name = liColName.get(i);

			int n = arrLengthMax[i] - name.length();

			for (int j = 0; j < n; j++) {
				sb.append(" ");
			}

			sb.append(name);

			if(i liData = liliData.get(i);

			if(liData!=null) {
				for (int j = 0; j < liData.size(); j++) {

					String str = liData.get(j);
					if(str!=null)
						bw.write(str);
					if (i < liData.size() - 1) {
						bw.write(SEP_FIELD);
					}
				}
			}

			if (i < liRowName.size() - 1) {
				bw.write(SEP_LINE);
			}
		}

		bw.close();

	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy