List of usage examples for org.jfree.data DefaultKeyedValues getValue
@Override
public Number getValue(Comparable key)
From source file:org.jfree.data.DefaultKeyedValuesTest.java
/** * Some checks for the getValue() methods. *//*from ww w .j a v a 2s . co m*/ @Test public void testGetValue() { DefaultKeyedValues v1 = new DefaultKeyedValues(); try { /* Number n = */ v1.getValue(-1); assertTrue(false); } catch (IndexOutOfBoundsException e) { // expected } try { /* Number n = */ v1.getValue(0); assertTrue(false); } catch (IndexOutOfBoundsException e) { // expected } DefaultKeyedValues v2 = new DefaultKeyedValues(); v2.addValue("K1", new Integer(1)); v2.addValue("K2", new Integer(2)); v2.addValue("K3", new Integer(3)); assertEquals(new Integer(3), v2.getValue(2)); boolean pass = false; try { /* Number n = */ v2.getValue("KK"); } catch (UnknownKeyException e) { pass = true; } assertTrue(pass); }
From source file:org.jfree.data.DefaultKeyedValuesTest.java
/** * Some checks for the addValue() method. *//*from w w w . j a v a 2 s . com*/ @Test public void testAddValue() { DefaultKeyedValues v1 = new DefaultKeyedValues(); v1.addValue("A", 1.0); assertEquals(new Double(1.0), v1.getValue("A")); v1.addValue("B", 2.0); assertEquals(new Double(2.0), v1.getValue("B")); v1.addValue("B", 3.0); assertEquals(new Double(3.0), v1.getValue("B")); assertEquals(2, v1.getItemCount()); v1.addValue("A", null); assertNull(v1.getValue("A")); assertEquals(2, v1.getItemCount()); boolean pass = false; try { v1.addValue(null, 99.9); } catch (IllegalArgumentException e) { pass = true; } assertTrue(pass); }
From source file:org.jfree.data.DefaultKeyedValuesTest.java
/** * Some checks for the insertValue() method. */// w w w. j a va2s. c om @Test public void testInsertValue() { DefaultKeyedValues v1 = new DefaultKeyedValues(); v1.insertValue(0, "A", 1.0); assertEquals(new Double(1.0), v1.getValue(0)); v1.insertValue(0, "B", 2.0); assertEquals(new Double(2.0), v1.getValue(0)); assertEquals(new Double(1.0), v1.getValue(1)); // it's OK to use an index equal to the size of the list v1.insertValue(2, "C", 3.0); assertEquals(new Double(2.0), v1.getValue(0)); assertEquals(new Double(1.0), v1.getValue(1)); assertEquals(new Double(3.0), v1.getValue(2)); // try replacing an existing value v1.insertValue(2, "B", 4.0); assertEquals(new Double(1.0), v1.getValue(0)); assertEquals(new Double(3.0), v1.getValue(1)); assertEquals(new Double(4.0), v1.getValue(2)); }
From source file:org.jfree.data.junit.DefaultKeyedValuesTest.java
/** * Some checks for the getValue() methods. *//* w ww . j a va2s . c o m*/ public void testGetValue() { DefaultKeyedValues v1 = new DefaultKeyedValues(); try { /* Number n = */ v1.getValue(-1); assertTrue(false); } catch (IndexOutOfBoundsException e) { // expected } try { /* Number n = */ v1.getValue(0); assertTrue(false); } catch (IndexOutOfBoundsException e) { // expected } DefaultKeyedValues v2 = new DefaultKeyedValues(); v2.addValue("K1", new Integer(1)); v2.addValue("K2", new Integer(2)); v2.addValue("K3", new Integer(3)); assertEquals(new Integer(3), v2.getValue(2)); boolean pass = false; try { /* Number n = */ v2.getValue("KK"); } catch (UnknownKeyException e) { pass = true; } assertTrue(pass); }
From source file:org.jfree.data.junit.DefaultKeyedValuesTest.java
/** * Some checks for the addValue() method. *//*ww w .j ava2s .c o m*/ public void testAddValue() { DefaultKeyedValues v1 = new DefaultKeyedValues(); v1.addValue("A", 1.0); assertEquals(new Double(1.0), v1.getValue("A")); v1.addValue("B", 2.0); assertEquals(new Double(2.0), v1.getValue("B")); v1.addValue("B", 3.0); assertEquals(new Double(3.0), v1.getValue("B")); assertEquals(2, v1.getItemCount()); v1.addValue("A", null); assertNull(v1.getValue("A")); assertEquals(2, v1.getItemCount()); boolean pass = false; try { v1.addValue(null, 99.9); } catch (IllegalArgumentException e) { pass = true; } assertTrue(pass); }
From source file:org.jfree.data.junit.DefaultKeyedValuesTest.java
/** * Some checks for the insertValue() method. *///from w ww . j a v a2s . c o m public void testInsertValue() { DefaultKeyedValues v1 = new DefaultKeyedValues(); v1.insertValue(0, "A", 1.0); assertEquals(new Double(1.0), v1.getValue(0)); v1.insertValue(0, "B", 2.0); assertEquals(new Double(2.0), v1.getValue(0)); assertEquals(new Double(1.0), v1.getValue(1)); // it's OK to use an index equal to the size of the list v1.insertValue(2, "C", 3.0); assertEquals(new Double(2.0), v1.getValue(0)); assertEquals(new Double(1.0), v1.getValue(1)); assertEquals(new Double(3.0), v1.getValue(2)); // try replacing an existing value v1.insertValue(2, "B", 4.0); assertEquals(new Double(1.0), v1.getValue(0)); assertEquals(new Double(3.0), v1.getValue(1)); assertEquals(new Double(4.0), v1.getValue(2)); }
From source file:org.jfree.data.DefaultKeyedValues2D.java
/** * Returns the value for the given row and column keys. This method will * throw an {@link UnknownKeyException} if either key is not defined in the * data structure.//w w w .j a v a 2s . c om * * @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>). * * @see #addValue(Number, Comparable, Comparable) * @see #removeValue(Comparable, Comparable) */ public Number getValue(Comparable rowKey, Comparable columnKey) { if (rowKey == null) { throw new IllegalArgumentException("Null 'rowKey' argument."); } if (columnKey == null) { throw new IllegalArgumentException("Null 'columnKey' argument."); } // check that the column key is defined in the 2D structure if (!(this.columnKeys.contains(columnKey))) { throw new UnknownKeyException("Unrecognised columnKey: " + columnKey); } // now fetch the row data - need to bear in mind that the row // structure may not have an entry for the column key, but that we // have already checked that the key is valid for the 2D structure int row = getRowIndex(rowKey); if (row >= 0) { DefaultKeyedValues rowData = (DefaultKeyedValues) this.rows.get(row); int col = rowData.getIndex(columnKey); return (col >= 0 ? rowData.getValue(col) : null); } else { throw new UnknownKeyException("Unrecognised rowKey: " + rowKey); } }
From source file:org.jfree.data.DefaultKeyedValues2D.java
/** * Returns the value for the given row and column keys. This method will * throw an {@link UnknownKeyException} if either key is not defined in the * data structure./*from w ww. j a v a 2s. com*/ * * @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>). * * @see #addValue(Number, Comparable, Comparable) * @see #removeValue(Comparable, Comparable) */ @Override public Number getValue(Comparable rowKey, Comparable columnKey) { ParamChecks.nullNotPermitted(rowKey, "rowKey"); ParamChecks.nullNotPermitted(columnKey, "columnKey"); // check that the column key is defined in the 2D structure if (!(this.columnKeys.contains(columnKey))) { throw new UnknownKeyException("Unrecognised columnKey: " + columnKey); } // now fetch the row data - need to bear in mind that the row // structure may not have an entry for the column key, but that we // have already checked that the key is valid for the 2D structure int row = getRowIndex(rowKey); if (row >= 0) { DefaultKeyedValues rowData = (DefaultKeyedValues) this.rows.get(row); int col = rowData.getIndex(columnKey); return (col >= 0 ? rowData.getValue(col) : null); } else { throw new UnknownKeyException("Unrecognised rowKey: " + rowKey); } }
From source file:org.jfree.data.DefaultKeyedValues2D.java
/** * Removes a value from the table by setting it to <code>null</code>. If * all the values in the specified row and/or column are now * <code>null</code>, the row and/or column is removed from the table. * * @param rowKey the row key (<code>null</code> not permitted). * @param columnKey the column key (<code>null</code> not permitted). * * @see #addValue(Number, Comparable, Comparable) *///from w w w. j a va 2 s .co m public void removeValue(Comparable rowKey, Comparable columnKey) { setValue(null, rowKey, columnKey); // 1. check whether the row is now empty. boolean allNull = true; int rowIndex = getRowIndex(rowKey); DefaultKeyedValues row = (DefaultKeyedValues) this.rows.get(rowIndex); for (int item = 0, itemCount = row.getItemCount(); item < itemCount; item++) { if (row.getValue(item) != null) { allNull = false; break; } } if (allNull) { this.rowKeys.remove(rowIndex); this.rows.remove(rowIndex); } // 2. check whether the column is now empty. allNull = true; //int columnIndex = getColumnIndex(columnKey); for (int item = 0, itemCount = this.rows.size(); item < itemCount; item++) { row = (DefaultKeyedValues) this.rows.get(item); int columnIndex = row.getIndex(columnKey); if (columnIndex >= 0 && row.getValue(columnIndex) != null) { allNull = false; break; } } if (allNull) { for (int item = 0, itemCount = this.rows.size(); item < itemCount; item++) { row = (DefaultKeyedValues) this.rows.get(item); int columnIndex = row.getIndex(columnKey); if (columnIndex >= 0) { row.removeValue(columnIndex); } } this.columnKeys.remove(columnKey); } }
From source file:org.jfree.data.DefaultKeyedValues2D.java
/** * Returns the value for a given row and column. * * @param row the row index./*from w w w . ja v a 2 s . c o m*/ * @param column the column index. * * @return The value. * * @see #getValue(Comparable, Comparable) */ public Number getValue(int row, int column) { Number result = null; DefaultKeyedValues rowData = (DefaultKeyedValues) this.rows.get(row); if (rowData != null) { Comparable columnKey = (Comparable) this.columnKeys.get(column); // the row may not have an entry for this key, in which case the // return value is null int index = rowData.getIndex(columnKey); if (index >= 0) { result = rowData.getValue(index); } } return result; }