slightly more type-safe and extending friendly navigation
authorSvjatoslav Agejenko <svjatoslav@svjatoslav.eu>
Sat, 18 Oct 2014 15:18:01 +0000 (18:18 +0300)
committerSvjatoslav Agejenko <svjatoslav@svjatoslav.eu>
Sat, 18 Oct 2014 15:18:01 +0000 (18:18 +0300)
src/main/java/eu/svjatoslav/commons/network/navigation/Navigation.java
src/main/java/eu/svjatoslav/commons/network/navigation/NavigationItem.java

index 1bdf378..6ef5e68 100755 (executable)
@@ -11,17 +11,18 @@ package eu.svjatoslav.commons.network.navigation;
 
 import java.net.MalformedURLException;
 import java.net.URL;
+import java.util.List;
 
 import javax.servlet.http.HttpServletRequest;
 
 import eu.svjatoslav.commons.network.Locale;
 import eu.svjatoslav.commons.network.LocaleConfiguration;
 
-public class Navigation {
+public class Navigation<NI extends NavigationItem> {
 
-       private NavigationItem rootNavigationItem;
+       private NI rootNavigationItem;
 
-       private NavigationItem defaultNavigationItem;
+       private NI defaultNavigationItem;
 
        protected final LocaleConfiguration localeConfiguration;
 
@@ -29,7 +30,7 @@ public class Navigation {
                this.localeConfiguration = localeConfiguration;
        }
 
-       public NavigationItem getDefaultNavigationItem() {
+       public NI getDefaultNavigationItem() {
                return defaultNavigationItem;
        }
 
@@ -44,17 +45,17 @@ public class Navigation {
                return localeConfiguration;
        }
 
-       public NavigationItem getRootNavigationItem() {
+       public NI getRootNavigationItem() {
                return rootNavigationItem;
        }
 
-       public NavigationItem getSelectedItem(final HttpServletRequest request) {
+       public NI getSelectedItem(final HttpServletRequest request) {
                final String requestUrl = request.getRequestURL().toString();
 
                try {
                        final String requestPath = new URL(requestUrl).getPath();
 
-                       final NavigationItem match = rootNavigationItem
+                       final NI match = (NI) rootNavigationItem
                                        .getMatchingNavigationItem(requestPath);
 
                        if (match != null)
@@ -72,14 +73,14 @@ public class Navigation {
                final Locale currentLocale = localeConfiguration
                                .detectCurrentLocale(request);
 
-               final NavigationItem selectedItem = getSelectedItem(request);
+               final NI selectedItem = getSelectedItem(request);
                final StringBuffer result = new StringBuffer();
 
                result.append("<div class=\""
                                + getGetNotNullString(rootNavigationItem.getCssPrefix())
                                + "navigationMenu\">");
 
-               for (final NavigationItem item : getRootNavigationItem()
+               for (final NI item : (List<NI>) getRootNavigationItem()
                                .getSubElements()) {
 
                        if (item == selectedItem)
@@ -101,14 +102,12 @@ public class Navigation {
                return result.toString();
        }
 
-       public NavigationItem setDefaultNavigationItem(
-                       final NavigationItem defaultNavigationItem) {
+       public NI setDefaultNavigationItem(final NI defaultNavigationItem) {
                this.defaultNavigationItem = defaultNavigationItem;
                return defaultNavigationItem;
        }
 
-       public NavigationItem setRootNavigationItem(
-                       final NavigationItem rootNavigationItem) {
+       public NI setRootNavigationItem(final NI rootNavigationItem) {
                this.rootNavigationItem = rootNavigationItem;
                return rootNavigationItem;
        }
index 2a27c5d..76a7d07 100755 (executable)
@@ -17,43 +17,41 @@ import java.util.Map;
 import eu.svjatoslav.commons.network.Locale;
 import eu.svjatoslav.commons.string.WildCardMatcher;
 
-public class NavigationItem {
+public class NavigationItem<NI extends NavigationItem, N extends Navigation> {
 
        private String matchingPattern;
+
        private final Map<Locale, String> localeToTitle = new HashMap<Locale, String>();
-       final ArrayList<NavigationItem> subElements = new ArrayList<NavigationItem>();
-       final private NavigationItem parent;
-       final private Navigation navigation;
+       private final ArrayList<NI> subElements = new ArrayList<NI>();
+       private final NI parent;
+       private final N navigation;
        private final String linkUrl;
-
        /**
         * CSS prefix is inherited to child menu items.
         */
        private String cssPrefix;
 
-       public NavigationItem(final Navigation navigation) {
+       public NavigationItem(final N navigation) {
                this.navigation = navigation;
                parent = null;
                matchingPattern = null;
                linkUrl = null;
        }
 
-       private NavigationItem(final Navigation navigation,
-                       final NavigationItem parent, final String linkUrl,
+       public NavigationItem(final NI parent, final String linkUrl,
                        final String... titles) {
-               this.navigation = navigation;
+               this.navigation = (N) parent.getNavigation();
                this.parent = parent;
                this.linkUrl = linkUrl;
                matchingPattern = linkUrl;
 
                initializeLocalizedTitles(titles);
+
+               parent.addNavigationItem(this);
        }
 
-       public NavigationItem addChild(final String linkUrl, final String... titles) {
-               final NavigationItem item = new NavigationItem(navigation, this,
-                               linkUrl, titles);
-               subElements.add(item);
-               return item;
+       public void addNavigationItem(final NI navigationItem) {
+               subElements.add(navigationItem);
        }
 
        protected String getCssPrefix() {
@@ -70,12 +68,12 @@ public class NavigationItem {
                return linkUrl;
        }
 
-       NavigationItem getMatchingNavigationItem(final String requestPath) {
+       NI getMatchingNavigationItem(final String requestPath) {
                if (matchesUrl(requestPath))
-                       return this;
+                       return (NI) this;
 
-               for (final NavigationItem childNavigationItem : subElements) {
-                       final NavigationItem match = childNavigationItem
+               for (final NI childNavigationItem : subElements) {
+                       final NI match = (NI) childNavigationItem
                                        .getMatchingNavigationItem(requestPath);
 
                        if (match != null)
@@ -84,7 +82,11 @@ public class NavigationItem {
                return null;
        }
 
-       public List<NavigationItem> getSubElements() {
+       public N getNavigation() {
+               return navigation;
+       }
+
+       public List<NI> getSubElements() {
                return subElements;
        }
 
@@ -93,7 +95,7 @@ public class NavigationItem {
        }
 
        private void initializeLocalizedTitles(final String... titles) {
-               final List<Locale> locales = navigation.getLocaleConfiguration()
+               final List<Locale> locales = getNavigation().getLocaleConfiguration()
                                .getAllowedLocales();
                if (locales.size() != titles.length)
                        throw new RuntimeException("There should be exactly "
@@ -116,4 +118,12 @@ public class NavigationItem {
                return this;
        }
 
+       @Override
+       public String toString() {
+               return "NavigationItem [matchingPattern=" + matchingPattern
+                               + ", localeToTitle=" + localeToTitle + ", subElements count="
+                               + subElements.size() + ", linkUrl=" + linkUrl + ", cssPrefix="
+                               + cssPrefix + "]";
+       }
+
 }