Replace unnecessary type parameters with <>
[Sone.git] / src / main / java / net / pterodactylus / sone / template / AlbumAccessor.java
1 /*
2  * Sone - AlbumAccessor.java - Copyright © 2011–2016 David Roden
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 package net.pterodactylus.sone.template;
19
20 import java.util.ArrayList;
21 import java.util.List;
22 import java.util.Random;
23
24 import net.pterodactylus.sone.data.Album;
25 import net.pterodactylus.sone.data.Image;
26 import net.pterodactylus.util.template.Accessor;
27 import net.pterodactylus.util.template.ReflectionAccessor;
28 import net.pterodactylus.util.template.TemplateContext;
29
30 /**
31  * {@link Accessor} implementation for {@link Album}s. A property named
32  * “backlinks” is added, it returns links to all parents and the owner Sone of
33  * an album.
34  */
35 public class AlbumAccessor extends ReflectionAccessor {
36
37         private final Random random = new Random();
38
39         /**
40          * {@inheritDoc}
41          */
42         @Override
43         public Object get(TemplateContext templateContext, Object object, String member) {
44                 Album album = (Album) object;
45                 if ("backlinks".equals(member)) {
46                         List<Link> backlinks = new ArrayList<>();
47                         Album currentAlbum = album;
48                         while (!currentAlbum.isRoot()) {
49                                 backlinks.add(0, new Link("imageBrowser.html?album=" + currentAlbum.getId(), currentAlbum.getTitle()));
50                                 currentAlbum = currentAlbum.getParent();
51                         }
52                         backlinks.add(0, new Link("imageBrowser.html?sone=" + album.getSone().getId(), SoneAccessor.getNiceName(album.getSone())));
53                         return backlinks;
54                 } else if ("albumImage".equals(member)) {
55                         List<Image> images = album.getImages();
56                         return images.isEmpty() ? null : images.get(random.nextInt(images.size()));
57                 }
58                 return super.get(templateContext, object, member);
59         }
60
61         /**
62          * Container for links.
63          */
64         private static class Link {
65
66                 /** The target of the link. */
67                 private final String target;
68
69                 /** The name of the link. */
70                 private final String name;
71
72                 /**
73                  * Creates a new link.
74                  *
75                  * @param target
76                  *              The target of the link
77                  * @param name
78                  *              The name of the link
79                  */
80                 private Link(String target, String name) {
81                         this.target = target;
82                         this.name = name;
83                 }
84
85                 //
86                 // ACCESSORS
87                 //
88
89                 /**
90                  * Returns the target of the link.
91                  *
92                  * @return The target of the link
93                  */
94                 public String getTarget() {
95                         return target;
96                 }
97
98                 /**
99                  * Returns the name of the link.
100                  *
101                  * @return The name of the link
102                  */
103                 public String getName() {
104                         return name;
105                 }
106
107         }
108
109 }