Example usage for org.jfree.data KeyedObjects getItemCount

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

Introduction

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

Prototype

public int getItemCount() 

Source Link

Document

Returns the number of items (values) in the collection.

Usage

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

/**
 * Tests this object for equality with an arbitrary object.
 *
 * @param obj  the object (<code>null</code> permitted).
 *
 * @return A boolean.//w  w  w.  j  av a2  s. co m
 */
@Override
public boolean equals(Object obj) {

    if (obj == this) {
        return true;
    }
    if (!(obj instanceof KeyedObjects)) {
        return false;
    }
    KeyedObjects that = (KeyedObjects) obj;
    int count = getItemCount();
    if (count != that.getItemCount()) {
        return false;
    }

    for (int i = 0; i < count; i++) {
        Comparable k1 = getKey(i);
        Comparable k2 = that.getKey(i);
        if (!k1.equals(k2)) {
            return false;
        }
        Object o1 = getObject(i);
        Object o2 = that.getObject(i);
        if (o1 == null) {
            if (o2 != null) {
                return false;
            }
        } else {
            if (!o1.equals(o2)) {
                return false;
            }
        }
    }
    return true;

}

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

/**
 * Some checks for the removeValue(int) method.
 *///from   w  w w . j  a  v  a  2 s .  c  om
@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.
 *//*from  w  w  w.  j  a v a2 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.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)
 *///from  w w  w  .  j  a va  2 s. c o 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);
    }
}

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

/**
 * Some checks for the removeValue(int) method.
 *///ww  w  .  j av a2  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   ww w  .  j  a  va  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);
}