From c74a3d6d7b7db4eeedcda9c2ae215c89c3b1394e Mon Sep 17 00:00:00 2001 From: Henri Sara Date: Fri, 7 Sep 2012 11:07:57 +0300 Subject: [PATCH] Return short list instead of exception if less items available (#8028) Container.Indexed.getItemIds(int, int) does not throw an exception if too few items are available in the container. --- server/src/com/vaadin/data/Container.java | 20 ++++++----------- .../src/com/vaadin/data/ContainerHelpers.java | 6 +---- .../data/util/AbstractInMemoryContainer.java | 7 +----- .../data/util/TestIndexedContainer.java | 22 +++++-------------- 4 files changed, 14 insertions(+), 41 deletions(-) diff --git a/server/src/com/vaadin/data/Container.java b/server/src/com/vaadin/data/Container.java index 28d6cad18d..47a0f9e7c8 100644 --- a/server/src/com/vaadin/data/Container.java +++ b/server/src/com/vaadin/data/Container.java @@ -503,17 +503,13 @@ public interface Container extends Serializable { * container, starting with the item id at startIndex.
*
* - * Implementations should return the exact number of item ids given by - * numberOfItems. The returned list must hence contain all - * of the item ids from the range:
+ * Implementations should return at most numberOfItems item + * ids, but can contain less if the container has less items than + * required to fulfill the request. The returned list must hence contain + * all of the item ids from the range:
*
* startIndex to - * startIndex + (numberOfItems-1).
- *
- * - * The returned list must contain all of the requested item ids or throw - * a {@link RangeOutOfContainerBoundsException} to indicate that the - * container does not contain all the requested item ids.
+ * max(startIndex + (numberOfItems-1), container.size()-1).
*
* For quick migration to new API see: * {@link ContainerHelpers#getItemIdsUsingGetIdByIndex(int, int, Indexed)} @@ -521,8 +517,6 @@ public interface Container extends Serializable { *
* Throws: {@link IllegalArgumentException} if * numberOfItems is < 0
- * Throws: {@link RangeOutOfContainerBoundsException} if all of - * the requested item ids cannot be fetched
* Throws: {@link IndexOutOfBoundsException} if * startIndex is outside the range of the container. (i.e. * startIndex < 0 || container.size()-1 < startIndex) @@ -532,8 +526,8 @@ public interface Container extends Serializable { * @param numberOfItems * the number of consecutive item ids to get from the given * start index, must be >= 0 - * @return List containing all of the requested item ids or empty list - * if numberOfItems == 0; not null + * @return List containing the requested item ids or empty list if + * numberOfItems == 0; not null * * @since 7.0 */ diff --git a/server/src/com/vaadin/data/ContainerHelpers.java b/server/src/com/vaadin/data/ContainerHelpers.java index 9ec2da4362..866fe97141 100644 --- a/server/src/com/vaadin/data/ContainerHelpers.java +++ b/server/src/com/vaadin/data/ContainerHelpers.java @@ -64,11 +64,7 @@ public class ContainerHelpers { int endIndex = startIndex + numberOfIds; if (endIndex > container.size()) { - throw new RangeOutOfContainerBoundsException( - "Cannot get all requested item ids from container. " - + "Container size might have changed, recalculate numberOfIds " - + "based on the actual container size!", - startIndex, numberOfIds, container.size()); + endIndex = container.size(); } ArrayList rangeOfIds = new ArrayList(); diff --git a/server/src/com/vaadin/data/util/AbstractInMemoryContainer.java b/server/src/com/vaadin/data/util/AbstractInMemoryContainer.java index dbfcad3982..be8abe0c75 100644 --- a/server/src/com/vaadin/data/util/AbstractInMemoryContainer.java +++ b/server/src/com/vaadin/data/util/AbstractInMemoryContainer.java @@ -26,7 +26,6 @@ import java.util.Set; import com.vaadin.data.Container; import com.vaadin.data.Container.ItemSetChangeNotifier; import com.vaadin.data.Item; -import com.vaadin.data.RangeOutOfContainerBoundsException; import com.vaadin.data.util.filter.SimpleStringFilter; import com.vaadin.data.util.filter.UnsupportedFilterException; @@ -278,11 +277,7 @@ public abstract class AbstractInMemoryContainer getVisibleItemIds().size()) { - throw new RangeOutOfContainerBoundsException( - "Cannot get all requested item ids from container. " - + "Container size might have changed, recalculate numberOfIds " - + "based on the actual container size!", - startIndex, numberOfIds, getVisibleItemIds().size()); + endIndex = getVisibleItemIds().size(); } return Collections.unmodifiableList(getVisibleItemIds().subList( diff --git a/server/tests/src/com/vaadin/data/util/TestIndexedContainer.java b/server/tests/src/com/vaadin/data/util/TestIndexedContainer.java index da2e2feac7..20aadfcb8b 100644 --- a/server/tests/src/com/vaadin/data/util/TestIndexedContainer.java +++ b/server/tests/src/com/vaadin/data/util/TestIndexedContainer.java @@ -2,8 +2,9 @@ package com.vaadin.data.util; import java.util.List; +import junit.framework.Assert; + import com.vaadin.data.Item; -import com.vaadin.data.RangeOutOfContainerBoundsException; public class TestIndexedContainer extends AbstractInMemoryContainerTest { @@ -342,22 +343,9 @@ public class TestIndexedContainer extends AbstractInMemoryContainerTest { public void testGetItemIdsRangeIndexOutOfBoundsDueToSizeChange() { IndexedContainer ic = new IndexedContainer(); ic.addItem(new Object()); - try { - ic.getItemIds(0, 10); - fail("Container returned items when the range was >> container size"); - } catch (RangeOutOfContainerBoundsException e) { - // This is expected... - assertTrue(e.isAdditionalParametersSet()); - assertEquals(0, e.getStartIndex()); - assertEquals(10, e.getNumberOfIds()); - assertEquals(1, e.getContainerCurrentSize()); - - } catch (IndexOutOfBoundsException e) { - fail("Container threw wrong exception when the range exceeded container size... "); - } catch (Exception e) { - // Should not happen! - fail("Container threw unspecified exception when fetching a range of items and the range started from -1"); - } + Assert.assertEquals( + "Container returned too many items when the range was >> container size", + 1, ic.getItemIds(0, 10).size()); } // Ticket 8028 -- 2.39.5