1 package eu.svjatoslav.commons.network.navigation;
3 import java.util.ArrayList;
4 import java.util.HashMap;
8 import eu.svjatoslav.commons.network.Locale;
9 import eu.svjatoslav.commons.string.WildCardMatcher;
11 public class NavigationItem {
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;
20 public NavigationItem() {
24 public NavigationItem(final Locale... localeOrder) {
25 this.localeOrder = localeOrder;
26 matchingWildcard = null;
30 public NavigationItem(final NavigationItem parent, final String linkUrl,
31 final String... titles) {
33 this.linkUrl = linkUrl;
34 matchingWildcard = linkUrl;
36 initializeLocalizedTitles(titles);
39 public NavigationItem addChild(final String linkUrl, final String... titles) {
41 final NavigationItem item = new NavigationItem(this, linkUrl, titles);
42 subElements.add(item);
47 public String getLinkUrl() {
51 public Locale[] getLocaleOrder() {
52 if (localeOrder != null)
55 return parent.getLocaleOrder();
58 public List<NavigationItem> getSubElements() {
62 public String getTitle() {
63 if (localeToTitle.size() != 1)
64 throw new RuntimeException("there shall be exactly one title");
66 return localeToTitle.values().iterator().next();
69 public String getTitle(final Locale locale) {
70 return localeToTitle.get(locale);
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).");
79 for (int i = 0; i < titles.length; i++)
80 localeToTitle.put(locales[i], titles[i]);
83 public boolean matchesUrl(final String url) {
84 return WildCardMatcher.match(url, matchingWildcard);