You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ContainerHelpers.java 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * Copyright 2000-2014 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.data;
  17. import java.io.Serializable;
  18. import java.util.ArrayList;
  19. import java.util.Collections;
  20. import java.util.List;
  21. import com.vaadin.data.Container.Indexed;
  22. /**
  23. * Contains helper methods for containers that can be used to ease development
  24. * of containers in Vaadin.
  25. *
  26. * @since 7.0
  27. */
  28. public class ContainerHelpers implements Serializable {
  29. /**
  30. * Get a range of item ids from the container using
  31. * {@link Indexed#getIdByIndex(int)}. This is just a helper method to aid
  32. * developers to quickly add the required functionality to a Container
  33. * during development. This should not be used in a "finished product"
  34. * unless fetching an id for an index is very inexpensive because a separate
  35. * request will be performed for each index in the range.
  36. *
  37. * @param startIndex
  38. * index of the first item id to get
  39. * @param numberOfIds
  40. * the number of consecutive items whose ids should be returned
  41. * @param container
  42. * the container from which the items should be fetched
  43. * @return A list of item ids in the range specified
  44. */
  45. public static List<?> getItemIdsUsingGetIdByIndex(int startIndex,
  46. int numberOfIds, Container.Indexed container) {
  47. if (container == null) {
  48. throw new IllegalArgumentException(
  49. "The given container cannot be null!");
  50. }
  51. if (startIndex < 0) {
  52. throw new IndexOutOfBoundsException(
  53. "Start index cannot be negative! startIndex=" + startIndex);
  54. }
  55. if (startIndex > container.size()) {
  56. throw new IndexOutOfBoundsException(
  57. "Start index exceeds container size! startIndex="
  58. + startIndex + " containerLastItemIndex="
  59. + (container.size() - 1));
  60. }
  61. if (numberOfIds < 1) {
  62. if (numberOfIds == 0) {
  63. return Collections.emptyList();
  64. }
  65. throw new IllegalArgumentException(
  66. "Cannot get negative amount of items! numberOfItems="
  67. + numberOfIds);
  68. }
  69. // not included in the range
  70. int endIndex = startIndex + numberOfIds;
  71. if (endIndex > container.size()) {
  72. endIndex = container.size();
  73. }
  74. ArrayList<Object> rangeOfIds = new ArrayList<Object>();
  75. for (int i = startIndex; i < endIndex; i++) {
  76. Object idByIndex = container.getIdByIndex(i);
  77. if (idByIndex == null) {
  78. throw new RuntimeException(
  79. "Unable to get item id for index: "
  80. + i
  81. + " from container using Container.Indexed#getIdByIndex() "
  82. + "even though container.size() > endIndex. "
  83. + "Returned item id was null. "
  84. + "Check your container implementation!");
  85. }
  86. rangeOfIds.add(idByIndex);
  87. }
  88. return Collections.unmodifiableList(rangeOfIds);
  89. }
  90. }