Extract some functionality into their own methods.
[Sone.git] / src / main / java / net / pterodactylus / sone / template / AlbumAccessor.java
index 2c0a00e..0ab114b 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Sone - AlbumAccessor.java - Copyright © 2011 David Roden
+ * Sone - AlbumAccessor.java - Copyright © 2011–2013 David Roden
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
 
 package net.pterodactylus.sone.template;
 
+import static net.pterodactylus.sone.template.SoneAccessor.getNiceName;
+
 import java.util.ArrayList;
-import java.util.HashMap;
 import java.util.List;
-import java.util.Map;
 
 import net.pterodactylus.sone.data.Album;
+import net.pterodactylus.sone.data.Sone;
 import net.pterodactylus.util.template.Accessor;
 import net.pterodactylus.util.template.ReflectionAccessor;
 import net.pterodactylus.util.template.TemplateContext;
@@ -36,43 +37,84 @@ import net.pterodactylus.util.template.TemplateContext;
  */
 public class AlbumAccessor extends ReflectionAccessor {
 
-       /**
-        * {@inheritDoc}
-        */
        @Override
        public Object get(TemplateContext templateContext, Object object, String member) {
                Album album = (Album) object;
                if ("backlinks".equals(member)) {
-                       List<Map<String, String>> backlinks = new ArrayList<Map<String, String>>();
-                       Album currentAlbum = album;
-                       while (currentAlbum != null) {
-                               backlinks.add(0, createLink("imageBrowser.html?album=" + currentAlbum.getId(), currentAlbum.getTitle()));
-                               currentAlbum = currentAlbum.getParent();
-                       }
-                       backlinks.add(0, createLink("imageBrowser.html?sone=" + album.getSone().getId(), SoneAccessor.getNiceName(album.getSone())));
-                       return backlinks;
+                       return generateBacklinks(album);
+               } else if ("albumImage".equals(member)) {
+                       return album.getAlbumImage().orNull();
                }
                return super.get(templateContext, object, member);
        }
 
-       //
-       // PRIVATE METHODS
-       //
+       private Object generateBacklinks(Album album) {
+               List<Link> backlinks = new ArrayList<Link>();
+               Album currentAlbum = album;
+               while (!currentAlbum.isRoot()) {
+                       backlinks.add(0, generateLinkToAlbum(currentAlbum));
+                       currentAlbum = currentAlbum.getParent();
+               }
+               backlinks.add(0, generateLinkToSone(album.getSone()));
+               return backlinks;
+       }
+
+       private Link generateLinkToSone(Sone sone) {
+               return new Link("imageBrowser.html?sone=" + sone.getId(), getNiceName(sone));
+       }
+
+       private Link generateLinkToAlbum(Album currentAlbum) {
+               return new Link("imageBrowser.html?album=" + currentAlbum.getId(), currentAlbum.getTitle());
+       }
 
        /**
-        * Creates a map containing mappings for “target” and “link.”
+        * Container for links.
         *
-        * @param target
-        *            The target to link to
-        * @param name
-        *            The name of the link
-        * @return The created map containing the mappings
+        * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
         */
-       private Map<String, String> createLink(String target, String name) {
-               Map<String, String> link = new HashMap<String, String>();
-               link.put("target", target);
-               link.put("name", name);
-               return link;
+       static class Link {
+
+               /** The target of the link. */
+               private final String target;
+
+               /** The name of the link. */
+               private final String name;
+
+               /**
+                * Creates a new link.
+                *
+                * @param target
+                *              The target of the link
+                * @param name
+                *              The name of the link
+                */
+               private Link(String target, String name) {
+                       this.target = target;
+                       this.name = name;
+               }
+
+               //
+               // ACCESSORS
+               //
+
+               /**
+                * Returns the target of the link.
+                *
+                * @return The target of the link
+                */
+               public String getTarget() {
+                       return target;
+               }
+
+               /**
+                * Returns the name of the link.
+                *
+                * @return The name of the link
+                */
+               public String getName() {
+                       return name;
+               }
+
        }
 
 }