package com.positive.charts.data.category;
import java.io.Serializable;
import java.util.List;
import com.positive.charts.common.UnknownKeyException;
import com.positive.charts.data.DefaultKeyedValues2D;
import com.positive.charts.data.general.AbstractDataset;
import com.positive.charts.event.DatasetChangeEvent;
/**
* A default implementation of the {@link CategoryDataset} interface.
*/
public class DefaultCategoryDataset extends AbstractDataset implements
CategoryDataset, Serializable {
/** For serialization. */
private static final long serialVersionUID = -8168173757291644622L;
/** A storage structure for the data. */
private final DefaultKeyedValues2D data;
/**
* Creates a new (empty) dataset.
*/
public DefaultCategoryDataset() {
this.data = new DefaultKeyedValues2D();
}
/**
* Adds a value to the table.
*
* @param value
* the value.
* @param rowKey
* the row key.
* @param columnKey
* the column key.
*/
public void addValue(final double value, final Comparable rowKey,
final Comparable columnKey) {
this.addValue(new Double(value), rowKey, columnKey);
}
/**
* Adds a value to the table. Performs the same function as setValue().
*
* @param value
* the value.
* @param rowKey
* the row key.
* @param columnKey
* the column key.
*/
public void addValue(final Number value, final Comparable rowKey,
final Comparable columnKey) {
this.data.addValue(value, rowKey, columnKey);
this.fireDatasetChanged();
}
/**
* Clears all data from the dataset and sends a {@link DatasetChangeEvent}
* to all registered listeners.
*/
public void clear() {
this.data.clear();
this.fireDatasetChanged();
}
/**
* Tests if this object is equal to another.
*
* @param obj
* the other object.
*
* @return A boolean.
*/
public boolean equals(final Object obj) {
if (obj == this) {
return true;
}
if (!(obj instanceof CategoryDataset)) {
return false;
}
final CategoryDataset that = (CategoryDataset) obj;
if (!this.getRowKeys().equals(that.getRowKeys())) {
return false;
}
if (!this.getColumnKeys().equals(that.getColumnKeys())) {
return false;
}
final int rowCount = this.getRowCount();
final int colCount = this.getColumnCount();
for (int r = 0; r < rowCount; r++) {
for (int c = 0; c < colCount; c++) {
final Number v1 = this.getValue(r, c);
final Number v2 = that.getValue(r, c);
if (v1 == null) {
if (v2 != null) {
return false;
}
} else if (!v1.equals(v2)) {
return false;
}
}
}
return true;
}
/**
* Returns the number of columns in the table.
*
* @return The column count.
*/
public int getColumnCount() {
return this.data.getColumnCount();
}
/**
* Returns the column index for a given key.
*
* @param key
* the column key.
*
* @return The column index.
*/
public int getColumnIndex(final Comparable key) {
return this.data.getColumnIndex(key);
}
/**
* Returns a column key.
*
* @param column
* the column index (zero-based).
*
* @return The column key.
*/
public Comparable getColumnKey(final int column) {
return this.data.getColumnKey(column);
}
/**
* Returns the column keys.
*
* @return The keys.
*/
public List getColumnKeys() {
return this.data.getColumnKeys();
}
/**
* Returns the number of rows in the table.
*
* @return The row count.
*/
public int getRowCount() {
return this.data.getRowCount();
}
/**
* Returns the row index for a given key.
*
* @param key
* the row key.
*
* @return The row index.
*/
public int getRowIndex(final Comparable key) {
return this.data.getRowIndex(key);
}
/**
* Returns a row key.
*
* @param row
* the row index (zero-based).
*
* @return The row key.
*/
public Comparable getRowKey(final int row) {
return this.data.getRowKey(row);
}
/**
* Returns the row keys.
*
* @return The keys.
*/
public List getRowKeys() {
return this.data.getRowKeys();
}
/**
* Returns the value for a pair of keys.
*
* @param rowKey
* the row key (<code>null</code> not permitted).
* @param columnKey
* the column key (<code>null</code> not permitted).
*
* @return The value (possibly <code>null</code>).
*
* @throws UnknownKeyException
* if either key is not defined in the dataset.
*/
public Number getValue(final Comparable rowKey, final Comparable columnKey) {
return this.data.getValue(rowKey, columnKey);
}
/**
* Returns a value from the table.
*
* @param row
* the row index (zero-based).
* @param column
* the column index (zero-based).
*
* @return The value (possibly <code>null</code>).
*/
public Number getValue(final int row, final int column) {
return this.data.getValue(row, column);
}
/**
* Returns a hash code for the dataset.
*
* @return A hash code.
*/
public int hashCode() {
return this.data.hashCode();
}
/**
* Adds the specified value to an existing value in the dataset (if the
* existing value is <code>null</code>, it is treated as if it were 0.0).
*
* @param value
* the value.
* @param rowKey
* the row key (<code>null</code> not permitted).
* @param columnKey
* the column key (<code>null</code> not permitted).
*
* @throws UnknownKeyException
* if either key is not defined in the dataset.
*/
public void incrementValue(final double value, final Comparable rowKey,
final Comparable columnKey) {
double existing = 0.0;
final Number n = this.getValue(rowKey, columnKey);
if (n != null) {
existing = n.doubleValue();
}
this.setValue(existing + value, rowKey, columnKey);
}
public void notifyListeners() {
this.fireDatasetChanged();
}
/**
* Removes a column from the dataset.
*
* @param columnKey
* the column key.
*/
public void removeColumn(final Comparable columnKey) {
this.data.removeColumn(columnKey);
this.fireDatasetChanged();
}
/**
* Removes a column from the dataset.
*
* @param columnIndex
* the column index.
*/
public void removeColumn(final int columnIndex) {
this.data.removeColumn(columnIndex);
this.fireDatasetChanged();
}
/**
* Removes a row from the dataset.
*
* @param rowKey
* the row key.
*/
public void removeRow(final Comparable rowKey) {
this.data.removeRow(rowKey);
this.fireDatasetChanged();
}
/**
* Removes a row from the dataset.
*
* @param rowIndex
* the row index.
*/
public void removeRow(final int rowIndex) {
this.data.removeRow(rowIndex);
this.fireDatasetChanged();
}
/**
* Removes a value from the dataset.
*
* @param rowKey
* the row key.
* @param columnKey
* the column key.
*/
public void removeValue(final Comparable rowKey, final Comparable columnKey) {
this.data.removeValue(rowKey, columnKey);
this.fireDatasetChanged();
}
/**
* Adds or updates a value in the table and sends a
* {@link DatasetChangeEvent} to all registered listeners.
*
* @param value
* the value.
* @param rowKey
* the row key (<code>null</code> not permitted).
* @param columnKey
* the column key (<code>null</code> not permitted).
*/
public void setValue(final double value, final Comparable rowKey,
final Comparable columnKey) {
this.setValue(new Double(value), rowKey, columnKey);
}
/**
* Adds or updates a value in the table and sends a
* {@link DatasetChangeEvent} to all registered listeners.
*
* @param value
* the value (<code>null</code> permitted).
* @param rowKey
* the row key (<code>null</code> not permitted).
* @param columnKey
* the column key (<code>null</code> not permitted).
*/
public void setValue(final Number value, final Comparable rowKey,
final Comparable columnKey) {
this.setValueNoNotify(value, rowKey, columnKey);
this.fireDatasetChanged();
}
public void setValueNoNotify(final Number value, final Comparable rowKey,
final Comparable columnKey) {
this.data.setValue(value, rowKey, columnKey);
}
}