Add javadoc, suppress warnings.
[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  * Scans an {@link Identity}’s properties for WoTNS targets.
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         /** The identity being scanned. */
36         private final Identity identity;
37
38         /** The located targets. */
39         private final Map<String, String> targets = new HashMap<String, String>();
40
41         /**
42          * Creates a new target scanner for the given identity.
43          *
44          * @param identity
45          *            The identity to scan for targets
46          */
47         public IdentityTargets(Identity identity) {
48                 this.identity = identity;
49         }
50
51         //
52         // ACCESSORS
53         //
54
55         /**
56          * Returns the targets of the identity.
57          *
58          * @return The targets defined in the identity
59          */
60         public Map<String, String> getTargets() {
61                 scanForTargets();
62                 return Collections.unmodifiableMap(targets);
63         }
64
65         /**
66          * Returns the target with the given name.
67          *
68          * @param name
69          *            The name of the target
70          * @return The target
71          */
72         public String getTarget(String name) {
73                 scanForTargets();
74                 return targets.get(name);
75         }
76
77         //
78         // PRIVATE METHODS
79         //
80
81         /**
82          * Re-scans the identity for targets.
83          */
84         private void scanForTargets() {
85                 synchronized (targets) {
86                         targets.clear();
87                         for (Entry<String, String> property : identity.getProperties().entrySet()) {
88                                 if (property.getKey().startsWith("tns.")) {
89                                         targets.put(property.getKey().substring(4), property.getValue());
90                                 }
91                         }
92                 }
93         }
94
95         //
96         // ITERABLE METHODS
97         //
98
99         /**
100          * {@inheritDoc}
101          */
102         @Override
103         public Iterator<Entry<String, String>> iterator() {
104                 synchronized (targets) {
105                         scanForTargets();
106                         return new HashMap<String, String>(targets).entrySet().iterator();
107                 }
108         }
109
110 }