2 * Sone - IdentityLoader.java - Copyright © 2013–2019 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.freenet.wot;
21 import java.util.logging.*;
22 import javax.annotation.*;
24 import com.google.common.base.*;
25 import com.google.inject.*;
26 import net.pterodactylus.sone.freenet.plugin.*;
28 import static java.util.concurrent.TimeUnit.*;
31 * Loads {@link OwnIdentity}s and the {@link Identity}s they trust.
33 public class IdentityLoader {
35 private final Logger logger = Logger.getLogger(IdentityLoader.class.getName());
36 private final WebOfTrustConnector webOfTrustConnector;
38 private final Context context;
40 public IdentityLoader(WebOfTrustConnector webOfTrustConnector) {
41 this(webOfTrustConnector, null);
45 public IdentityLoader(WebOfTrustConnector webOfTrustConnector, @Nullable Context context) {
46 this.webOfTrustConnector = webOfTrustConnector;
47 this.context = context;
50 public Map<OwnIdentity, Collection<Identity>> loadIdentities() throws WebOfTrustException {
51 Stopwatch stopwatch = Stopwatch.createStarted();
52 Collection<OwnIdentity> currentOwnIdentities = webOfTrustConnector.loadAllOwnIdentities();
53 logger.fine("Loaded " + currentOwnIdentities.size() + " own identities in " + (stopwatch.elapsed(MILLISECONDS) / 1000.0) + "s.");
54 return loadTrustedIdentitiesForOwnIdentities(currentOwnIdentities);
57 private Map<OwnIdentity, Collection<Identity>> loadTrustedIdentitiesForOwnIdentities(Collection<OwnIdentity> ownIdentities) throws PluginException {
58 Map<OwnIdentity, Collection<Identity>> currentIdentities = new HashMap<>();
60 for (OwnIdentity ownIdentity : ownIdentities) {
61 if (identityDoesNotHaveTheCorrectContext(ownIdentity)) {
62 currentIdentities.put(ownIdentity, Collections.<Identity>emptySet());
66 Stopwatch stopwatch = Stopwatch.createStarted();
67 Set<Identity> trustedIdentities = webOfTrustConnector.loadTrustedIdentities(ownIdentity, (context == null) ? null : context.getContext());
68 logger.fine("Loaded " + trustedIdentities.size() + " identities for " + ownIdentity.getNickname() + " in " + (stopwatch.elapsed(MILLISECONDS) / 1000.0) + "s.");
69 currentIdentities.put(ownIdentity, trustedIdentities);
72 return currentIdentities;
75 private boolean identityDoesNotHaveTheCorrectContext(OwnIdentity ownIdentity) {
76 return (context != null) && !ownIdentity.hasContext(context.getContext());