Replace utils’ Pair by custom containers.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 16 Jan 2013 11:14:58 +0000 (12:14 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 16 Jan 2013 18:04:10 +0000 (19:04 +0100)
src/main/java/net/pterodactylus/sone/core/FreenetInterface.java
src/main/java/net/pterodactylus/sone/core/SoneDownloader.java
src/main/java/net/pterodactylus/sone/core/UpdateChecker.java
src/main/java/net/pterodactylus/sone/freenet/plugin/PluginConnector.java

index 4e8b49a..9515876 100644 (file)
@@ -29,7 +29,6 @@ import java.util.logging.Logger;
 import net.pterodactylus.sone.data.Image;
 import net.pterodactylus.sone.data.Sone;
 import net.pterodactylus.sone.data.TemporaryImage;
-import net.pterodactylus.util.collection.Pair;
 import net.pterodactylus.util.logging.Logging;
 
 import com.db4o.ObjectContainer;
@@ -101,13 +100,13 @@ public class FreenetInterface {
         *            The URI to fetch
         * @return The result of the fetch, or {@code null} if an error occured
         */
-       public Pair<FreenetURI, FetchResult> fetchUri(FreenetURI uri) {
+       public Fetched fetchUri(FreenetURI uri) {
                FetchResult fetchResult = null;
                FreenetURI currentUri = new FreenetURI(uri);
                while (true) {
                        try {
                                fetchResult = client.fetch(currentUri);
-                               return new Pair<FreenetURI, FetchResult>(currentUri, fetchResult);
+                               return new Fetched(currentUri, fetchResult);
                        } catch (FetchException fe1) {
                                if (fe1.getMode() == FetchException.PERMANENT_REDIRECT) {
                                        currentUri = fe1.newURI;
@@ -302,6 +301,56 @@ public class FreenetInterface {
        }
 
        /**
+        * Container for a fetched URI and the {@link FetchResult}.
+        *
+        * @author <a href="mailto:d.roden@xplosion.de">David Roden</a>
+        */
+       public static class Fetched {
+
+               /** The fetched URI. */
+               private final FreenetURI freenetUri;
+
+               /** The fetch result. */
+               private final FetchResult fetchResult;
+
+               /**
+                * Creates a new fetched URI.
+                *
+                * @param freenetUri
+                *            The URI that was fetched
+                * @param fetchResult
+                *            The fetch result
+                */
+               public Fetched(FreenetURI freenetUri, FetchResult fetchResult) {
+                       this.freenetUri = freenetUri;
+                       this.fetchResult = fetchResult;
+               }
+
+               //
+               // ACCESSORS
+               //
+
+               /**
+                * Returns the fetched URI.
+                *
+                * @return The fetched URI
+                */
+               public FreenetURI getFreenetUri() {
+                       return freenetUri;
+               }
+
+               /**
+                * Returns the fetch result.
+                *
+                * @return The fetch result
+                */
+               public FetchResult getFetchResult() {
+                       return fetchResult;
+               }
+
+       }
+
+       /**
         * Callback for USK watcher events.
         *
         * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
index 4142dda..28c6bf2 100644 (file)
@@ -26,6 +26,7 @@ import java.util.Set;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
+import net.pterodactylus.sone.core.FreenetInterface.Fetched;
 import net.pterodactylus.sone.data.Album;
 import net.pterodactylus.sone.data.Client;
 import net.pterodactylus.sone.data.Image;
@@ -34,7 +35,6 @@ import net.pterodactylus.sone.data.PostReply;
 import net.pterodactylus.sone.data.Profile;
 import net.pterodactylus.sone.data.Sone;
 import net.pterodactylus.sone.data.Sone.SoneStatus;
-import net.pterodactylus.util.collection.Pair;
 import net.pterodactylus.util.io.Closer;
 import net.pterodactylus.util.logging.Logging;
 import net.pterodactylus.util.number.Numbers;
@@ -155,13 +155,13 @@ public class SoneDownloader extends AbstractService {
                FreenetURI requestUri = soneUri.setMetaString(new String[] { "sone.xml" });
                sone.setStatus(SoneStatus.downloading);
                try {
-                       Pair<FreenetURI, FetchResult> fetchResults = freenetInterface.fetchUri(requestUri);
+                       Fetched fetchResults = freenetInterface.fetchUri(requestUri);
                        if (fetchResults == null) {
                                /* TODO - mark Sone as bad. */
                                return null;
                        }
-                       logger.log(Level.FINEST, String.format("Got %d bytes back.", fetchResults.getRight().size()));
-                       Sone parsedSone = parseSone(sone, fetchResults.getRight(), fetchResults.getLeft());
+                       logger.log(Level.FINEST, String.format("Got %d bytes back.", fetchResults.getFetchResult().size()));
+                       Sone parsedSone = parseSone(sone, fetchResults.getFetchResult(), fetchResults.getFreenetUri());
                        if (parsedSone != null) {
                                if (!fetchOnly) {
                                        core.updateSone(parsedSone);
index 3f4d235..5ccc96d 100644 (file)
@@ -26,12 +26,11 @@ import java.util.Properties;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
+import net.pterodactylus.sone.core.FreenetInterface.Fetched;
 import net.pterodactylus.sone.main.SonePlugin;
-import net.pterodactylus.util.collection.Pair;
 import net.pterodactylus.util.io.Closer;
 import net.pterodactylus.util.logging.Logging;
 import net.pterodactylus.util.version.Version;
-import freenet.client.FetchResult;
 import freenet.keys.FreenetURI;
 import freenet.support.api.Bucket;
 
@@ -168,12 +167,12 @@ public class UpdateChecker {
                        public void editionFound(FreenetURI uri, long edition, boolean newKnownGood, boolean newSlot) {
                                logger.log(Level.FINEST, String.format("Found update for %s: %d, %s, %s", uri, edition, newKnownGood, newSlot));
                                if (newKnownGood || newSlot) {
-                                       Pair<FreenetURI, FetchResult> uriResult = freenetInterface.fetchUri(uri.setMetaString(new String[] { "sone.properties" }));
+                                       Fetched uriResult = freenetInterface.fetchUri(uri.setMetaString(new String[] { "sone.properties" }));
                                        if (uriResult == null) {
                                                logger.log(Level.WARNING, String.format("Could not fetch properties of latest homepage: %s", uri));
                                                return;
                                        }
-                                       Bucket resultBucket = uriResult.getRight().asBucket();
+                                       Bucket resultBucket = uriResult.getFetchResult().asBucket();
                                        try {
                                                parseProperties(resultBucket.getInputStream(), edition);
                                                latestEdition = edition;
index 1dae44b..d556a6c 100644 (file)
@@ -21,8 +21,6 @@ import java.util.Collections;
 import java.util.HashMap;
 import java.util.Map;
 
-import net.pterodactylus.util.collection.Pair;
-
 import com.google.inject.Inject;
 
 import freenet.pluginmanager.FredPluginTalker;
@@ -44,7 +42,7 @@ public class PluginConnector implements FredPluginTalker {
        private final PluginRespirator pluginRespirator;
 
        /** Connector listener managers for all plugin connections. */
-       private final Map<Pair<String, String>, ConnectorListenerManager> connectorListenerManagers = Collections.synchronizedMap(new HashMap<Pair<String, String>, ConnectorListenerManager>());
+       private final Map<PluginIdentifier, ConnectorListenerManager> connectorListenerManagers = Collections.synchronizedMap(new HashMap<PluginIdentifier, ConnectorListenerManager>());
 
        /**
         * Creates a new plugin connector.
@@ -160,10 +158,10 @@ public class PluginConnector implements FredPluginTalker {
         *         and {@code create} is {@code false}
         */
        private ConnectorListenerManager getConnectorListenerManager(String pluginName, String identifier, boolean create) {
-               ConnectorListenerManager connectorListenerManager = connectorListenerManagers.get(new Pair<String, String>(pluginName, identifier));
+               ConnectorListenerManager connectorListenerManager = connectorListenerManagers.get(new PluginIdentifier(pluginName, identifier));
                if (create && (connectorListenerManager == null)) {
                        connectorListenerManager = new ConnectorListenerManager(this);
-                       connectorListenerManagers.put(new Pair<String, String>(pluginName, identifier), connectorListenerManager);
+                       connectorListenerManagers.put(new PluginIdentifier(pluginName, identifier), connectorListenerManager);
                }
                return connectorListenerManager;
        }
@@ -204,4 +202,57 @@ public class PluginConnector implements FredPluginTalker {
                connectorListenerManager.fireReceivedReply(params, data);
        }
 
+       /**
+        * Container for identifying plugins. Plugins are identified by their plugin
+        * name and their unique identifier.
+        *
+        * @author <a href="mailto:d.roden@xplosion.de">David Roden</a>
+        */
+       private static class PluginIdentifier {
+
+               /** The plugin name. */
+               private final String pluginName;
+
+               /** The plugin identifier. */
+               private final String identifier;
+
+               /**
+                * Creates a new plugin identifier.
+                *
+                * @param pluginName
+                *            The name of the plugin
+                * @param identifier
+                *            The identifier of the plugin
+                */
+               public PluginIdentifier(String pluginName, String identifier) {
+                       this.pluginName = pluginName;
+                       this.identifier = identifier;
+               }
+
+               //
+               // OBJECT METHODS
+               //
+
+               /**
+                * {@inheritDoc}
+                */
+               @Override
+               public int hashCode() {
+                       return pluginName.hashCode() ^ identifier.hashCode();
+               }
+
+               /**
+                * {@inheritDoc}
+                */
+               @Override
+               public boolean equals(Object object) {
+                       if (!(object instanceof PluginIdentifier)) {
+                               return false;
+                       }
+                       PluginIdentifier pluginIdentifier = (PluginIdentifier) object;
+                       return pluginName.equals(pluginIdentifier.pluginName) && identifier.equals(pluginIdentifier.identifier);
+               }
+
+       }
+
 }