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