🔥 Remove unused field
[Sone.git] / src / main / java / net / pterodactylus / sone / template / IdentityAccessor.java
1 /*
2  * Sone - IdentityAccessor.java - Copyright Â© 2010–2019 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.Set;
21
22 import javax.inject.Inject;
23 import javax.inject.Singleton;
24
25 import net.pterodactylus.sone.core.Core;
26 import net.pterodactylus.sone.freenet.wot.Identity;
27 import net.pterodactylus.sone.freenet.wot.OwnIdentity;
28 import net.pterodactylus.util.template.Accessor;
29 import net.pterodactylus.util.template.ReflectionAccessor;
30 import net.pterodactylus.util.template.TemplateContext;
31
32 /**
33  * {@link Accessor} implementation that adds a â€śuniqueNickname” member to an
34  * {@link Identity}.
35  */
36 @Singleton
37 public class IdentityAccessor extends ReflectionAccessor {
38
39         /** The core. */
40         private final Core core;
41
42         /**
43          * Creates a new identity accessor.
44          *
45          * @param core
46          *            The core
47          */
48         @Inject
49         public IdentityAccessor(Core core) {
50                 this.core = core;
51         }
52
53         /**
54          * {@inheritDoc}
55          */
56         @Override
57         public Object get(TemplateContext templateContext, Object object, String member) {
58                 Identity identity = (Identity) object;
59                 if ("uniqueNickname".equals(member)) {
60                         int minLength = -1;
61                         boolean found;
62                         Set<OwnIdentity> ownIdentities = core.getIdentityManager().getAllOwnIdentities();
63                         do {
64                                 boolean unique = true;
65                                 String abbreviatedWantedNickname = getAbbreviatedNickname(identity, ++minLength);
66                                 for (Identity ownIdentity : ownIdentities) {
67                                         if (ownIdentity.equals(identity)) {
68                                                 continue;
69                                         }
70                                         String abbreviatedNickname = getAbbreviatedNickname(ownIdentity, minLength);
71                                         if (abbreviatedNickname.equals(abbreviatedWantedNickname)) {
72                                                 unique = false;
73                                                 break;
74                                         }
75                                 }
76                                 found = unique;
77                         } while (!found && (minLength < 43));
78                         return getAbbreviatedNickname(identity, minLength);
79                 }
80                 return super.get(templateContext, object, member);
81         }
82
83         //
84         // PRIVATE METHODS
85         //
86
87         /**
88          * Returns the nickname of the given identity, optionally appending the
89          * first characters of the ID to it.
90          *
91          * @param identity
92          *            The identity
93          * @param length
94          *            The number of characters from the beginning of the ID to
95          *            append to the nickname
96          * @return The nickname with optional ID appendage
97          */
98         private static String getAbbreviatedNickname(Identity identity, int length) {
99                 return identity.getNickname() + ((length > 0) ? "@" + identity.getId().substring(0, length) : "");
100         }
101
102 }