switched to HTTPS
[svjatoslav_commons.git] / src / main / java / eu / svjatoslav / commons / network / navigation / NavigationItem.java
1 /*
2  * Svjatoslav Commons - shared library of common functionality. Author: Svjatoslav Agejenko.
3  * This project is released under Creative Commons Zero (CC0) license.
4  */
5 package eu.svjatoslav.commons.network.navigation;
6
7 import eu.svjatoslav.commons.string.GlobMatcher;
8
9 import java.util.ArrayList;
10 import java.util.List;
11
12 public class NavigationItem {
13
14     private final ArrayList<NavigationItem> children = new ArrayList<>();
15     private final String url;
16     private String pattern;
17     private String title;
18     private boolean isDefault;
19
20     public NavigationItem(final String url, String pattern, String title ) {
21         this.url = url;
22         this.pattern = pattern;
23         this.title = title;
24     }
25
26     public NavigationItem setDefault(){
27         isDefault = true;
28         return this;
29     }
30
31     public boolean isDefault(){
32         return isDefault;
33     }
34
35     public void add(final NavigationItem navigationItem) {
36         children.add(navigationItem);
37     }
38
39     public String getUrl() {
40         return url;
41     }
42
43     NavigationItem getMatch(final String requestPath) {
44         if (matchesUrl(requestPath))
45             return this;
46
47         for (final NavigationItem childNavigationItem : children) {
48             final NavigationItem match = childNavigationItem.getMatch(requestPath);
49
50             if (match != null)
51                 return match;
52         }
53         return null;
54     }
55
56     public List<NavigationItem> getChildren() {
57         return children;
58     }
59
60     public String getTitle() {
61         return title;
62     }
63
64     public boolean matchesUrl(final String url) {
65         return GlobMatcher.match(url, pattern);
66     }
67
68     public NavigationItem getDefaultNavigationItem() {
69         for (NavigationItem child : children)
70             if (child.isDefault())
71                 return child;
72         return null;
73     }
74 }