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