Skip to content

Commit ba11f24

Browse files
authored
refactor: make haswidgets api accept collections (#6837)
1 parent 631870e commit ba11f24

File tree

4 files changed

+98
-28
lines changed

4 files changed

+98
-28
lines changed

vaadin-dashboard-flow-parent/vaadin-dashboard-flow/src/main/java/com/vaadin/flow/component/dashboard/Dashboard.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import java.io.Serializable;
1212
import java.util.ArrayList;
13+
import java.util.Collection;
1314
import java.util.Collections;
1415
import java.util.List;
1516
import java.util.Map;
@@ -133,14 +134,11 @@ public List<DashboardWidget> getWidgets() {
133134
}
134135

135136
@Override
136-
public void add(DashboardWidget... widgets) {
137+
public void add(Collection<DashboardWidget> widgets) {
137138
Objects.requireNonNull(widgets, "Widgets to add cannot be null.");
138-
List<DashboardWidget> toAdd = new ArrayList<>(widgets.length);
139-
for (DashboardWidget widget : widgets) {
140-
Objects.requireNonNull(widget, "Widget to add cannot be null.");
141-
toAdd.add(widget);
142-
}
143-
toAdd.forEach(this::doAddWidget);
139+
widgets.forEach(widget -> Objects.requireNonNull(widget,
140+
"Widget to add cannot be null."));
141+
widgets.forEach(this::doAddWidget);
144142
updateClient();
145143
}
146144

@@ -181,9 +179,9 @@ public void addWidgetAtIndex(int index, DashboardWidget widget) {
181179
}
182180

183181
@Override
184-
public void remove(DashboardWidget... widgets) {
182+
public void remove(Collection<DashboardWidget> widgets) {
185183
Objects.requireNonNull(widgets, "Widgets to remove cannot be null.");
186-
List<DashboardWidget> toRemove = new ArrayList<>(widgets.length);
184+
List<DashboardWidget> toRemove = new ArrayList<>(widgets.size());
187185
for (DashboardWidget widget : widgets) {
188186
Objects.requireNonNull(widget, "Widget to remove cannot be null.");
189187
Element parent = widget.getElement().getParent();

vaadin-dashboard-flow-parent/vaadin-dashboard-flow/src/main/java/com/vaadin/flow/component/dashboard/DashboardSection.java

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
package com.vaadin.flow.component.dashboard;
1010

1111
import java.util.ArrayList;
12+
import java.util.Collection;
1213
import java.util.Collections;
1314
import java.util.List;
1415
import java.util.Objects;
@@ -20,7 +21,6 @@
2021
import com.vaadin.flow.component.Tag;
2122
import com.vaadin.flow.component.dependency.JsModule;
2223
import com.vaadin.flow.component.dependency.NpmPackage;
23-
import com.vaadin.flow.dom.Element;
2424

2525
/**
2626
* DashboardSection is a container for organizing multiple
@@ -91,14 +91,11 @@ public Stream<Component> getChildren() {
9191
}
9292

9393
@Override
94-
public void add(DashboardWidget... widgets) {
94+
public void add(Collection<DashboardWidget> widgets) {
9595
Objects.requireNonNull(widgets, "Widgets to add cannot be null.");
96-
List<DashboardWidget> toAdd = new ArrayList<>(widgets.length);
97-
for (DashboardWidget widget : widgets) {
98-
Objects.requireNonNull(widget, "Widget to add cannot be null.");
99-
toAdd.add(widget);
100-
}
101-
toAdd.forEach(this::doAddWidget);
96+
widgets.forEach(widget -> Objects.requireNonNull(widget,
97+
"Widget to add cannot be null."));
98+
widgets.forEach(this::doAddWidget);
10299
updateClient();
103100
}
104101

@@ -119,12 +116,12 @@ public void addWidgetAtIndex(int index, DashboardWidget widget) {
119116
}
120117

121118
@Override
122-
public void remove(DashboardWidget... widgets) {
119+
public void remove(Collection<DashboardWidget> widgets) {
123120
Objects.requireNonNull(widgets, "Widgets to remove cannot be null.");
124-
List<DashboardWidget> toRemove = new ArrayList<>(widgets.length);
121+
var toRemove = new ArrayList<DashboardWidget>(widgets.size());
125122
for (DashboardWidget widget : widgets) {
126123
Objects.requireNonNull(widget, "Widget to remove cannot be null.");
127-
Element parent = widget.getElement().getParent();
124+
var parent = widget.getElement().getParent();
128125
if (parent == null) {
129126
LoggerFactory.getLogger(getClass()).debug(
130127
"Removal of a widget with no parent does nothing.");

vaadin-dashboard-flow-parent/vaadin-dashboard-flow/src/main/java/com/vaadin/flow/component/dashboard/HasWidgets.java

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@
99
package com.vaadin.flow.component.dashboard;
1010

1111
import java.io.Serializable;
12+
import java.util.Arrays;
13+
import java.util.Collection;
1214
import java.util.List;
15+
import java.util.Objects;
1316

1417
/**
1518
* HasWidgets is an interface for components that can contain and manage
@@ -35,7 +38,18 @@ public interface HasWidgets extends Serializable {
3538
* @param widgets
3639
* the widgets to add, not {@code null}
3740
*/
38-
void add(DashboardWidget... widgets);
41+
default void add(DashboardWidget... widgets) {
42+
Objects.requireNonNull(widgets, "Widgets to add cannot be null.");
43+
add(Arrays.asList(widgets));
44+
}
45+
46+
/**
47+
* Adds the given widgets to this component.
48+
*
49+
* @param widgets
50+
* the widgets to add, not {@code null}
51+
*/
52+
void add(Collection<DashboardWidget> widgets);
3953

4054
/**
4155
* Adds the given widget as child of this component at the specific index.
@@ -60,7 +74,21 @@ public interface HasWidgets extends Serializable {
6074
* if there is a widget whose non {@code null} parent is not
6175
* this component
6276
*/
63-
void remove(DashboardWidget... widgets);
77+
default void remove(DashboardWidget... widgets) {
78+
Objects.requireNonNull(widgets, "Widgets to remove cannot be null.");
79+
remove(Arrays.asList(widgets));
80+
}
81+
82+
/**
83+
* Removes the given widgets from this component.
84+
*
85+
* @param widgets
86+
* the widgets to remove, not {@code null}
87+
* @throws IllegalArgumentException
88+
* if there is a widget whose non {@code null} parent is not
89+
* this component
90+
*/
91+
void remove(Collection<DashboardWidget> widgets);
6492

6593
/**
6694
* Removes all widgets from this component.

vaadin-dashboard-flow-parent/vaadin-dashboard-flow/src/test/java/com/vaadin/flow/component/dashboard/DashboardTest.java

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
package com.vaadin.flow.component.dashboard;
1010

1111
import java.util.ArrayList;
12+
import java.util.Collection;
1213
import java.util.List;
1314
import java.util.Set;
1415
import java.util.concurrent.atomic.AtomicInteger;
@@ -35,20 +36,35 @@ public void setup() {
3536
}
3637

3738
@Test
38-
public void addWidget_widgetIsAdded() {
39-
DashboardWidget widget1 = getNewWidget();
40-
DashboardWidget widget2 = getNewWidget();
39+
public void addWidgetInArray_widgetIsAdded() {
40+
var widget1 = getNewWidget();
41+
var widget2 = getNewWidget();
4142
dashboard.add(widget1, widget2);
4243
fakeClientCommunication();
4344
assertChildComponents(dashboard, widget1, widget2);
4445
}
4546

47+
@Test
48+
public void addWidgetInCollection_widgetIsAdded() {
49+
var widget1 = getNewWidget();
50+
var widget2 = getNewWidget();
51+
dashboard.add(List.of(widget1, widget2));
52+
fakeClientCommunication();
53+
assertChildComponents(dashboard, widget1, widget2);
54+
}
55+
4656
@Test
4757
public void addNullWidget_exceptionIsThrown() {
4858
Assert.assertThrows(NullPointerException.class,
4959
() -> dashboard.add((DashboardWidget) null));
5060
}
5161

62+
@Test
63+
public void addNullCollection_exceptionIsThrown() {
64+
Assert.assertThrows(NullPointerException.class,
65+
() -> dashboard.add((Collection<DashboardWidget>) null));
66+
}
67+
5268
@Test
5369
public void addNullWidgetInArray_noWidgetIsAdded() {
5470
DashboardWidget widget = getNewWidget();
@@ -61,6 +77,20 @@ public void addNullWidgetInArray_noWidgetIsAdded() {
6177
assertChildComponents(dashboard);
6278
}
6379

80+
@Test
81+
public void addNullWidgetInCollection_noWidgetIsAdded() {
82+
var widgets = new ArrayList<DashboardWidget>();
83+
widgets.add(getNewWidget());
84+
widgets.add(null);
85+
try {
86+
dashboard.add(widgets);
87+
} catch (NullPointerException e) {
88+
// Do nothing
89+
}
90+
fakeClientCommunication();
91+
assertChildComponents(dashboard);
92+
}
93+
6494
@Test
6595
public void addWidgetAtIndex_widgetIsCorrectlyAdded() {
6696
DashboardWidget widget1 = getNewWidget();
@@ -101,22 +131,39 @@ public void addNullWidgetAtIndex_exceptionIsThrown() {
101131
}
102132

103133
@Test
104-
public void removeWidget_widgetIsRemoved() {
105-
DashboardWidget widget1 = getNewWidget();
106-
DashboardWidget widget2 = getNewWidget();
134+
public void removeWidgetInArray_widgetIsRemoved() {
135+
var widget1 = getNewWidget();
136+
var widget2 = getNewWidget();
107137
dashboard.add(widget1, widget2);
108138
fakeClientCommunication();
109139
dashboard.remove(widget1);
110140
fakeClientCommunication();
111141
assertChildComponents(dashboard, widget2);
112142
}
113143

144+
@Test
145+
public void removeWidgetInCollection_widgetIsRemoved() {
146+
var widget1 = getNewWidget();
147+
var widget2 = getNewWidget();
148+
dashboard.add(widget1, widget2);
149+
fakeClientCommunication();
150+
dashboard.remove(List.of(widget1));
151+
fakeClientCommunication();
152+
assertChildComponents(dashboard, widget2);
153+
}
154+
114155
@Test
115156
public void removeNullWidget_exceptionIsThrown() {
116157
Assert.assertThrows(NullPointerException.class,
117158
() -> dashboard.remove((DashboardWidget) null));
118159
}
119160

161+
@Test
162+
public void removeNullWidgetCollection_exceptionIsThrown() {
163+
Assert.assertThrows(NullPointerException.class,
164+
() -> dashboard.remove((Collection<DashboardWidget>) null));
165+
}
166+
120167
@Test
121168
public void removeAllWidgets_widgetsAreRemoved() {
122169
DashboardWidget widget1 = getNewWidget();

0 commit comments

Comments
 (0)