]> source.dussan.org Git - vaadin-framework.git/commitdiff
Merged:
authorArtur Signell <artur.signell@itmill.com>
Tue, 21 Apr 2009 07:33:33 +0000 (07:33 +0000)
committerArtur Signell <artur.signell@itmill.com>
Tue, 21 Apr 2009 07:33:33 +0000 (07:33 +0000)
Fixed #2857 - Added checks for constructor arguments.
Added a simple test case for BeanItemContainer.
Changed BeanItemContainer to always return BeanItems instead of generic Items.

svn changeset:7468/svn branch:6.0

src/com/itmill/toolkit/data/util/BeanItemContainer.java
src/com/itmill/toolkit/tests/components/beanitemcontainer/TestBeanItemContainerUsage.java [new file with mode: 0644]

index d4da324feb6f69f8f868877d58d58b4f4e30fbbd..aeb1e83c2d67f10b1e0634fd2a64191fd3a5119c 100644 (file)
@@ -16,7 +16,6 @@ import java.util.Map;
 import java.util.Set;
 
 import com.itmill.toolkit.data.Container;
-import com.itmill.toolkit.data.Item;
 import com.itmill.toolkit.data.Property;
 import com.itmill.toolkit.data.Container.Filterable;
 import com.itmill.toolkit.data.Container.Indexed;
@@ -65,23 +64,38 @@ public class BeanItemContainer<BT> implements Indexed, Sortable, Filterable,
      * 
      * @param type
      *            the class of beans to be used with this containers.
+     * @throws IllegalArgumentException
+     *             If the type is null
      */
     public BeanItemContainer(Class<BT> type) {
+        if (type == null) {
+            throw new IllegalArgumentException(
+                    "The type passed to BeanItemContainer must not be null");
+        }
         this.type = type;
         model = BeanItem.getPropertyDescriptors(type);
     }
 
     /**
-     * Constructs BeanItemContainer with given collection of beans in it.
+     * Constructs BeanItemContainer with given collection of beans in it. The
+     * collection must not be empty or an IllegalArgument is thrown.
      * 
-     * @param list
+     * @param collection
      *            non empty {@link Collection} of beans.
+     * @throws IllegalArgumentException
+     *             If the collection is null or empty.
      */
