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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
|
/*
* Copyright 2000-2016 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;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import com.vaadin.data.provider.TreeDataProvider;
/**
* Class for representing hierarchical data.
* <p>
* Typically used as a backing data source for {@link TreeDataProvider}.
*
* @author Vaadin Ltd
* @since 8.1
*
* @param <T>
* data type
*/
public class TreeData<T> implements Serializable {
private static class HierarchyWrapper<T> implements Serializable {
private T item;
private T parent;
private List<T> children;
public HierarchyWrapper(T item, T parent) {
this.item = item;
this.parent = parent;
children = new ArrayList<>();
}
public T getItem() {
return item;
}
public void setItem(T item) {
this.item = item;
}
public T getParent() {
return parent;
}
public void setParent(T parent) {
this.parent = parent;
}
public List<T> getChildren() {
return children;
}
public void setChildren(List<T> children) {
this.children = children;
}
public void addChild(T child) {
children.add(child);
}
public void removeChild(T child) {
children.remove(child);
}
}
private final Map<T, HierarchyWrapper<T>> itemToWrapperMap;
/**
* Creates an initially empty hierarchical data representation to which
* items can be added or removed.
*/
public TreeData() {
itemToWrapperMap = new LinkedHashMap<>();
itemToWrapperMap.put(null, new HierarchyWrapper<>(null, null));
}
/**
* Adds the items as root items to this structure.
*
* @param items
* the items to add
* @return this
*
* @throws IllegalArgumentException
* if any of the given items have already been added to this
* structure
* @throws NullPointerException
* if any of the items are {code null}
*/
public TreeData<T> addRootItems(T... items) {
addItems(null, items);
return this;
}
/**
* Adds the items of the given collection as root items to this structure.
*
* @param items
* the collection of items to add
* @return this
*
* @throws IllegalArgumentException
* if any of the given items have already been added to this
* structure
* @throws NullPointerException
* if any of the items are {code null}
*/
public TreeData<T> addRootItems(Collection<T> items) {
addItems(null, items);
return this;
}
/**
* Adds the items of the given stream as root items to this structure.
*
* @param items
* the stream of root items to add
* @return this
*
* @throws IllegalArgumentException
* if any of the given items have already been added to this
* structure
* @throws NullPointerException
* if any of the items are {code null}
*/
public TreeData<T> addRootItems(Stream<T> items) {
addItems(null, items);
return this;
}
/**
* Adds a data item as a child of {@code parent}. Call with {@code null} as
* parent to add a root level item. The given parent item must already exist
* in this structure, and an item can only be added to this structure once.
*
* @param parent
* the parent item for which the items are added as children
* @param item
* the item to add
* @return this
*
* @throws IllegalArgumentException
* if parent is not null and not already added to this structure
* @throws IllegalArgumentException
* if the item has already been added to this structure
* @throws NullPointerException
* if item is null
*/
public TreeData<T> addItem(T parent, T item) {
Objects.requireNonNull(item, "Item cannot be null");
if (parent != null && !contains(parent)) {
throw new IllegalArgumentException(
"Parent needs to be added before children. "
+ "To add root items, call with parent as null");
}
if (contains(item)) {
throw new IllegalArgumentException(
"Cannot add the same item multiple times: " + item);
}
putItem(item, parent);
return this;
}
/**
* Adds a list of data items as children of {@code parent}. Call with
* {@code null} as parent to add root level items. The given parent item
* must already exist in this structure, and an item can only be added to
* this structure once.
*
* @param parent
* the parent item for which the items are added as children
* @param items
* the list of items to add
* @return this
*
* @throws IllegalArgumentException
* if parent is not null and not already added to this structure
* @throws IllegalArgumentException
* if any of the given items have already been added to this
* structure
* @throws NullPointerException
* if any of the items are null
*/
public TreeData<T> addItems(T parent,
@SuppressWarnings("unchecked") T... items) {
Arrays.asList(items).stream().forEach(item -> addItem(parent, item));
return this;
}
/**
* Adds a list of data items as children of {@code parent}. Call with
* {@code null} as parent to add root level items. The given parent item
* must already exist in this structure, and an item can only be added to
* this structure once.
*
* @param parent
* the parent item for which the items are added as children
* @param items
* the collection of items to add
* @return this
*
* @throws IllegalArgumentException
* if parent is not null and not already added to this structure
* @throws IllegalArgumentException
* if any of the given items have already been added to this
* structure
* @throws NullPointerException
* if any of the items are null
*/
public TreeData<T> addItems(T parent, Collection<T> items) {
items.stream().forEach(item -> addItem(parent, item));
return this;
}
/**
* Adds data items contained in a stream as children of {@code parent}. Call
* with {@code null} as parent to add root level items. The given parent
* item must already exist in this structure, and an item can only be added
* to this structure once.
*
* @param parent
* the parent item for which the items are added as children
* @param items
* stream of items to add
* @return this
*
* @throws IllegalArgumentException
* if parent is not null and not already added to this structure
* @throws IllegalArgumentException
* if any of the given items have already been added to this
* structure
* @throws NullPointerException
* if any of the items are null
*/
public TreeData<T> addItems(T parent, Stream<T> items) {
items.forEach(item -> addItem(parent, item));
return this;
}
/**
* Adds the given items as root items and uses the given value provider to
* recursively populate children of the root items.
*
* @param rootItems
* the root items to add
* @param childItemProvider
* the value provider used to recursively populate this TreeData
* from the given root items
* @return this
*/
public TreeData<T> addItems(Collection<T> rootItems,
ValueProvider<T, Collection<T>> childItemProvider) {
rootItems.forEach(item -> {
addItem(null, item);
Collection<T> childItems = childItemProvider.apply(item);
addItems(item, childItems);
addItemsRecursively(childItems, childItemProvider);
});
return this;
}
/**
* Adds the given items as root items and uses the given value provider to
* recursively populate children of the root items.
*
* @param rootItems
* the root items to add
* @param childItemProvider
* the value provider used to recursively populate this TreeData
* from the given root items
* @return this
*/
public TreeData<T> addItems(Stream<T> rootItems,
ValueProvider<T, Stream<T>> childItemProvider) {
// Must collect to lists since the algorithm iterates multiple times
return addItems(rootItems.collect(Collectors.toList()),
item -> childItemProvider.apply(item)
.collect(Collectors.toList()));
}
/**
* Remove a given item from this structure. Additionally, this will
* recursively remove any descendants of the item.
*
* @param item
* the item to remove, or null to clear all data
* @return this
*
* @throws IllegalArgumentException
* if the item does not exist in this structure
*/
public TreeData<T> removeItem(T item) {
if (!contains(item)) {
throw new IllegalArgumentException(
"Item '" + item + "' not in the hierarchy");
}
new ArrayList<>(getChildren(item)).forEach(child -> removeItem(child));
itemToWrapperMap.get(itemToWrapperMap.get(item).getParent())
.removeChild(item);
if (item != null) {
// remove non root item from backing map
itemToWrapperMap.remove(item);
}
return this;
}
/**
* Clear all items from this structure. Shorthand for calling
* {@link #removeItem(Object)} with null.
*
* @return this
*/
public TreeData<T> clear() {
removeItem(null);
return this;
}
/**
* Gets the root items of this structure.
*
* @return an unmodifiable list of root items of this structure
*/
public List<T> getRootItems() {
return getChildren(null);
}
/**
* Get the immediate child items for the given item.
*
* @param item
* the item for which to retrieve child items for, null to
* retrieve all root items
* @return an unmodifiable list of child items for the given item
*
* @throws IllegalArgumentException
* if the item does not exist in this structure
*/
public List<T> getChildren(T item) {
if (!contains(item)) {
throw new IllegalArgumentException(
"Item '" + item + "' not in the hierarchy");
}
return Collections
.unmodifiableList(itemToWrapperMap.get(item).getChildren());
}
/**
* Check whether the given item is in this hierarchy.
*
* @param item
* the item to check
* @return {@code true} if the item is in this hierarchy, {@code false} if
* not
*/
public boolean contains(T item) {
return itemToWrapperMap.containsKey(item);
}
private void putItem(T item, T parent) {
HierarchyWrapper<T> wrappedItem = new HierarchyWrapper<>(item, parent);
if (itemToWrapperMap.containsKey(parent)) {
itemToWrapperMap.get(parent).addChild(item);
}
itemToWrapperMap.put(item, wrappedItem);
}
private void addItemsRecursively(Collection<T> items,
ValueProvider<T, Collection<T>> childItemProvider) {
items.forEach(item -> {
Collection<T> childItems = childItemProvider.apply(item);
addItems(item, childItems);
addItemsRecursively(childItems, childItemProvider);
});
}
}
|