Skip to content

Commit

Permalink
Update V1/V2 NavigationItemImpl to use child supplier
Browse files Browse the repository at this point in the history
Updates `NavigationItemImpl` constructor to accept a `Supplier` which
supplies a list of children instead of passing the list its self in
the constructor.

This allows the work of constructing a navigation sub-tree to be
deferred and only completed if the `NavigationItem#getChildren()`
method is called.

----
refs adobe#2526
  • Loading branch information
ky940819 committed Jun 11, 2023
1 parent 28efbf8 commit de66725
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 16 deletions.
Expand Up @@ -17,6 +17,7 @@

import java.util.Collections;
import java.util.List;
import java.util.function.Supplier;

import org.jetbrains.annotations.NotNull;

Expand All @@ -26,21 +27,63 @@
import com.day.cq.wcm.api.components.Component;
import com.fasterxml.jackson.annotation.JsonIgnore;

/**
* V1 Navigation Item Implementation.
*/
public class NavigationItemImpl extends PageListItemImpl implements NavigationItem {

protected List<NavigationItem> children = Collections.emptyList();
protected int level;
protected boolean active;
private boolean current;
/**
* List of children.
* Note - this will be null until populated from {@link #childrenSupplier} and so should remain private.
*/
private List<NavigationItem> children;

public NavigationItemImpl(Page page, boolean active, boolean current, @NotNull LinkManager linkManager, int level,
List<NavigationItem> children,
String parentId, Component component) {
/**
* Navigation level.
*/
private final int level;

/**
* Flag indicating if this navigation item is active.
*/
private final boolean active;

/**
* Flag indicating if this navigation item is for the current page.
*/
private final boolean current;

/**
* Supplier for child navigation items.
*/
@NotNull
private final Supplier<@NotNull List<NavigationItem>> childrenSupplier;

/**
* Construct a Navigation Item.
*
* @param page The page for which to create a navigation item.
* @param active Flag indicating if the navigation item is active.
* @param current Flag indicating if the navigation item is current page.
* @param linkManager Link manager service.
* @param level Depth level of the navigation item.
* @param childrenSupplier The child navigation items supplier.
* @param parentId ID of the parent navigation component.
* @param component The parent navigation {@link Component}.
*/
public NavigationItemImpl(@NotNull final Page page,
final boolean active,
final boolean current,
@NotNull final LinkManager linkManager,
final int level,
@NotNull final Supplier<List<NavigationItem>> childrenSupplier,
final String parentId,
final Component component) {
super(linkManager, page, parentId, component);
this.active = active;
this.current = current;
this.level = level;
this.children = children;
this.childrenSupplier = childrenSupplier;
}

@Override
Expand All @@ -62,6 +105,9 @@ public boolean isCurrent() {

@Override
public List<NavigationItem> getChildren() {
if (this.children == null) {
this.children = Collections.unmodifiableList(this.childrenSupplier.get());
}
return children;
}

Expand Down
Expand Up @@ -17,6 +17,7 @@

import java.util.Collections;
import java.util.List;
import java.util.function.Supplier;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand All @@ -28,20 +29,63 @@
import com.day.cq.wcm.api.components.Component;
import com.fasterxml.jackson.annotation.JsonIgnore;

/**
* V2 Navigation Item Implementation.
*/
public class NavigationItemImpl extends PageListItemImpl implements NavigationItem {

protected List<NavigationItem> children = Collections.emptyList();
protected int level;
protected boolean active;
private boolean current;
/**
* List of children.
* Note - this will be null until populated from {@link #childrenSupplier} and so should remain private.
*/
private List<NavigationItem> children;

public NavigationItemImpl(Page page, boolean active, boolean current, @NotNull LinkManager linkManager, int level, List<NavigationItem> children,
String parentId, Component component) {
/**
* Navigation level.
*/
private final int level;

/**
* Flag indicating if this navigation item is active.
*/
private final boolean active;

/**
* Flag indicating if this navigation item is for the current page.
*/
private final boolean current;

/**
* Supplier for child navigation items.
*/
@NotNull
private final Supplier<@NotNull List<NavigationItem>> childrenSupplier;

/**
* Construct a Navigation Item.
*
* @param page The page for which to create a navigation item.
* @param active Flag indicating if the navigation item is active.
* @param current Flag indicating if the navigation item is current page.
* @param linkManager Link manager service.
* @param level Depth level of the navigation item.
* @param childrenSupplier The child navigation items supplier.
* @param parentId ID of the parent navigation component.
* @param component The parent navigation {@link Component}.
*/
public NavigationItemImpl(@NotNull final Page page,
final boolean active,
final boolean current,
@NotNull final LinkManager linkManager,
final int level,
@NotNull final Supplier<@NotNull List<@NotNull NavigationItem>> childrenSupplier,
final String parentId,
final Component component) {
super(linkManager, page, parentId, component);
this.active = active;
this.current = current;
this.level = level;
this.children = children;
this.childrenSupplier = childrenSupplier;
}

@Override
Expand All @@ -63,6 +107,9 @@ public boolean isCurrent() {

@Override
public List<NavigationItem> getChildren() {
if (this.children == null) {
this.children = Collections.unmodifiableList(this.childrenSupplier.get());
}
return children;
}

Expand Down
Expand Up @@ -44,7 +44,7 @@ protected void test() {
when(linkBuilder.build()).thenReturn(link);
when(linkManager.get(page)).thenReturn(linkBuilder);
Component component = mock(Component.class);
NavigationItemImpl navigationItem = new NavigationItemImpl(page, true, true, linkManager, 0, Collections.emptyList(), "id", component);
NavigationItemImpl navigationItem = new NavigationItemImpl(page, true, true, linkManager, 0, Collections::emptyList, "id", component);
assertEquals(page, navigationItem.getPage());
assertTrue(navigationItem.isActive());
assertEquals(Collections.emptyList(), navigationItem.getChildren());
Expand Down

0 comments on commit de66725

Please sign in to comment.