Use Joiner and TO_NICE_NAME to concatenate the Sone names.
[Sone.git] / src / main / java / net / pterodactylus / sone / template / CollectionAccessor.java
1 /*
2  * Sone - CollectionAccessor.java - Copyright © 2010–2013 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 static com.google.common.base.Joiner.on;
21 import static com.google.common.collect.FluentIterable.from;
22 import static net.pterodactylus.sone.data.Sone.TO_NICE_NAME;
23
24 import java.util.ArrayList;
25 import java.util.Collection;
26 import java.util.Collections;
27 import java.util.List;
28
29 import net.pterodactylus.sone.data.Sone;
30 import net.pterodactylus.util.template.Accessor;
31 import net.pterodactylus.util.template.ReflectionAccessor;
32 import net.pterodactylus.util.template.TemplateContext;
33
34 import com.google.common.base.Joiner;
35
36 /**
37  * {@link Accessor} for {@link Collection}s that adds a couple of specialized
38  * properties that only work for collections that contain the right types.
39  * <dl>
40  * <dd>soneNames</dd>
41  * <dt>Returns the nice names of all {@link Sone}s in the collection, sorted
42  * ascending by their nice names.</dt>
43  * </dl>
44  *
45  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
46  */
47 public class CollectionAccessor extends ReflectionAccessor {
48
49         @Override
50         public Object get(TemplateContext templateContext, Object object, String member) {
51                 if (object == null) {
52                         return null;
53                 }
54                 Collection<?> collection = (Collection<?>) object;
55                 if (member.equals("soneNames")) {
56                         List<Sone> sones = new ArrayList<Sone>();
57                         for (Object sone : collection) {
58                                 if (!(sone instanceof Sone)) {
59                                         continue;
60                                 }
61                                 sones.add((Sone) sone);
62                         }
63                         Collections.sort(sones, Sone.NICE_NAME_COMPARATOR);
64                         return on(", ").join(from(sones).transform(TO_NICE_NAME));
65                 }
66                 return super.get(templateContext, object, member);
67         }
68
69 }