2 * Sone - IdentityAccessor.java - Copyright © 2010 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;
20 import java.util.Collections;
23 import net.pterodactylus.sone.core.Core;
24 import net.pterodactylus.sone.freenet.wot.Identity;
25 import net.pterodactylus.sone.freenet.wot.OwnIdentity;
26 import net.pterodactylus.sone.freenet.wot.PluginException;
27 import net.pterodactylus.util.template.Accessor;
28 import net.pterodactylus.util.template.DataProvider;
29 import net.pterodactylus.util.template.ReflectionAccessor;
32 * {@link Accessor} implementation that adds a “uniqueNickname” member to an
35 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
37 public class IdentityAccessor extends ReflectionAccessor {
40 private final Core core;
43 * Creates a new identity accessor.
48 public IdentityAccessor(Core core) {
56 public Object get(DataProvider dataProvider, Object object, String member) {
57 Identity identity = (Identity) object;
58 if ("uniqueNickname".equals(member)) {
60 boolean found = false;
61 Set<OwnIdentity> ownIdentities = null;
63 ownIdentities = core.getWebOfTrustConnector().loadAllOwnIdentities();
64 } catch (PluginException e) {
65 ownIdentities = Collections.emptySet();
68 boolean unique = true;
69 String abbreviatedWantedNickname = getAbbreviatedNickname(identity, ++minLength);
70 for (Identity ownIdentity : ownIdentities) {
71 if (ownIdentity.equals(identity)) {
74 String abbreviatedNickname = getAbbreviatedNickname(ownIdentity, minLength);
75 if (abbreviatedNickname.equals(abbreviatedWantedNickname)) {
81 } while (!found && (minLength < 43));
82 return getAbbreviatedNickname(identity, minLength);
84 return super.get(dataProvider, object, member);
92 * Returns the nickname of the given identity, optionally appending the
93 * first characters of the ID to it.
98 * The number of characters from the beginning of the ID to
99 * append to the nickname
100 * @return The nickname with optional ID appendage
102 private String getAbbreviatedNickname(Identity identity, int length) {
103 return identity.getNickname() + ((length > 0) ? "@" + identity.getId().substring(0, length) : "");