diff options
author | Henri Sara <hesara@vaadin.com> | 2012-08-31 13:29:42 +0300 |
---|---|---|
committer | Henri Sara <hesara@vaadin.com> | 2012-08-31 14:09:58 +0300 |
commit | 01646b14df5708e50e4eb031ec5b4f0da4ab5d15 (patch) | |
tree | aea2f654ed4f0ddca0f61708d1a3be8edb09deaf /server/src/com/vaadin/data/ContainerHelpers.java | |
parent | 61a1218d388b85b496e0fd98f2a5c5226ab4f83d (diff) | |
download | vaadin-framework-01646b14df5708e50e4eb031ec5b4f0da4ab5d15.tar.gz vaadin-framework-01646b14df5708e50e4eb031ec5b4f0da4ab5d15.zip |
Add Container.Indexed.getItemIds(int, int) for a range of items (#8028)
This permits optimization of data fetches from various containers
where getting single items by index in a loop might be costly.
Also add a helper method in ContainerHelpers to make it easier to
implement the new method where performance of fetching an id by index is
not an issue. SQLContainer still uses this helper instead of an
optimized implementation.
Diffstat (limited to 'server/src/com/vaadin/data/ContainerHelpers.java')
-rw-r--r-- | server/src/com/vaadin/data/ContainerHelpers.java | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/server/src/com/vaadin/data/ContainerHelpers.java b/server/src/com/vaadin/data/ContainerHelpers.java new file mode 100644 index 0000000000..9ec2da4362 --- /dev/null +++ b/server/src/com/vaadin/data/ContainerHelpers.java @@ -0,0 +1,91 @@ +package com.vaadin.data; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +import com.vaadin.data.Container.Indexed; + +/** + * Contains helper methods for containers that can be used to ease development + * of containers in Vaadin. + * + * @since 7.0 + */ +public class ContainerHelpers { + + /** + * Get a range of item ids from the container using + * {@link Indexed#getIdByIndex(int)}. This is just a helper method to aid + * developers to quickly add the required functionality to a Container + * during development. This should not be used in a "finished product" + * unless fetching an id for an index is very inexpensive because a separate + * request will be performed for each index in the range. + * + * @param startIndex + * index of the first item id to get + * @param numberOfIds + * the number of consecutive items whose ids should be returned + * @param container + * the container from which the items should be fetched + * @return A list of item ids in the range specified + */ + public static List<?> getItemIdsUsingGetIdByIndex(int startIndex, + int numberOfIds, Container.Indexed container) { + + if (container == null) { + throw new IllegalArgumentException( + "The given container cannot be null!"); + } + + if (startIndex < 0) { + throw new IndexOutOfBoundsException( + "Start index cannot be negative! startIndex=" + startIndex); + } + + if (startIndex > container.size()) { + throw new IndexOutOfBoundsException( + "Start index exceeds container size! startIndex=" + + startIndex + " containerLastItemIndex=" + + (container.size() - 1)); + } + + if (numberOfIds < 1) { + if (numberOfIds == 0) { + return Collections.emptyList(); + } + + throw new IllegalArgumentException( + "Cannot get negative amount of items! numberOfItems=" + + numberOfIds); + } + + // not included in the range + 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()); + } + + ArrayList<Object> rangeOfIds = new ArrayList<Object>(); + for (int i = startIndex; i < endIndex; i++) { + Object idByIndex = container.getIdByIndex(i); + if (idByIndex == null) { + throw new RuntimeException( + "Unable to get item id for index: " + + i + + " from container using Container.Indexed#getIdByIndex() " + + "even though container.size() > endIndex. " + + "Returned item id was null. " + + "Check your container implementation!"); + } + rangeOfIds.add(idByIndex); + } + + return Collections.unmodifiableList(rangeOfIds); + } +} |