/**
 * Represents one record read from a csv formatted file
 * 
 * @author pdegenne@teledetection.fr
 */
public class Csvrecord {

	private final String ERR_HEADER = "Datafacer Csvfile: ";
	private String[] cols;

	/**
	 * Creates a new Csvrecord by parsing the line given in argument
	 * 
	 * @param newline
	 *            A String with each field separated by a ';' character
	 */
	public Csvrecord(String newline) {
		this.cols = newline.split(";");
	}

	/**
	 * Obtains the value from one column of the record
	 * 
	 * @param colNumber
	 *            Index of the column to read (first is #0)
	 * @return The value of the column in text type
	 */
	public String getColumnAsText(int colNumber) {
		String result = "";
		try {
			result = cols[colNumber];
		} catch (ArrayIndexOutOfBoundsException aiobe) {
			System.out.println(ERR_HEADER
					+ "Impossible to reach column number " + colNumber
					+ ". The number of available columns is " + cols.length
					+ ".");
		}
		return result;
	}

	/**
	 * Obtains the value from one column of the record
	 * 
	 * @param colNumber
	 *            Index of the column to read (first is #0)
	 * @return The value of the column in int type
	 */
	public int getColumnAsInt(int colNumber) {
		int result = 0;
		try {
			result = new Integer(cols[colNumber]).intValue();
		} catch (ArrayIndexOutOfBoundsException aiobe) {
			System.out.println(ERR_HEADER
					+ "Impossible to reach column number " + colNumber
					+ ". The number of available columns is " + cols.length
					+ ".");
		} catch (NumberFormatException nfe) {
			System.out.println(ERR_HEADER
					+ "Impossible de convertir la colonne " + colNumber
					+ " en int. Texte trouvé dans cette colonne :"
					+ cols[colNumber]);
		}
		return result;
	}

	/**
	 * Obtains the value from one column of the record
	 * 
	 * @param colNumber
	 *            Index of the column to read (first is #0)
	 * @return The value of the column in real type
	 */
	public double getColumnAsReal(int colNumber) {
		double result = 0;
		try {
			result = new Double(cols[colNumber]).doubleValue();
		} catch (ArrayIndexOutOfBoundsException aiobe) {
			System.out.println(ERR_HEADER
					+ "Impossible to reach column number " + colNumber
					+ ". The number of available columns is " + cols.length
					+ ".");
		} catch (NumberFormatException nfe) {
			System.out.println(ERR_HEADER
					+ "Impossible de convertir la colonne " + colNumber
					+ " en real. Texte trouvé dans cette colonne :"
					+ cols[colNumber]);
		}
		return result;
	}

	/**
	 * Obtains the value from one column of the record
	 * 
	 * @param colNumber
	 *            Index of the column to read (first is #0)
	 * @return The value of the column in boolean type
	 */
	public boolean getColumnAsBoolean(int colNumber) {
		boolean result = false;
		try {
			result = new Boolean(cols[colNumber]).booleanValue();
		} catch (ArrayIndexOutOfBoundsException aiobe) {
			System.out.println(ERR_HEADER
					+ "Impossible to reach column number " + colNumber
					+ ". The number of available columns is " + cols.length
					+ ".");
		}
		return result;
	}

}
