Add Sone accessor.
[Sone.git] / src / main / java / net / pterodactylus / sone / template / SoneAccessor.java
1 /*
2  * Sone - SoneAccessor.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 net.pterodactylus.sone.data.Profile;
21 import net.pterodactylus.sone.data.Sone;
22 import net.pterodactylus.util.template.Accessor;
23 import net.pterodactylus.util.template.DataProvider;
24 import net.pterodactylus.util.template.ReflectionAccessor;
25
26 /**
27  * {@link Accessor} for {@link Sone}s that adds a couple of properties to Sones.
28  * <dl>
29  * <dt>niceName</dt>
30  * <dd>Will show a combination of first name, middle name, and last name, if
31  * available, otherwise the username of the Sone is returned.</dd>
32  * </dl>
33  *
34  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
35  */
36 public class SoneAccessor extends ReflectionAccessor {
37
38         /**
39          * {@inheritDoc}
40          */
41         @Override
42         public Object get(DataProvider dataProvider, Object object, String member) {
43                 Sone sone = (Sone) object;
44                 if (member.equals("niceName")) {
45                         Profile profile = sone.getProfile();
46                         String firstName = profile.getFirstName();
47                         String middleName = profile.getMiddleName();
48                         String lastName = profile.getLastName();
49                         if (firstName == null) {
50                                 if (lastName == null) {
51                                         return sone.getName();
52                                 }
53                                 return lastName;
54                         }
55                         return firstName + ((middleName != null) ? " " + middleName : "") + ((lastName != null) ? " " + lastName : "");
56                 }
57                 return super.get(dataProvider, object, member);
58         }
59
60 }