+/*
+ * Svjatoslav Commons - shared library of common functionality.
+ * Copyright ©2012-2017, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of version 3 of the GNU Lesser General Public License
+ * or later as published by the Free Software Foundation.
+ */
+
package eu.svjatoslav.commons.network.navigation;
+import eu.svjatoslav.commons.string.WildCardMatcher;
+
import java.util.ArrayList;
import java.util.List;
-import eu.svjatoslav.commons.string.WildCardMatcher;
-
public class NavigationItem {
- private final String title;
- private final String link;
- private final String matchingWildcard;
+ private final ArrayList<NavigationItem> children = new ArrayList<>();
+ private final String url;
+ private String pattern;
+ private String title;
+ private boolean isDefault;
+
+ public NavigationItem(final String url, String pattern, String title ) {
+ this.url = url;
+ this.pattern = pattern;
+ this.title = title;
+ }
- ArrayList<NavigationItem> subElements = new ArrayList<NavigationItem>();
+ public NavigationItem setDefault(){
+ isDefault = true;
+ return this;
+ }
- public NavigationItem(final String title, final String link) {
- this.title = title;
- this.link = link;
- matchingWildcard = link;
- }
+ public boolean isDefault(){
+ return isDefault;
+ }
- public NavigationItem(final String title, final String link,
- final String wildcard) {
- this.title = title;
- this.link = link;
- matchingWildcard = wildcard;
- }
+ public void add(final NavigationItem navigationItem) {
+ children.add(navigationItem);
+ }
- public void addSubNavigation(final NavigationItem item) {
- subElements.add(item);
- }
+ public String getUrl() {
+ return url;
+ }
- public String getLink() {
- return link;
- }
+ NavigationItem getMatch(final String requestPath) {
+ if (matchesUrl(requestPath))
+ return this;
- public List<NavigationItem> getSubElements() {
- return subElements;
- }
+ for (final NavigationItem childNavigationItem : children) {
+ final NavigationItem match = childNavigationItem.getMatch(requestPath);
- public String getTitle() {
- return title;
- }
+ if (match != null)
+ return match;
+ }
+ return null;
+ }
- public boolean matchesUrl(final String url) {
+ public List<NavigationItem> getChildren() {
+ return children;
+ }
- return WildCardMatcher.match(url, matchingWildcard);
+ public String getTitle() {
+ return title;
+ }
- }
+ public boolean matchesUrl(final String url) {
+ return WildCardMatcher.match(url, pattern);
+ }
+ public NavigationItem getDefaultNavigationItem() {
+ for (NavigationItem child : children)
+ if (child.isDefault())
+ return child;
+ return null;
+ }
}