default constructor is nice still
[svjatoslav_commons.git] / src / main / java / eu / svjatoslav / commons / network / navigation / NavigationItem.java
1 package eu.svjatoslav.commons.network.navigation;
2
3 import java.util.ArrayList;
4 import java.util.HashMap;
5 import java.util.List;
6 import java.util.Map;
7
8 import eu.svjatoslav.commons.network.Locale;
9 import eu.svjatoslav.commons.string.WildCardMatcher;
10
11 public class NavigationItem {
12
13         private final String matchingWildcard;
14         private final Map<Locale, String> localeToTitle = new HashMap<Locale, String>();
15         ArrayList<NavigationItem> subElements = new ArrayList<NavigationItem>();
16         private Locale[] localeOrder;
17         private NavigationItem parent;
18         private final String linkUrl;
19
20         public NavigationItem() {
21                 this(Locale.ENG);
22         }
23
24         public NavigationItem(final Locale... localeOrder) {
25                 this.localeOrder = localeOrder;
26                 matchingWildcard = null;
27                 linkUrl = null;
28         }
29
30         public NavigationItem(final NavigationItem parent, final String linkUrl,
31                         final String... titles) {
32                 this.parent = parent;
33                 this.linkUrl = linkUrl;
34                 matchingWildcard = linkUrl;
35
36                 initializeLocalizedTitles(titles);
37         }
38
39         public NavigationItem addChild(final String linkUrl, final String... titles) {
40
41                 final NavigationItem item = new NavigationItem(this, linkUrl, titles);
42                 subElements.add(item);
43
44                 return item;
45         }
46
47         public String getLinkUrl() {
48                 return linkUrl;
49         }
50
51         public Locale[] getLocaleOrder() {
52                 if (localeOrder != null)
53                         return localeOrder;
54
55                 return parent.getLocaleOrder();
56         }
57
58         public List<NavigationItem> getSubElements() {
59                 return subElements;
60         }
61
62         public String getTitle() {
63                 if (localeToTitle.size() != 1)
64                         throw new RuntimeException("there shall be exactly one title");
65
66                 return localeToTitle.values().iterator().next();
67         }
68
69         public String getTitle(final Locale locale) {
70                 return localeToTitle.get(locale);
71         }
72
73         private void initializeLocalizedTitles(final String... titles) {
74                 final Locale[] locales = getLocaleOrder();
75                 if (locales.length != titles.length)
76                         throw new RuntimeException("There should be exactly "
77                                         + locales.length + " title(s).");
78
79                 for (int i = 0; i < titles.length; i++)
80                         localeToTitle.put(locales[i], titles[i]);
81         }
82
83         public boolean matchesUrl(final String url) {
84                 return WildCardMatcher.match(url, matchingWildcard);
85
86         }
87
88 }