Skip to content

Commit

Permalink
refactor: make haswidgets api accept collections (#6837)
Browse files Browse the repository at this point in the history
  • Loading branch information
ugur-vaadin authored Nov 20, 2024
1 parent 631870e commit ba11f24
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -133,14 +134,11 @@ public List<DashboardWidget> getWidgets() {
}

@Override
public void add(DashboardWidget... widgets) {
public void add(Collection<DashboardWidget> widgets) {
Objects.requireNonNull(widgets, "Widgets to add cannot be null.");
List<DashboardWidget> toAdd = new ArrayList<>(widgets.length);
for (DashboardWidget widget : widgets) {
Objects.requireNonNull(widget, "Widget to add cannot be null.");
toAdd.add(widget);
}
toAdd.forEach(this::doAddWidget);
widgets.forEach(widget -> Objects.requireNonNull(widget,
"Widget to add cannot be null."));
widgets.forEach(this::doAddWidget);
updateClient();
}

Expand Down Expand Up @@ -181,9 +179,9 @@ public void addWidgetAtIndex(int index, DashboardWidget widget) {
}

@Override
public void remove(DashboardWidget... widgets) {
public void remove(Collection<DashboardWidget> widgets) {
Objects.requireNonNull(widgets, "Widgets to remove cannot be null.");
List<DashboardWidget> toRemove = new ArrayList<>(widgets.length);
List<DashboardWidget> toRemove = new ArrayList<>(widgets.size());
for (DashboardWidget widget : widgets) {
Objects.requireNonNull(widget, "Widget to remove cannot be null.");
Element parent = widget.getElement().getParent();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
package com.vaadin.flow.component.dashboard;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
Expand All @@ -20,7 +21,6 @@
import com.vaadin.flow.component.Tag;
import com.vaadin.flow.component.dependency.JsModule;
import com.vaadin.flow.component.dependency.NpmPackage;
import com.vaadin.flow.dom.Element;

/**
* DashboardSection is a container for organizing multiple
Expand Down Expand Up @@ -91,14 +91,11 @@ public Stream<Component> getChildren() {
}

@Override
public void add(DashboardWidget... widgets) {
public void add(Collection<DashboardWidget> widgets) {
Objects.requireNonNull(widgets, "Widgets to add cannot be null.");
List<DashboardWidget> toAdd = new ArrayList<>(widgets.length);
for (DashboardWidget widget : widgets) {
Objects.requireNonNull(widget, "Widget to add cannot be null.");
toAdd.add(widget);
}
toAdd.forEach(this::doAddWidget);
widgets.forEach(widget -> Objects.requireNonNull(widget,
"Widget to add cannot be null."));
widgets.forEach(this::doAddWidget);
updateClient();
}

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

@Override
public void remove(DashboardWidget... widgets) {
public void remove(Collection<DashboardWidget> widgets) {
Objects.requireNonNull(widgets, "Widgets to remove cannot be null.");
List<DashboardWidget> toRemove = new ArrayList<>(widgets.length);
var toRemove = new ArrayList<DashboardWidget>(widgets.size());
for (DashboardWidget widget : widgets) {
Objects.requireNonNull(widget, "Widget to remove cannot be null.");
Element parent = widget.getElement().getParent();
var parent = widget.getElement().getParent();
if (parent == null) {
LoggerFactory.getLogger(getClass()).debug(
"Removal of a widget with no parent does nothing.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
package com.vaadin.flow.component.dashboard;

import java.io.Serializable;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Objects;

/**
* HasWidgets is an interface for components that can contain and manage
Expand All @@ -35,7 +38,18 @@ public interface HasWidgets extends Serializable {
* @param widgets
* the widgets to add, not {@code null}
*/
void add(DashboardWidget... widgets);
default void add(DashboardWidget... widgets) {
Objects.requireNonNull(widgets, "Widgets to add cannot be null.");
add(Arrays.asList(widgets));
}

/**
* Adds the given widgets to this component.
*
* @param widgets
* the widgets to add, not {@code null}
*/
void add(Collection<DashboardWidget> widgets);

/**
* Adds the given widget as child of this component at the specific index.
Expand All @@ -60,7 +74,21 @@ public interface HasWidgets extends Serializable {
* if there is a widget whose non {@code null} parent is not
* this component
*/
void remove(DashboardWidget... widgets);
default void remove(DashboardWidget... widgets) {
Objects.requireNonNull(widgets, "Widgets to remove cannot be null.");
remove(Arrays.asList(widgets));
}

/**
* Removes the given widgets from this component.
*
* @param widgets
* the widgets to remove, not {@code null}
* @throws IllegalArgumentException
* if there is a widget whose non {@code null} parent is not
* this component
*/
void remove(Collection<DashboardWidget> widgets);

/**
* Removes all widgets from this component.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
package com.vaadin.flow.component.dashboard;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
Expand All @@ -35,20 +36,35 @@ public void setup() {
}

@Test
public void addWidget_widgetIsAdded() {
DashboardWidget widget1 = getNewWidget();
DashboardWidget widget2 = getNewWidget();
public void addWidgetInArray_widgetIsAdded() {
var widget1 = getNewWidget();
var widget2 = getNewWidget();
dashboard.add(widget1, widget2);
fakeClientCommunication();
assertChildComponents(dashboard, widget1, widget2);
}

@Test
public void addWidgetInCollection_widgetIsAdded() {
var widget1 = getNewWidget();
var widget2 = getNewWidget();
dashboard.add(List.of(widget1, widget2));
fakeClientCommunication();
assertChildComponents(dashboard, widget1, widget2);
}

@Test
public void addNullWidget_exceptionIsThrown() {
Assert.assertThrows(NullPointerException.class,
() -> dashboard.add((DashboardWidget) null));
}

@Test
public void addNullCollection_exceptionIsThrown() {
Assert.assertThrows(NullPointerException.class,
() -> dashboard.add((Collection<DashboardWidget>) null));
}

@Test
public void addNullWidgetInArray_noWidgetIsAdded() {
DashboardWidget widget = getNewWidget();
Expand All @@ -61,6 +77,20 @@ public void addNullWidgetInArray_noWidgetIsAdded() {
assertChildComponents(dashboard);
}

@Test
public void addNullWidgetInCollection_noWidgetIsAdded() {
var widgets = new ArrayList<DashboardWidget>();
widgets.add(getNewWidget());
widgets.add(null);
try {
dashboard.add(widgets);
} catch (NullPointerException e) {
// Do nothing
}
fakeClientCommunication();
assertChildComponents(dashboard);
}

@Test
public void addWidgetAtIndex_widgetIsCorrectlyAdded() {
DashboardWidget widget1 = getNewWidget();
Expand Down Expand Up @@ -101,22 +131,39 @@ public void addNullWidgetAtIndex_exceptionIsThrown() {
}

@Test
public void removeWidget_widgetIsRemoved() {
DashboardWidget widget1 = getNewWidget();
DashboardWidget widget2 = getNewWidget();
public void removeWidgetInArray_widgetIsRemoved() {
var widget1 = getNewWidget();
var widget2 = getNewWidget();
dashboard.add(widget1, widget2);
fakeClientCommunication();
dashboard.remove(widget1);
fakeClientCommunication();
assertChildComponents(dashboard, widget2);
}

@Test
public void removeWidgetInCollection_widgetIsRemoved() {
var widget1 = getNewWidget();
var widget2 = getNewWidget();
dashboard.add(widget1, widget2);
fakeClientCommunication();
dashboard.remove(List.of(widget1));
fakeClientCommunication();
assertChildComponents(dashboard, widget2);
}

@Test
public void removeNullWidget_exceptionIsThrown() {
Assert.assertThrows(NullPointerException.class,
() -> dashboard.remove((DashboardWidget) null));
}

@Test
public void removeNullWidgetCollection_exceptionIsThrown() {
Assert.assertThrows(NullPointerException.class,
() -> dashboard.remove((Collection<DashboardWidget>) null));
}

@Test
public void removeAllWidgets_widgetsAreRemoved() {
DashboardWidget widget1 = getNewWidget();
Expand Down

0 comments on commit ba11f24

Please sign in to comment.