Example usage for org.jfree.data KeyedObjects getIndex

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

Introduction

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

Prototype

public int getIndex(Comparable key) 

Source Link

Document

Returns the index for a given key, or -1.

Usage

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

/**
 * Simple checks for the getIndex(Comparable) method.
 *//*from  w w w. j a v  a2s  .c  o m*/
@Test
public void testGetIndex() {
    KeyedObjects ko1 = new KeyedObjects();
    ko1.addObject("Key 1", "Object 1");
    ko1.addObject("Key 2", null);
    ko1.addObject("Key 3", "Object 2");
    assertEquals(0, ko1.getIndex("Key 1"));
    assertEquals(1, ko1.getIndex("Key 2"));
    assertEquals(2, ko1.getIndex("Key 3"));

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

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

/**
 * Removes an entire column from the table.
 *
 * @param columnKey  the column key (<code>null</code> not permitted).
 *
 * @throws UnknownKeyException if <code>rowKey</code> is not recognised.
 *
 * @see #removeRow(Comparable)/*from   www  . j a va2s. co m*/
 */
public void removeColumn(Comparable columnKey) {
    int index = getColumnIndex(columnKey);
    if (index < 0) {
        throw new UnknownKeyException("Column key (" + columnKey + ") not recognised.");
    }
    Iterator iterator = this.rows.iterator();
    while (iterator.hasNext()) {
        KeyedObjects rowData = (KeyedObjects) iterator.next();
        int i = rowData.getIndex(columnKey);
        if (i >= 0) {
            rowData.removeValue(i);
        }
    }
    this.columnKeys.remove(columnKey);
}

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

/**
 * Returns the object for the given row and column keys.
 *
 * @param rowKey  the row key (<code>null</code> not permitted).
 * @param columnKey  the column key (<code>null</code> not permitted).
 *
 * @return The object (possibly <code>null</code>).
 *
 * @throws IllegalArgumentException if <code>rowKey</code> or
 *         <code>columnKey</code> is <code>null</code>.
 * @throws UnknownKeyException if <code>rowKey</code> or
 *         <code>columnKey</code> is not recognised.
 *///from w  ww  .  j  av a 2s .  c  o m
public Object getObject(Comparable rowKey, Comparable columnKey) {
    ParamChecks.nullNotPermitted(rowKey, "rowKey");
    ParamChecks.nullNotPermitted(columnKey, "columnKey");
    int row = this.rowKeys.indexOf(rowKey);
    if (row < 0) {
        throw new UnknownKeyException("Row key (" + rowKey + ") not recognised.");
    }
    int column = this.columnKeys.indexOf(columnKey);
    if (column < 0) {
        throw new UnknownKeyException("Column key (" + columnKey + ") not recognised.");
    }
    KeyedObjects rowData = (KeyedObjects) this.rows.get(row);
    int index = rowData.getIndex(columnKey);
    if (index >= 0) {
        return rowData.getObject(index);
    } else {
        return null;
    }
}

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

/**
 * Some checks for the removeValue(int) method.
 *//* ww  w  .  j a v a2s . co  m*/
@Test
public void testRemoveValueInt() {
    KeyedObjects ko1 = new KeyedObjects();
    ko1.setObject("Key 1", "Object 1");
    ko1.setObject("Key 2", null);
    ko1.setObject("Key 3", "Object 2");

    ko1.removeValue(1);
    assertEquals(2, ko1.getItemCount());
    assertEquals(1, ko1.getIndex("Key 3"));

    // try negative key index
    boolean pass = false;
    try {
        ko1.removeValue(-1);
    } catch (IndexOutOfBoundsException e) {
        pass = true;
    }
    assertTrue(pass);

    // try key index == itemCount
    pass = false;
    try {
        ko1.removeValue(2);
    } catch (IndexOutOfBoundsException e) {
        pass = true;
    }
    assertTrue(pass);
}

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

/**
 * Some checks for the removeValue() methods.
 *///w  ww. j a va  2 s  . c o m
@Test
public void testRemoveValue() {
    KeyedObjects ko1 = new KeyedObjects();
    ko1.setObject("Key 1", "Object 1");
    ko1.setObject("Key 2", null);
    ko1.setObject("Key 3", "Object 2");

    ko1.removeValue(1);
    assertEquals(2, ko1.getItemCount());
    assertEquals(1, ko1.getIndex("Key 3"));

    ko1.removeValue("Key 1");
    assertEquals(1, ko1.getItemCount());
    assertEquals(0, ko1.getIndex("Key 3"));

    // try unknown key
    boolean pass = false;
    try {
        ko1.removeValue("UNKNOWN");
    } catch (UnknownKeyException e) {
        pass = true;
    }
    assertTrue(pass);

    // try null argument
    pass = false;
    try {
        ko1.removeValue(null);
    } catch (IllegalArgumentException e) {
        pass = true;
    }
    assertTrue(pass);
}

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

/**
 * Simple checks for the getIndex(Comparable) method.
 *//*from w  w  w.j  av  a2  s  .c  om*/
public void testGetIndex() {
    KeyedObjects ko1 = new KeyedObjects();
    ko1.addObject("Key 1", "Object 1");
    ko1.addObject("Key 2", null);
    ko1.addObject("Key 3", "Object 2");
    assertEquals(0, ko1.getIndex("Key 1"));
    assertEquals(1, ko1.getIndex("Key 2"));
    assertEquals(2, ko1.getIndex("Key 3"));

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

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

/**
 * Returns the object for a given row and column.
 *
 * @param row  the row index (in the range 0 to getRowCount() - 1).
 * @param column  the column index (in the range 0 to getColumnCount() - 1).
 *
 * @return The object (possibly <code>null</code>).
 *
 * @see #getObject(Comparable, Comparable)
 *//*w  w w.  j  av  a2s  .  co  m*/
public Object getObject(int row, int column) {
    Object result = null;
    KeyedObjects rowData = (KeyedObjects) this.rows.get(row);
    if (rowData != null) {
        Comparable columnKey = (Comparable) this.columnKeys.get(column);
        if (columnKey != null) {
            int index = rowData.getIndex(columnKey);
            if (index >= 0) {
                result = rowData.getObject(columnKey);
            }
        }
    }
    return result;
}

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

/**
 * Some checks for the removeValue(int) method.
 *//*w w  w . j a  v  a  2 s.c om*/
public void testRemoveValueInt() {
    KeyedObjects ko1 = new KeyedObjects();
    ko1.setObject("Key 1", "Object 1");
    ko1.setObject("Key 2", null);
    ko1.setObject("Key 3", "Object 2");

    ko1.removeValue(1);
    assertEquals(2, ko1.getItemCount());
    assertEquals(1, ko1.getIndex("Key 3"));

    // try negative key index
    boolean pass = false;
    try {
        ko1.removeValue(-1);
    } catch (IndexOutOfBoundsException e) {
        pass = true;
    }
    assertTrue(pass);

    // try key index == itemCount
    pass = false;
    try {
        ko1.removeValue(2);
    } catch (IndexOutOfBoundsException e) {
        pass = true;
    }
    assertTrue(pass);
}

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

/**
 * Some checks for the removeValue() methods.
 *//*from   w w w  .  ja  v a 2s. com*/
public void testRemoveValue() {
    KeyedObjects ko1 = new KeyedObjects();
    ko1.setObject("Key 1", "Object 1");
    ko1.setObject("Key 2", null);
    ko1.setObject("Key 3", "Object 2");

    ko1.removeValue(1);
    assertEquals(2, ko1.getItemCount());
    assertEquals(1, ko1.getIndex("Key 3"));

    ko1.removeValue("Key 1");
    assertEquals(1, ko1.getItemCount());
    assertEquals(0, ko1.getIndex("Key 3"));

    // try unknown key
    boolean pass = false;
    try {
        ko1.removeValue("UNKNOWN");
    } catch (UnknownKeyException e) {
        pass = true;
    }
    assertTrue(pass);

    // try null argument
    pass = false;
    try {
        ko1.removeValue(null);
    } catch (IllegalArgumentException e) {
        pass = true;
    }
    assertTrue(pass);
}

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

/**
 * Removes an object from the table by setting it to <code>null</code>.  If
 * all the objects 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 #addObject(Object, Comparable, Comparable)
 */// w  w  w. ja v  a 2 s . co  m
public void removeObject(Comparable rowKey, Comparable columnKey) {
    int rowIndex = getRowIndex(rowKey);
    if (rowIndex < 0) {
        throw new UnknownKeyException("Row key (" + rowKey + ") not recognised.");
    }
    int columnIndex = getColumnIndex(columnKey);
    if (columnIndex < 0) {
        throw new UnknownKeyException("Column key (" + columnKey + ") not recognised.");
    }
    setObject(null, rowKey, columnKey);

    // 1. check whether the row is now empty.
    boolean allNull = true;
    KeyedObjects row = (KeyedObjects) this.rows.get(rowIndex);

    for (int item = 0, itemCount = row.getItemCount(); item < itemCount; item++) {
        if (row.getObject(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;

    for (int item = 0, itemCount = this.rows.size(); item < itemCount; item++) {
        row = (KeyedObjects) this.rows.get(item);
        int colIndex = row.getIndex(columnKey);
        if (colIndex >= 0 && row.getObject(colIndex) != null) {
            allNull = false;
            break;
        }
    }

    if (allNull) {
        for (int item = 0, itemCount = this.rows.size(); item < itemCount; item++) {
            row = (KeyedObjects) this.rows.get(item);
            int colIndex = row.getIndex(columnKey);
            if (colIndex >= 0) {
                row.removeValue(colIndex);
            }
        }
        this.columnKeys.remove(columnKey);
    }
}