2 * Sone - IdentityAccessor.java - Copyright © 2010–2016 David Roden
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.
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.
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/>.
18 package net.pterodactylus.sone.template;
22 import javax.inject.Inject;
23 import javax.inject.Singleton;
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;
33 * {@link Accessor} implementation that adds a “uniqueNickname” member to an
36 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
39 public class IdentityAccessor extends ReflectionAccessor {
42 private final Core core;
45 * Creates a new identity accessor.
51 public IdentityAccessor(Core core) {
59 public Object get(TemplateContext templateContext, Object object, String member) {
60 Identity identity = (Identity) object;
61 if ("uniqueNickname".equals(member)) {
64 Set<OwnIdentity> ownIdentities = core.getIdentityManager().getAllOwnIdentities();
66 boolean unique = true;
67 String abbreviatedWantedNickname = getAbbreviatedNickname(identity, ++minLength);
68 for (Identity ownIdentity : ownIdentities) {
69 if (ownIdentity.equals(identity)) {
72 String abbreviatedNickname = getAbbreviatedNickname(ownIdentity, minLength);
73 if (abbreviatedNickname.equals(abbreviatedWantedNickname)) {
79 } while (!found && (minLength < 43));
80 return getAbbreviatedNickname(identity, minLength);
82 return super.get(templateContext, object, member);
90 * Returns the nickname of the given identity, optionally appending the
91 * first characters of the ID to it.
96 * The number of characters from the beginning of the ID to
97 * append to the nickname
98 * @return The nickname with optional ID appendage
100 private static String getAbbreviatedNickname(Identity identity, int length) {
101 return identity.getNickname() + ((length > 0) ? "@" + identity.getId().substring(0, length) : "");