Merge branch 'release-0.0.8'
[WoTNS.git] / src / main / java / net / pterodactylus / wotns / main / IdentityTargets.java
index 2eacd08..a992091 100644 (file)
@@ -26,25 +26,49 @@ import java.util.Map.Entry;
 import net.pterodactylus.wotns.freenet.wot.Identity;
 
 /**
- * TODO
+ * Scans an {@link Identity}’s properties for WoTNS targets.
  *
  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
  */
 public class IdentityTargets implements Iterable<Entry<String, String>> {
 
+       /** The identity being scanned. */
        private final Identity identity;
 
+       /** The located targets. */
        private final Map<String, String> targets = new HashMap<String, String>();
 
+       /**
+        * Creates a new target scanner for the given identity.
+        *
+        * @param identity
+        *            The identity to scan for targets
+        */
        public IdentityTargets(Identity identity) {
                this.identity = identity;
        }
 
+       //
+       // ACCESSORS
+       //
+
+       /**
+        * Returns the targets of the identity.
+        *
+        * @return The targets defined in the identity
+        */
        public Map<String, String> getTargets() {
                scanForTargets();
                return Collections.unmodifiableMap(targets);
        }
 
+       /**
+        * Returns the target with the given name.
+        *
+        * @param name
+        *            The name of the target
+        * @return The target
+        */
        public String getTarget(String name) {
                scanForTargets();
                return targets.get(name);
@@ -54,8 +78,12 @@ public class IdentityTargets implements Iterable<Entry<String, String>> {
        // PRIVATE METHODS
        //
 
+       /**
+        * Re-scans the identity for targets.
+        */
        private void scanForTargets() {
                synchronized (targets) {
+                       targets.clear();
                        for (Entry<String, String> property : identity.getProperties().entrySet()) {
                                if (property.getKey().startsWith("tns.")) {
                                        targets.put(property.getKey().substring(4), property.getValue());
@@ -73,7 +101,10 @@ public class IdentityTargets implements Iterable<Entry<String, String>> {
         */
        @Override
        public Iterator<Entry<String, String>> iterator() {
-               return targets.entrySet().iterator();
+               synchronized (targets) {
+                       scanForTargets();
+                       return new HashMap<String, String>(targets).entrySet().iterator();
+               }
        }
 
 }