de.otto.edison.navigation.NavBar Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of edison-core Show documentation
Show all versions of edison-core Show documentation
Core library for all Edison libraries.
package de.otto.edison.navigation;
import net.jcip.annotations.ThreadSafe;
import java.util.ArrayList;
import java.util.List;
import static java.util.Collections.emptyList;
import static java.util.Collections.unmodifiableList;
import static java.util.Comparator.comparing;
/**
* The navigation of Edison-Microservices on /internal pages.
*
* The NavBar can be extended by more items. In order to do this, just {@link #register}
*
* @since 1.0.0
*/
@ThreadSafe
public class NavBar {
private volatile List items;
private NavBar(final List items) {
updateAndSortItems(new ArrayList<>(items));
}
public static NavBar emptyNavBar() {
return new NavBar(emptyList());
}
public static NavBar navBar(final List items) {
return new NavBar(items);
}
/**
* Registers another item in this NavBar.
*
* The item is positioned in the navigation by {@link NavBarItem#getPosition()},
* then by {@link NavBarItem#getTitle()}.
*
* @param item New NavBarItem.
*/
public void register(final NavBarItem item) {
updateAndSortItems(new ArrayList(items) {{
add(item);
}});
}
/**
* Returns the list of all {@link NavBarItem items}.
*
* The list is sorted by {@link NavBarItem#getPosition()}, then by {@link NavBarItem#getTitle()}.
*
* @return Unmodifiable list of navbar items.
*/
public List getItems() {
return unmodifiableList(items);
}
private void updateAndSortItems(final List items) {
items.sort(
comparing(NavBarItem::getPosition)
.thenComparing(NavBarItem::getTitle));
this.items = unmodifiableList(items);
}
}