/* * RapidMiner * * Copyright (C) 2001-2008 by Rapid-I and the contributors * * Complete list of developers available at our web site: * * http://rapid-i.com * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see http://www.gnu.org/licenses/. */ package com.rapidminer.datatable; import java.io.IOException; import java.io.PrintWriter; import java.util.Date; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import com.rapidminer.tools.Tableable; import com.rapidminer.tools.Tools; /** * This abstract data table implementation provides some default implementations for data * tables like listener handling. The method {@link #fireEvent()} can be used to promote * changes to all listeners. * * In addition, IO methods are also provided by this abstract implementation. * * @author Ingo Mierswa * @version $Id: AbstractDataTable.java,v 1.7 2008/07/07 10:52:20 ingomierswa Exp $ */ public abstract class AbstractDataTable implements DataTable, Tableable { /** The list of data table listeners. */ private List<DataTableListener> listeners = new LinkedList<DataTableListener>(); /** The name of the table. */ private String name; public AbstractDataTable(String name) { this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String[] getColumnNames() { String[] result = new String[getNumberOfColumns()]; for (int i = 0; i < result.length; i++) result[i] = getColumnName(i); return result; } public void addDataTableListener(DataTableListener dataTableListener) { this.listeners.add(dataTableListener); } public void removeDataTableListener(DataTableListener dataTableListener) { this.listeners.remove(dataTableListener); } protected void fireEvent() { Iterator i = listeners.iterator(); while (i.hasNext()) { ((DataTableListener) i.next()).dataTableUpdated(this); } } public String getValueAsString(DataTableRow row, int column) { if (isDate(column)) { return Tools.formatDate(new Date((long)row.getValue(column))); } else if (isDateTime(column)) { return Tools.formatDateTime(new Date((long)row.getValue(column))); } else if (isTime(column)) { return Tools.formatTime(new Date((long)row.getValue(column))); } else if (isNominal(column)) { return mapIndex(column, (int)row.getValue(column)); } else { return row.getValue(column) + ""; } } public void write(PrintWriter out) throws IOException { out.println("# Generated by " + getName() + "[" + getClass().getName() + "]"); for (int j = 0; j < getNumberOfColumns(); j++) { out.print((j != 0 ? "\t" : "# ") + getColumnName(j)); } out.println(); Iterator i = iterator(); while (i.hasNext()) { DataTableRow row = (DataTableRow) i.next(); for (int j = 0; j < getNumberOfColumns(); j++) { out.print((j != 0 ? "\t" : "") + getValueAsString(row, j)); } out.println(); } out.flush(); } public boolean containsMissingValues() { Iterator<DataTableRow> i = iterator(); while (i.hasNext()) { DataTableRow row = i.next(); for (int j = 0; j < getNumberOfColumns(); j++) { if (Double.isNaN(row.getValue(j))) return true; } } return false; } public int getRowNumber() { return getNumberOfRows(); } public int getColumnNumber() { return getNumberOfColumns(); } public String getCell(int row, int column) { double value = getRow(row).getValue(column); if (isDate(column)) { return Tools.formatDate(new Date((long)value)); } else if (isDateTime(column)) { return Tools.formatDateTime(new Date((long)value)); } else if (isTime(column)) { return Tools.formatTime(new Date((long)value)); } else if (isNominal(column)) { return mapIndex(column, (int)value); } else { return Tools.formatIntegerIfPossible(value); } } }