Example usage for org.jfree.data DefaultKeyedValues getIndex

List of usage examples for org.jfree.data DefaultKeyedValues getIndex

Introduction

In this page you can find the example usage for org.jfree.data DefaultKeyedValues getIndex.

Prototype

@Override
public int getIndex(Comparable key) 

Source Link

Document

Returns the index for a given key.

Usage

From source file:org.jfree.data.DefaultKeyedValuesTest.java

/**
 * Another check for the getIndex(Comparable) method.
 *//*from  ww w  . j ava2s . com*/
@Test
public void testGetIndex2() {
    DefaultKeyedValues v = new DefaultKeyedValues();
    assertEquals(-1, v.getIndex("K1"));
    v.addValue("K1", 1.0);
    assertEquals(0, v.getIndex("K1"));
    v.removeValue("K1");
    assertEquals(-1, v.getIndex("K1"));
}

From source file:org.jfree.data.DefaultKeyedValuesTest.java

/**
 * Some checks for the getIndex() methods.
 *//*from   ww  w  .j  a va  2  s  .co m*/
@Test
public void testGetIndex() {
    DefaultKeyedValues v1 = new DefaultKeyedValues();
    assertEquals(-1, v1.getIndex("K1"));

    DefaultKeyedValues v2 = new DefaultKeyedValues();
    v2.addValue("K1", new Integer(1));
    v2.addValue("K2", new Integer(2));
    v2.addValue("K3", new Integer(3));
    assertEquals(2, v2.getIndex("K3"));

    // try null
    boolean pass = false;
    try {
        v2.getIndex(null);
    } catch (IllegalArgumentException e) {
        pass = true;
    }
    assertTrue(pass);
}

From source file:org.jfree.data.junit.DefaultKeyedValuesTest.java

/**
 * Another check for the getIndex(Comparable) method.
 *///from  w  w w  .j  a v a 2 s.co m
public void testGetIndex2() {
    DefaultKeyedValues v = new DefaultKeyedValues();
    assertEquals(-1, v.getIndex("K1"));
    v.addValue("K1", 1.0);
    assertEquals(0, v.getIndex("K1"));
    v.removeValue("K1");
    assertEquals(-1, v.getIndex("K1"));
}

From source file:org.jfree.data.junit.DefaultKeyedValuesTest.java

/**
 * Some checks for the getIndex() methods.
 *///from   ww w  . j  a v a 2 s  . c  o m
public void testGetIndex() {
    DefaultKeyedValues v1 = new DefaultKeyedValues();
    assertEquals(-1, v1.getIndex("K1"));

    DefaultKeyedValues v2 = new DefaultKeyedValues();
    v2.addValue("K1", new Integer(1));
    v2.addValue("K2", new Integer(2));
    v2.addValue("K3", new Integer(3));
    assertEquals(2, v2.getIndex("K3"));

    // try null
    boolean pass = false;
    try {
        v2.getIndex(null);
    } catch (IllegalArgumentException e) {
        pass = true;
    }
    assertTrue(pass);
}

From source file:org.jfree.data.DefaultKeyedValues2D.java

/**
 * Removes a column from the table./*w  w  w  .j  ava  2 s .c  o  m*/
 *
 * @param columnKey  the column key (<code>null</code> not permitted).
 * 
 * @throws UnknownKeyException if the table does not contain a column with
 *     the specified key.
 * @throws IllegalArgumentException if <code>columnKey</code> is 
 *     <code>null</code>.
 * 
 * @see #removeColumn(int)
 * @see #removeRow(Comparable)
 */
public void removeColumn(Comparable columnKey) {
    if (columnKey == null) {
        throw new IllegalArgumentException("Null 'columnKey' argument.");
    }
    if (!this.columnKeys.contains(columnKey)) {
        throw new UnknownKeyException("Unknown key: " + columnKey);
    }
    Iterator iterator = this.rows.iterator();
    while (iterator.hasNext()) {
        DefaultKeyedValues rowData = (DefaultKeyedValues) iterator.next();
        int index = rowData.getIndex(columnKey);
        if (index >= 0) {
            rowData.removeValue(columnKey);
        }
    }
    this.columnKeys.remove(columnKey);
}

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 ww  . j a v  a  2 s.  co m
 *
 * @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

/**
 * Removes a column from the table.//from   ww  w  .  j a va 2  s . c om
 *
 * @param columnKey  the column key (<code>null</code> not permitted).
 *
 * @throws UnknownKeyException if the table does not contain a column with
 *     the specified key.
 * @throws IllegalArgumentException if <code>columnKey</code> is
 *     <code>null</code>.
 *
 * @see #removeColumn(int)
 * @see #removeRow(Comparable)
 */
public void removeColumn(Comparable columnKey) {
    ParamChecks.nullNotPermitted(columnKey, "columnKey");
    if (!this.columnKeys.contains(columnKey)) {
        throw new UnknownKeyException("Unknown key: " + columnKey);
    }
    Iterator iterator = this.rows.iterator();
    while (iterator.hasNext()) {
        DefaultKeyedValues rowData = (DefaultKeyedValues) iterator.next();
        int index = rowData.getIndex(columnKey);
        if (index >= 0) {
            rowData.removeValue(columnKey);
        }
    }
    this.columnKeys.remove(columnKey);
}

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  ww 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)
 */
@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.DefaultKeyedValuesTest.java

/**
 * Some tests for the removeValue() method.
 *//* w  w w  .j ava  2  s  . c  om*/
@Test
public void testRemoveValue() {
    DefaultKeyedValues data = new DefaultKeyedValues();
    data.addValue("A", new Double(1.0));
    data.addValue("B", null);
    data.addValue("C", new Double(3.0));
    data.addValue("D", new Double(2.0));
    assertEquals(1, data.getIndex("B"));
    data.removeValue("B");
    assertEquals(-1, data.getIndex("B"));

    boolean pass = false;
    try {
        data.removeValue("XXX");
    } catch (UnknownKeyException e) {
        pass = true;
    }
    assertTrue(pass);
}

From source file:org.jfree.data.junit.DefaultKeyedValuesTest.java

/**
 * Some tests for the removeValue() method.
 *///from   w ww  . jav  a2  s  .c  o m
public void testRemoveValue() {
    DefaultKeyedValues data = new DefaultKeyedValues();
    data.addValue("A", new Double(1.0));
    data.addValue("B", null);
    data.addValue("C", new Double(3.0));
    data.addValue("D", new Double(2.0));
    assertEquals(1, data.getIndex("B"));
    data.removeValue("B");
    assertEquals(-1, data.getIndex("B"));

    boolean pass = false;
    try {
        data.removeValue("XXX");
    } catch (UnknownKeyException e) {
        pass = true;
    }
    assertTrue(pass);
}