001: package prefuse.data.column;
002:
003: import java.util.Arrays;
004:
005: import prefuse.data.DataReadOnlyException;
006: import prefuse.data.DataTypeException;
007:
008: /**
009: * Column instance for sotring flaot values.
010: *
011: * @author <a href="http://jheer.org">jeffrey heer</a>
012: */
013: public class FloatColumn extends AbstractColumn {
014:
015: private float[] m_values;
016: private int m_size;
017:
018: /**
019: * Create a new empty FloatColumn.
020: */
021: public FloatColumn() {
022: this (0, 10, 0f);
023: }
024:
025: /**
026: * Create a new FloatColumn.
027: * @param nrows the initial size of the column
028: */
029: public FloatColumn(int nrows) {
030: this (nrows, nrows, 0f);
031: }
032:
033: /**
034: * Create a new FloatColumn.
035: * @param nrows the initial size of the column
036: * @param capacity the initial capacity of the column
037: * @param defaultValue the default value for the column
038: */
039: public FloatColumn(int nrows, int capacity, float defaultValue) {
040: super (float.class, new Float(defaultValue));
041: if (capacity < nrows) {
042: throw new IllegalArgumentException(
043: "Capacity value can not be less than the row count.");
044: }
045: m_values = new float[capacity];
046: Arrays.fill(m_values, defaultValue);
047: m_size = nrows;
048: }
049:
050: // ------------------------------------------------------------------------
051: // Column Metadata
052:
053: /**
054: * @see prefuse.data.column.Column#getRowCount()
055: */
056: public int getRowCount() {
057: return m_size;
058: }
059:
060: /**
061: * @see prefuse.data.column.Column#setMaximumRow(int)
062: */
063: public void setMaximumRow(int nrows) {
064: if (nrows > m_values.length) {
065: int capacity = Math.max((3 * m_values.length) / 2 + 1,
066: nrows);
067: float[] values = new float[capacity];
068: System.arraycopy(m_values, 0, values, 0, m_size);
069: Arrays.fill(values, m_size, capacity,
070: ((Float) m_defaultValue).floatValue());
071: m_values = values;
072: }
073: m_size = nrows;
074: }
075:
076: // ------------------------------------------------------------------------
077: // Data Access Methods
078:
079: /**
080: * @see prefuse.data.column.Column#get(int)
081: */
082: public Object get(int row) {
083: return new Float(getFloat(row));
084: }
085:
086: /**
087: * @see prefuse.data.column.Column#set(java.lang.Object, int)
088: */
089: public void set(Object val, int row) throws DataTypeException {
090: if (m_readOnly) {
091: throw new DataReadOnlyException();
092: } else if (val != null) {
093: if (val instanceof Number) {
094: setFloat(((Number) val).floatValue(), row);
095: } else if (val instanceof String) {
096: setString((String) val, row);
097: } else {
098: throw new DataTypeException(val.getClass());
099: }
100: } else {
101: throw new DataTypeException(
102: "Column does not accept null values");
103: }
104: }
105:
106: // ------------------------------------------------------------------------
107: // Data Type Convenience Methods
108:
109: /**
110: * @see prefuse.data.column.AbstractColumn#getFloat(int)
111: */
112: public float getFloat(int row) throws DataTypeException {
113: if (row < 0 || row > m_size) {
114: throw new IllegalArgumentException(
115: "Row index out of bounds: " + row);
116: }
117: return m_values[row];
118: }
119:
120: /**
121: * @see prefuse.data.column.AbstractColumn#setFloat(float, int)
122: */
123: public void setFloat(float val, int row) throws DataTypeException {
124: if (m_readOnly) {
125: throw new DataReadOnlyException();
126: } else if (row < 0 || row >= m_size) {
127: throw new IllegalArgumentException(
128: "Row index out of bounds: " + row);
129: }
130: // get the previous value
131: float prev = m_values[row];
132:
133: // exit early if no change
134: if (prev == val)
135: return;
136:
137: // set the new value
138: m_values[row] = val;
139:
140: // fire a change event
141: fireColumnEvent(row, prev);
142: }
143:
144: // /**
145: // * @see prefuse.data.column.AbstractColumn#getString(int)
146: // */
147: // public String getString(int row) throws DataTypeException {
148: // return String.valueOf(getFloat(row));
149: // }
150: //
151: // /**
152: // * @see prefuse.data.column.AbstractColumn#setString(java.lang.String, int)
153: // */
154: // public void setString(String val, int row) throws DataTypeException {
155: // setFloat(Float.parseFloat(val), row);
156: // }
157:
158: // ------------------------------------------------------------------------
159:
160: /**
161: * @see prefuse.data.column.Column#getInt(int)
162: */
163: public int getInt(int row) throws DataTypeException {
164: return (int) getFloat(row);
165: }
166:
167: /**
168: * @see prefuse.data.column.Column#getLong(int)
169: */
170: public long getLong(int row) throws DataTypeException {
171: return (long) getFloat(row);
172: }
173:
174: /**
175: * @see prefuse.data.column.Column#getDouble(int)
176: */
177: public double getDouble(int row) throws DataTypeException {
178: return getFloat(row);
179: }
180:
181: } // end of class FloatColumn
|