aboutsummaryrefslogtreecommitdiffstats
path: root/server/src/main/java/com/vaadin/data/HasItems.java
blob: 0425c6e55e4f9f999c4c7f8b64088e5e4d9b1d63 (plain)
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
/*
 * Copyright 2000-2022 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.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import com.vaadin.data.provider.BackEndDataProvider;
import com.vaadin.data.provider.DataProvider;
import com.vaadin.data.provider.ListDataProvider;
import com.vaadin.ui.Component;

/**
 * A component that displays a collection of items.
 *
 * @author Vaadin Ltd
 *
 * @since 8.0
 *
 * @param <T>
 *            the type of the displayed item
 */
public interface HasItems<T> extends Component {

    /**
     * Returns the source of data items used by this listing.
     *
     * @return the data provider, not null
     */
    public DataProvider<T, ?> getDataProvider();

    /**
     * Sets the data items of this component provided as a collection.
     * <p>
     * The provided items are wrapped into a {@link ListDataProvider} and this
     * instance is used as a data provider for the
     * {@link #setDataProvider(DataProvider)} method. It means that the items
     * collection can be accessed later on via
     * {@link ListDataProvider#getItems()}:
     *
     * <pre>
     * <code>
     * HasDataProvider&lt;String&gt; listing = new CheckBoxGroup&lt;&gt;();
     * listing.setItems(Arrays.asList("a","b"));
     * ...
     *
     * Collection<String> collection = ((ListDataProvider<String>)listing.getDataProvider()).getItems();
     * </code>
     * </pre>
     * <p>
     * The provided collection instance may be used as-is. Subsequent
     * modification of the collection might cause inconsistent data to be shown
     * in the component unless it is explicitly instructed to read the data
     * again.
     *
     * @param items
     *            the data items to display, not null
     *
     */
    public void setItems(Collection<T> items);

    /**
     * Sets the data items of this listing.
     * <p>
     * The provided items are wrapped into a {@link ListDataProvider} and this
     * instance is used as a data provider for the
     * {@link #setDataProvider(DataProvider)} method. It means that the items
     * collection can be accessed later on via
     * {@link ListDataProvider#getItems()}:
     *
     * <pre>
     * <code>
     * HasDataProvider<String> listing = new CheckBoxGroup<>();
     * listing.setItems("a","b");
     * ...
     *
     * Collection<String> collection = ((ListDataProvider<String>)listing.getDataProvider()).getItems();
     * </code>
     * </pre>
     * <p>
     *
     * @see #setItems(Collection)
     *
     * @param items
     *            the data items to display
     */
    public default void setItems(@SuppressWarnings("unchecked") T... items) {
        setItems(new ArrayList<>(Arrays.asList(items)));
    }

    /**
     * Sets the data items of this listing provided as a stream.
     * <p>
     * This is just a shorthand for {@link #setItems(Collection)}, that
     * <b>collects objects in the stream to a list</b>. Thus, using this method,
     * instead of its array and Collection variations, doesn't save any memory.
     * If you have a large data set to bind, using a lazy data provider is
     * recommended. See {@link BackEndDataProvider} for more info.
     * <p>
     * The provided items are wrapped into a {@link ListDataProvider} and this
     * instance is used as a data provider for the
     * {@link #setDataProvider(DataProvider)} method. It means that the items
     * collection can be accessed later on via
     * {@link ListDataProvider#getItems()}:
     *
     * <pre>
     * <code>
     * HasDataProvider&lt;String&gt; listing = new CheckBoxGroup<&gt;();
     * listing.setItems(Stream.of("a","b"));
     * ...
     *
     * Collection<String> collection = ((ListDataProvider&lt;String&gt;)listing.getDataProvider()).getItems();
     * </code>
     * </pre>
     * <p>
     *
     * @see #setItems(Collection)
     *
     * @param streamOfItems
     *            the stream of data items to display, not {@code null}
     */
    public default void setItems(Stream<T> streamOfItems) {
        setItems(streamOfItems.collect(Collectors.toList()));
    }
}