From: David ‘Bombe’ Roden Date: Sat, 30 Oct 2010 00:20:37 +0000 (+0200) Subject: Add accessor for Identitys. X-Git-Tag: 0.2-RC1~82 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=5d1ad36c04f83468efd85f6f03983306b2d98d6d;p=Sone.git Add accessor for Identitys. --- diff --git a/src/main/java/net/pterodactylus/sone/template/IdentityAccessor.java b/src/main/java/net/pterodactylus/sone/template/IdentityAccessor.java new file mode 100644 index 0000000..4ea956d --- /dev/null +++ b/src/main/java/net/pterodactylus/sone/template/IdentityAccessor.java @@ -0,0 +1,106 @@ +/* + * Sone - IdentityAccessor.java - Copyright © 2010 David Roden + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.pterodactylus.sone.template; + +import java.util.Collections; +import java.util.Set; + +import net.pterodactylus.sone.core.Core; +import net.pterodactylus.sone.freenet.wot.Identity; +import net.pterodactylus.sone.freenet.wot.OwnIdentity; +import net.pterodactylus.sone.freenet.wot.PluginException; +import net.pterodactylus.util.template.Accessor; +import net.pterodactylus.util.template.DataProvider; +import net.pterodactylus.util.template.ReflectionAccessor; + +/** + * {@link Accessor} implementation that adds a “uniqueNickname” member to an + * {@link Identity}. + * + * @author David ‘Bombe’ Roden + */ +public class IdentityAccessor extends ReflectionAccessor { + + /** The core. */ + private final Core core; + + /** + * Creates a new identity accessor. + * + * @param core + * The core + */ + public IdentityAccessor(Core core) { + this.core = core; + } + + /** + * {@inheritDoc} + */ + @Override + public Object get(DataProvider dataProvider, Object object, String member) { + Identity identity = (Identity) object; + if ("uniqueNickname".equals(member)) { + int minLength = -1; + boolean found = false; + Set ownIdentities = null; + try { + ownIdentities = core.getWebOfTrustConnector().loadAllOwnIdentities(); + } catch (PluginException e) { + ownIdentities = Collections.emptySet(); + } + do { + boolean unique = true; + String abbreviatedWantedNickname = getAbbreviatedNickname(identity, ++minLength); + for (Identity ownIdentity : ownIdentities) { + if (ownIdentity.equals(identity)) { + continue; + } + String abbreviatedNickname = getAbbreviatedNickname(ownIdentity, minLength); + if (abbreviatedNickname.equals(abbreviatedWantedNickname)) { + unique = false; + break; + } + } + found = unique; + } while (!found && (minLength < 43)); + return getAbbreviatedNickname(identity, minLength); + } + return super.get(dataProvider, object, member); + } + + // + // PRIVATE METHODS + // + + /** + * Returns the nickname of the given identity, optionally appending the + * first characters of the ID to it. + * + * @param identity + * The identity + * @param length + * The number of characters from the beginning of the ID to + * append to the nickname + * @return The nickname with optional ID appendage + */ + private String getAbbreviatedNickname(Identity identity, int length) { + return identity.getNickname() + ((length > 0) ? "@" + identity.getId().substring(0, length) : ""); + } + +}