-    public BeanItemContainer(Collection<BT> list) {
-        type = (Class<BT>) list.iterator().next().getClass();
+    public BeanItemContainer(Collection<BT> collection)
+            throws IllegalArgumentException {
+        if (collection == null || collection.isEmpty()) {
+            throw new IllegalArgumentException(
+                    "The collection passed to BeanItemContainer must not be null or empty");
+        }
+
+        type = (Class<BT>) collection.iterator().next().getClass();
         model = BeanItem.getPropertyDescriptors(type);
         int i = 0;
-        for (BT bt : list) {
+        for (BT bt : collection) {
             addItemAt(i++, bt);
         }
     }
@@ -102,7 +116,7 @@ public class BeanItemContainer<BT> implements Indexed, Sortable, Filterable,
      * 
      * @see com.itmill.toolkit.data.Container.Indexed#addItemAt(int, Object)
      */
-    public Item addItemAt(int index, Object newItemId)
+    public BeanItem addItemAt(int index, Object newItemId)
             throws UnsupportedOperationException {
         if (index < 0 || index > size()) {
             return null;
@@ -128,7 +142,7 @@ public class BeanItemContainer<BT> implements Indexed, Sortable, Filterable,
      *            Id of the new item to be added.
      * @return Returns new item or null if the operation fails.
      */
-    private Item addItemAtInternalIndex(int index, Object newItemId) {
+    private BeanItem addItemAtInternalIndex(int index, Object newItemId) {
         // Make sure that the Item has not been created yet
         if (allItems.contains(newItemId)) {
             return null;
@@ -179,7 +193,7 @@ public class BeanItemContainer<BT> implements Indexed, Sortable, Filterable,
      * @see com.itmill.toolkit.data.Container.Ordered#addItemAfter(Object,
      *      Object)
      */
-    public Item addItemAfter(Object previousItemId, Object newItemId)
+    public BeanItem addItemAfter(Object previousItemId, Object newItemId)
             throws UnsupportedOperationException {
         // only add if the previous item is visible
         if (list.contains(previousItemId)) {
@@ -256,7 +270,7 @@ public class BeanItemContainer<BT> implements Indexed, Sortable, Filterable,
      * 
      * @see com.itmill.toolkit.data.Container#addItem(Object)
      */
-    public Item addItem(Object itemId) throws UnsupportedOperationException {
+    public BeanItem addItem(Object itemId) throws UnsupportedOperationException {
         if (list.size() > 0) {
             // add immediately after last visible item
             int lastIndex = allItems.indexOf(lastItemId());
@@ -275,18 +289,16 @@ public class BeanItemContainer<BT> implements Indexed, Sortable, Filterable,
         return beanToItem.get(itemId).getItemProperty(propertyId);
     }
 
-    @SuppressWarnings("unchecked")
-    public Collection getContainerPropertyIds() {
+    public Collection<String> getContainerPropertyIds() {
         return model.keySet();
     }
 
-    public Item getItem(Object itemId) {
+    public BeanItem getItem(Object itemId) {
         return beanToItem.get(itemId);
     }
 
-    @SuppressWarnings("unchecked")
-    public Collection getItemIds() {
-        return (Collection) list.clone();
+    public Collection<BT> getItemIds() {
+        return (Collection<BT>) list.clone();
     }
 
     public Class<?> getType(Object propertyId) {
diff --git a/src/com/itmill/toolkit/tests/components/beanitemcontainer/TestBeanItemContainerUsage.java b/src/com/itmill/toolkit/tests/components/beanitemcontainer/TestBeanItemContainerUsage.java
new file mode 100644 (file)
index 0000000..b01197c
--- /dev/null
@@ -0,0 +1,75 @@
+package com.itmill.toolkit.tests.components.beanitemcontainer;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import com.itmill.toolkit.data.util.BeanItemContainer;
+import com.itmill.toolkit.tests.components.TestBase;
+import com.itmill.toolkit.ui.Table;
+
+public class TestBeanItemContainerUsage extends TestBase {
+
+    @Override
+    protected String getDescription() {
+        return "A test for the BeanItemContainer. The table should contain three persons and show their first and last names and their age.";
+    }
+
+    @Override
+    protected Integer getTicketNumber() {
+        return 1061;
+    }
+
+    @Override
+    protected void setup() {
+        Table t = new Table("Table containing Persons");
+        t.setPageLength(5);
+        t.setWidth("100%");
+        List<Person> persons = new ArrayList<Person>();
+        persons.add(new Person("Jones", "Birchman", 35));
+        persons.add(new Person("Marc", "Smith", 30));
+        persons.add(new Person("Greg", "Sandman", 75));
+
+        BeanItemContainer<Person> bic = new BeanItemContainer<Person>(persons);
+        t.setContainerDataSource(bic);
+
+        addComponent(t);
+    }
+
+    public static class Person {
+        private String firstName;
+        private String lastName;
+        private int age;
+
+        public String getFirstName() {
+            return firstName;
+        }
+
+        public void setFirstName(String firstName) {
+            this.firstName = firstName;
+        }
+
+        public String getLastName() {
+            return lastName;
+        }
+
+        public void setLastName(String lastName) {
+            this.lastName = lastName;
+        }
+
+        public int getAge() {
+            return age;
+        }
+
+        public void setAge(int age) {
+            this.age = age;
+        }
+
+        public Person(String firstName, String lastName, int age) {
+            super();
+            this.firstName = firstName;
+            this.lastName = lastName;
+            this.age = age;
+        }
+
+    }
+}