Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

BeanItemContainer.java 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. @VaadinApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.data.util;
  5. import java.util.Collection;
  6. /**
  7. * An in-memory container for JavaBeans.
  8. *
  9. * <p>
  10. * The properties of the container are determined automatically by introspecting
  11. * the used JavaBean class. Only beans of the same type can be added to the
  12. * container.
  13. * </p>
  14. *
  15. * <p>
  16. * BeanItemContainer uses the beans themselves as identifiers. The
  17. * {@link Object#hashCode()} of a bean is used when storing and looking up beans
  18. * so it must not change during the lifetime of the bean (it should not depend
  19. * on any part of the bean that can be modified). Typically this restricts the
  20. * implementation of {@link Object#equals(Object)} as well in order for it to
  21. * fulfill the contract between {@code equals()} and {@code hashCode()}.
  22. * </p>
  23. *
  24. * <p>
  25. * To add items to the container, use the methods {@link #addBean(Object)},
  26. * {@link #addBeanAfter(Object, Object)} and {@link #addBeanAt(int, Object)}.
  27. * Also {@link #addItem(Object)}, {@link #addItemAfter(Object, Object)} and
  28. * {@link #addItemAt(int, Object)} can be used as synonyms for them.
  29. * </p>
  30. *
  31. * <p>
  32. * It is not possible to add additional properties to the container and nested
  33. * bean properties are not supported.
  34. * </p>
  35. *
  36. * @param <BEANTYPE>
  37. * The type of the Bean
  38. *
  39. * @since 5.4
  40. */
  41. @SuppressWarnings("serial")
  42. public class BeanItemContainer<BEANTYPE> extends
  43. AbstractBeanContainer<BEANTYPE, BEANTYPE> {
  44. /**
  45. * Bean identity resolver that returns the bean itself as its item
  46. * identifier.
  47. *
  48. * This corresponds to the old behavior of {@link BeanItemContainer}, and
  49. * requires suitable (identity-based) equals() and hashCode() methods on the
  50. * beans.
  51. *
  52. * @param <BT>
  53. *
  54. * @since 6.5
  55. */
  56. private static class IdentityBeanIdResolver<BT> implements
  57. BeanIdResolver<BT, BT> {
  58. @Override
  59. public BT getIdForBean(BT bean) {
  60. return bean;
  61. }
  62. }
  63. /**
  64. * Constructs a {@code BeanItemContainer} for beans of the given type.
  65. *
  66. * @param type
  67. * the type of the beans that will be added to the container.
  68. * @throws IllegalArgumentException
  69. * If {@code type} is null
  70. */
  71. public BeanItemContainer(Class<? super BEANTYPE> type)
  72. throws IllegalArgumentException {
  73. super(type);
  74. super.setBeanIdResolver(new IdentityBeanIdResolver<BEANTYPE>());
  75. }
  76. /**
  77. * Constructs a {@code BeanItemContainer} and adds the given beans to it.
  78. * The collection must not be empty.
  79. * {@link BeanItemContainer#BeanItemContainer(Class)} can be used for
  80. * creating an initially empty {@code BeanItemContainer}.
  81. *
  82. * Note that when using this constructor, the actual class of the first item
  83. * in the collection is used to determine the bean properties supported by
  84. * the container instance, and only beans of that class or its subclasses
  85. * can be added to the collection. If this is problematic or empty
  86. * collections need to be supported, use {@link #BeanItemContainer(Class)}
  87. * and {@link #addAll(Collection)} instead.
  88. *
  89. * @param collection
  90. * a non empty {@link Collection} of beans.
  91. * @throws IllegalArgumentException
  92. * If the collection is null or empty.
  93. *
  94. * @deprecated use {@link #BeanItemContainer(Class, Collection)} instead
  95. */
  96. @SuppressWarnings("unchecked")
  97. @Deprecated
  98. public BeanItemContainer(Collection<? extends BEANTYPE> collection)
  99. throws IllegalArgumentException {
  100. // must assume the class is BT
  101. // the class information is erased by the compiler
  102. this((Class<BEANTYPE>) getBeanClassForCollection(collection),
  103. collection);
  104. }
  105. /**
  106. * Internal helper method to support the deprecated {@link Collection}
  107. * container.
  108. *
  109. * @param <BT>
  110. * @param collection
  111. * @return
  112. * @throws IllegalArgumentException
  113. */
  114. @SuppressWarnings("unchecked")
  115. @Deprecated
  116. private static <BT> Class<? extends BT> getBeanClassForCollection(
  117. Collection<? extends BT> collection)
  118. throws IllegalArgumentException {
  119. if (collection == null || collection.isEmpty()) {
  120. throw new IllegalArgumentException(
  121. "The collection passed to BeanItemContainer constructor must not be null or empty. Use the other BeanItemContainer constructor.");
  122. }
  123. return (Class<? extends BT>) collection.iterator().next().getClass();
  124. }
  125. /**
  126. * Constructs a {@code BeanItemContainer} and adds the given beans to it.
  127. *
  128. * @param type
  129. * the type of the beans that will be added to the container.
  130. * @param collection
  131. * a {@link Collection} of beans (can be empty or null).
  132. * @throws IllegalArgumentException
  133. * If {@code type} is null
  134. */
  135. public BeanItemContainer(Class<? super BEANTYPE> type,
  136. Collection<? extends BEANTYPE> collection)
  137. throws IllegalArgumentException {
  138. super(type);
  139. super.setBeanIdResolver(new IdentityBeanIdResolver<BEANTYPE>());
  140. if (collection != null) {
  141. addAll(collection);
  142. }
  143. }
  144. /**
  145. * Adds all the beans from a {@link Collection} in one go. More efficient
  146. * than adding them one by one.
  147. *
  148. * @param collection
  149. * The collection of beans to add. Must not be null.
  150. */
  151. @Override
  152. public void addAll(Collection<? extends BEANTYPE> collection) {
  153. super.addAll(collection);
  154. }
  155. /**
  156. * Adds the bean after the given bean.
  157. *
  158. * The bean is used both as the item contents and as the item identifier.
  159. *
  160. * @param previousItemId
  161. * the bean (of type BT) after which to add newItemId
  162. * @param newItemId
  163. * the bean (of type BT) to add (not null)
  164. *
  165. * @see com.vaadin.data.Container.Ordered#addItemAfter(Object, Object)
  166. */
  167. @Override
  168. @SuppressWarnings("unchecked")
  169. public BeanItem<BEANTYPE> addItemAfter(Object previousItemId,
  170. Object newItemId) throws IllegalArgumentException {
  171. return super.addBeanAfter((BEANTYPE) previousItemId,
  172. (BEANTYPE) newItemId);
  173. }
  174. /**
  175. * Adds a new bean at the given index.
  176. *
  177. * The bean is used both as the item contents and as the item identifier.
  178. *
  179. * @param index
  180. * Index at which the bean should be added.
  181. * @param newItemId
  182. * The bean to add to the container.
  183. * @return Returns the new BeanItem or null if the operation fails.
  184. */
  185. @Override
  186. @SuppressWarnings("unchecked")
  187. public BeanItem<BEANTYPE> addItemAt(int index, Object newItemId)
  188. throws IllegalArgumentException {
  189. return super.addBeanAt(index, (BEANTYPE) newItemId);
  190. }
  191. /**
  192. * Adds the bean to the Container.
  193. *
  194. * The bean is used both as the item contents and as the item identifier.
  195. *
  196. * @see com.vaadin.data.Container#addItem(Object)
  197. */
  198. @Override
  199. @SuppressWarnings("unchecked")
  200. public BeanItem<BEANTYPE> addItem(Object itemId) {
  201. return super.addBean((BEANTYPE) itemId);
  202. }
  203. /**
  204. * Adds the bean to the Container.
  205. *
  206. * The bean is used both as the item contents and as the item identifier.
  207. *
  208. * @see com.vaadin.data.Container#addItem(Object)
  209. */
  210. @Override
  211. public BeanItem<BEANTYPE> addBean(BEANTYPE bean) {
  212. return addItem(bean);
  213. }
  214. /**
  215. * Unsupported in BeanItemContainer.
  216. */
  217. @Override
  218. protected void setBeanIdResolver(
  219. AbstractBeanContainer.BeanIdResolver<BEANTYPE, BEANTYPE> beanIdResolver)
  220. throws UnsupportedOperationException {
  221. throw new UnsupportedOperationException(
  222. "BeanItemContainer always uses an IdentityBeanIdResolver");
  223. }
  224. }