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