Add accessor for Identitys.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sat, 30 Oct 2010 00:20:37 +0000 (02:20 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sat, 30 Oct 2010 16:39:42 +0000 (18:39 +0200)
src/main/java/net/pterodactylus/sone/template/IdentityAccessor.java [new file with mode: 0644]

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 (file)
index 0000000..4ea956d
--- /dev/null
@@ -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 <http://www.gnu.org/licenses/>.
+ */
+
+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 <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+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<OwnIdentity> 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) : "");
+       }
+
+}