Add an identity loader.
[Sone.git] / src / main / java / net / pterodactylus / sone / freenet / wot / IdentityLoader.java
1 /*
2  * Sone - IdentityLoader.java - Copyright © 2013 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 static com.google.common.collect.HashMultimap.create;
21
22 import java.util.Collection;
23 import java.util.Set;
24
25 import net.pterodactylus.sone.freenet.plugin.PluginException;
26
27 import com.google.common.base.Optional;
28 import com.google.common.collect.Multimap;
29
30 /**
31  * Loads {@link OwnIdentity}s and the {@link Identity}s they trust.
32  *
33  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
34  */
35 public class IdentityLoader {
36
37         private final WebOfTrustConnector webOfTrustConnector;
38         private final Optional<String> context;
39
40         public IdentityLoader(WebOfTrustConnector webOfTrustConnector) {
41                 this(webOfTrustConnector, Optional.<String>absent());
42         }
43
44         public IdentityLoader(WebOfTrustConnector webOfTrustConnector, Optional<String> context) {
45                 this.webOfTrustConnector = webOfTrustConnector;
46                 this.context = context;
47         }
48
49         public Multimap<OwnIdentity, Identity> loadIdentities() throws WebOfTrustException {
50                 Collection<OwnIdentity> currentOwnIdentities = webOfTrustConnector.loadAllOwnIdentities();
51                 return loadTrustedIdentitiesForOwnIdentities(currentOwnIdentities);
52         }
53
54         private Multimap<OwnIdentity, Identity> loadTrustedIdentitiesForOwnIdentities(Collection<OwnIdentity> ownIdentities) throws PluginException {
55                 Multimap<OwnIdentity, Identity> currentIdentities = create();
56
57                 for (OwnIdentity ownIdentity : ownIdentities) {
58                         if (identityDoesNotHaveTheCorrectContext(ownIdentity)) {
59                                 continue;
60                         }
61
62                         Set<Identity> trustedIdentities = webOfTrustConnector.loadTrustedIdentities(ownIdentity, context.orNull());
63                         currentIdentities.putAll(ownIdentity, trustedIdentities);
64                 }
65
66                 return currentIdentities;
67         }
68
69         private boolean identityDoesNotHaveTheCorrectContext(OwnIdentity ownIdentity) {
70                 return context.isPresent() && !ownIdentity.hasContext(context.get());
71         }
72
73 }