Add Freenet plugin and WoT subprojects
[fwot.git] / wot / src / main / java / net / pterodactylus / 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.freenet.wot;
19
20 import java.util.Collection;
21 import java.util.Collections;
22 import java.util.HashMap;
23 import java.util.Map;
24 import java.util.Set;
25
26 import net.pterodactylus.freenet.plugin.PluginException;
27
28 import com.google.common.base.Optional;
29 import com.google.inject.Inject;
30
31 /**
32  * Loads {@link OwnIdentity}s and the {@link Identity}s they trust.
33  *
34  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
35  */
36 public class IdentityLoader {
37
38         private final WebOfTrustConnector webOfTrustConnector;
39         private final Optional<Context> context;
40
41         public IdentityLoader(WebOfTrustConnector webOfTrustConnector) {
42                 this(webOfTrustConnector, Optional.<Context>absent());
43         }
44
45         @Inject
46         public IdentityLoader(WebOfTrustConnector webOfTrustConnector, Optional<Context> context) {
47                 this.webOfTrustConnector = webOfTrustConnector;
48                 this.context = context;
49         }
50
51         public Map<OwnIdentity, Collection<Identity>> loadIdentities() throws PluginException {
52                 Collection<OwnIdentity> currentOwnIdentities = webOfTrustConnector.loadAllOwnIdentities();
53                 return loadTrustedIdentitiesForOwnIdentities(currentOwnIdentities);
54         }
55
56         private Map<OwnIdentity, Collection<Identity>> loadTrustedIdentitiesForOwnIdentities(
57                         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                         Set<Identity> trustedIdentities = loadTrustedIdentities(ownIdentity);
67                         currentIdentities.put(ownIdentity, trustedIdentities);
68                 }
69
70                 return currentIdentities;
71         }
72
73         private boolean identityDoesNotHaveTheCorrectContext(OwnIdentity ownIdentity) {
74                 return context.isPresent() && !ownIdentity.hasContext(context.transform(Context::getContext).get());
75         }
76
77         private Set<Identity> loadTrustedIdentities(OwnIdentity ownIdentity) throws PluginException {
78                 return webOfTrustConnector.loadTrustedIdentities(ownIdentity, context.transform(Context::getContext));
79         }
80
81 }