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