From 036c6fa20ce11ce8093f46fc0fce1c1da1839789 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Wed, 16 Jan 2013 12:14:58 +0100 Subject: [PATCH] =?utf8?q?Replace=20utils=E2=80=99=20Pair=20by=20custom=20?= =?utf8?q?containers.?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../pterodactylus/sone/core/FreenetInterface.java | 55 +++++++++++++++++-- .../pterodactylus/sone/core/SoneDownloader.java | 8 +-- .../net/pterodactylus/sone/core/UpdateChecker.java | 7 ++- .../sone/freenet/plugin/PluginConnector.java | 61 ++++++++++++++++++++-- 4 files changed, 115 insertions(+), 16 deletions(-) diff --git a/src/main/java/net/pterodactylus/sone/core/FreenetInterface.java b/src/main/java/net/pterodactylus/sone/core/FreenetInterface.java index 4e8b49a..9515876 100644 --- a/src/main/java/net/pterodactylus/sone/core/FreenetInterface.java +++ b/src/main/java/net/pterodactylus/sone/core/FreenetInterface.java @@ -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 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(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 David Roden + */ + 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 David ‘Bombe’ Roden diff --git a/src/main/java/net/pterodactylus/sone/core/SoneDownloader.java b/src/main/java/net/pterodactylus/sone/core/SoneDownloader.java index 4142dda..28c6bf2 100644 --- a/src/main/java/net/pterodactylus/sone/core/SoneDownloader.java +++ b/src/main/java/net/pterodactylus/sone/core/SoneDownloader.java @@ -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 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); diff --git a/src/main/java/net/pterodactylus/sone/core/UpdateChecker.java b/src/main/java/net/pterodactylus/sone/core/UpdateChecker.java index 3f4d235..5ccc96d 100644 --- a/src/main/java/net/pterodactylus/sone/core/UpdateChecker.java +++ b/src/main/java/net/pterodactylus/sone/core/UpdateChecker.java @@ -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 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; diff --git a/src/main/java/net/pterodactylus/sone/freenet/plugin/PluginConnector.java b/src/main/java/net/pterodactylus/sone/freenet/plugin/PluginConnector.java index 1dae44b..d556a6c 100644 --- a/src/main/java/net/pterodactylus/sone/freenet/plugin/PluginConnector.java +++ b/src/main/java/net/pterodactylus/sone/freenet/plugin/PluginConnector.java @@ -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, ConnectorListenerManager> connectorListenerManagers = Collections.synchronizedMap(new HashMap, ConnectorListenerManager>()); + private final Map connectorListenerManagers = Collections.synchronizedMap(new HashMap()); /** * 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(pluginName, identifier)); + ConnectorListenerManager connectorListenerManager = connectorListenerManagers.get(new PluginIdentifier(pluginName, identifier)); if (create && (connectorListenerManager == null)) { connectorListenerManager = new ConnectorListenerManager(this); - connectorListenerManagers.put(new Pair(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 David Roden + */ + 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); + } + + } + } -- 2.7.4