Extract nice name formatting into its own method.
[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  * <dt>isFriend</dt>
33  * <dd>Will return {@code true} if the sone in question is a friend of the
34  * currently logged in Sone (as determined by accessing the “currentSone”
35  * variable of the given {@link DataProvider}).</dd>
36  * <dt>isCurrent</dt>
37  * <dd>Will return {@code true} if the sone in question is the currently logged
38  * in Sone.</dd>
39  * </dl>
40  *
41  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
42  */
43 public class SoneAccessor extends ReflectionAccessor {
44
45         /**
46          * {@inheritDoc}
47          */
48         @Override
49         public Object get(DataProvider dataProvider, Object object, String member) {
50                 Sone sone = (Sone) object;
51                 if (member.equals("niceName")) {
52                         return getNiceName(sone);
53                 } else if (member.equals("isFriend")) {
54                         Sone currentSone = (Sone) dataProvider.getData("currentSone");
55                         return currentSone.hasFriendSone(sone) ? true : null;
56                 } else if (member.equals("isCurrent")) {
57                         Sone currentSone = (Sone) dataProvider.getData("currentSone");
58                         return currentSone.equals(sone);
59                 }
60                 return super.get(dataProvider, object, member);
61         }
62
63         //
64         // STATIC METHODS
65         //
66
67         /**
68          * Returns the nice name of the given Sone.
69          *
70          * @param sone
71          *            The Sone to get the nice name for
72          * @return The nice name of the Sone
73          */
74         public static String getNiceName(Sone sone) {
75                 Profile profile = sone.getProfile();
76                 String firstName = profile.getFirstName();
77                 String middleName = profile.getMiddleName();
78                 String lastName = profile.getLastName();
79                 if (firstName == null) {
80                         if (lastName == null) {
81                                 return sone.getName();
82                         }
83                         return lastName;
84                 }
85                 return firstName + ((middleName != null) ? " " + middleName : "") + ((lastName != null) ? " " + lastName : "");
86         }
87
88 }