2eacd08a867b79349f9286aa2bf4d1ccc778e496
[WoTNS.git] / src / main / java / net / pterodactylus / wotns / main / IdentityTargets.java
1 /*
2  * WoTNS - IdentityTargets.java - Copyright © 2011 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.wotns.main;
19
20 import java.util.Collections;
21 import java.util.HashMap;
22 import java.util.Iterator;
23 import java.util.Map;
24 import java.util.Map.Entry;
25
26 import net.pterodactylus.wotns.freenet.wot.Identity;
27
28 /**
29  * TODO
30  *
31  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
32  */
33 public class IdentityTargets implements Iterable<Entry<String, String>> {
34
35         private final Identity identity;
36
37         private final Map<String, String> targets = new HashMap<String, String>();
38
39         public IdentityTargets(Identity identity) {
40                 this.identity = identity;
41         }
42
43         public Map<String, String> getTargets() {
44                 scanForTargets();
45                 return Collections.unmodifiableMap(targets);
46         }
47
48         public String getTarget(String name) {
49                 scanForTargets();
50                 return targets.get(name);
51         }
52
53         //
54         // PRIVATE METHODS
55         //
56
57         private void scanForTargets() {
58                 synchronized (targets) {
59                         for (Entry<String, String> property : identity.getProperties().entrySet()) {
60                                 if (property.getKey().startsWith("tns.")) {
61                                         targets.put(property.getKey().substring(4), property.getValue());
62                                 }
63                         }
64                 }
65         }
66
67         //
68         // ITERABLE METHODS
69         //
70
71         /**
72          * {@inheritDoc}
73          */
74         @Override
75         public Iterator<Entry<String, String>> iterator() {
76                 return targets.entrySet().iterator();
77         }
78
79 }