🎨 Replace Identities helper with Kotlin version
[Sone.git] / src / main / java / net / pterodactylus / sone / freenet / wot / IdentityLoader.java
1 /*
2  * Sone - IdentityLoader.java - Copyright Â© 2013–2019 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.freenet.wot;
19
20 import java.util.*;
21 import java.util.logging.*;
22 import javax.annotation.*;
23
24 import com.google.common.base.*;
25 import com.google.inject.*;
26 import net.pterodactylus.sone.freenet.plugin.*;
27
28 import static java.util.concurrent.TimeUnit.*;
29
30 /**
31  * Loads {@link OwnIdentity}s and the {@link Identity}s they trust.
32  */
33 public class IdentityLoader {
34
35         private final Logger logger = Logger.getLogger(IdentityLoader.class.getName());
36         private final WebOfTrustConnector webOfTrustConnector;
37         @Nullable
38         private final Context context;
39
40         public IdentityLoader(WebOfTrustConnector webOfTrustConnector) {
41                 this(webOfTrustConnector, null);
42         }
43
44         @Inject
45         public IdentityLoader(WebOfTrustConnector webOfTrustConnector, @Nullable Context context) {
46                 this.webOfTrustConnector = webOfTrustConnector;
47                 this.context = context;
48         }
49
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);
55         }
56
57         private Map<OwnIdentity, Collection<Identity>> loadTrustedIdentitiesForOwnIdentities(Collection<OwnIdentity> ownIdentities) throws PluginException {
58                 Map<OwnIdentity, Collection<Identity>> currentIdentities = new HashMap<>();
59
60                 for (OwnIdentity ownIdentity : ownIdentities) {
61                         if (identityDoesNotHaveTheCorrectContext(ownIdentity)) {
62                                 currentIdentities.put(ownIdentity, Collections.<Identity>emptySet());
63                                 continue;
64                         }
65
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);
70                 }
71
72                 return currentIdentities;
73         }
74
75         private boolean identityDoesNotHaveTheCorrectContext(OwnIdentity ownIdentity) {
76                 return (context != null) && !ownIdentity.hasContext(context.getContext());
77         }
78
79 }