Replace utils’ Numbers by Guava’s Optional and Ints/Longs.tryParse.
[Sone.git] / src / main / java / net / pterodactylus / sone / core / SoneDownloader.java
index 72ff008..9bc14a8 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Sone - SoneDownloader.java - Copyright © 2010–2012 David Roden
+ * Sone - SoneDownloader.java - Copyright © 2010–2013 David Roden
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -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,16 +35,18 @@ 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;
 import net.pterodactylus.util.service.AbstractService;
 import net.pterodactylus.util.xml.SimpleXML;
 import net.pterodactylus.util.xml.XML;
 
 import org.w3c.dom.Document;
 
+import com.google.common.base.Optional;
+import com.google.common.primitives.Ints;
+import com.google.common.primitives.Longs;
+
 import freenet.client.FetchResult;
 import freenet.keys.FreenetURI;
 import freenet.support.api.Bucket;
@@ -155,13 +158,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);
@@ -236,7 +239,7 @@ public class SoneDownloader extends AbstractService {
                        return null;
                }
 
-               Sone sone = new Sone(originalSone.getId()).setIdentity(originalSone.getIdentity());
+               Sone sone = new Sone(originalSone.getId(), false).setIdentity(originalSone.getIdentity());
 
                SimpleXML soneXml;
                try {
@@ -250,7 +253,7 @@ public class SoneDownloader extends AbstractService {
                Integer protocolVersion = null;
                String soneProtocolVersion = soneXml.getValue("protocol-version", null);
                if (soneProtocolVersion != null) {
-                       protocolVersion = Numbers.safeParseInteger(soneProtocolVersion);
+                       protocolVersion = Optional.fromNullable(Ints.tryParse(soneProtocolVersion)).or(0);
                }
                if (protocolVersion == null) {
                        logger.log(Level.INFO, "No protocol version found, assuming 0.");
@@ -327,9 +330,9 @@ public class SoneDownloader extends AbstractService {
                String profileFirstName = profileXml.getValue("first-name", null);
                String profileMiddleName = profileXml.getValue("middle-name", null);
                String profileLastName = profileXml.getValue("last-name", null);
-               Integer profileBirthDay = Numbers.safeParseInteger(profileXml.getValue("birth-day", null));
-               Integer profileBirthMonth = Numbers.safeParseInteger(profileXml.getValue("birth-month", null));
-               Integer profileBirthYear = Numbers.safeParseInteger(profileXml.getValue("birth-year", null));
+               Integer profileBirthDay = Ints.tryParse(profileXml.getValue("birth-day", null));
+               Integer profileBirthMonth = Ints.tryParse(profileXml.getValue("birth-month", null));
+               Integer profileBirthYear = Ints.tryParse(profileXml.getValue("birth-year", null));
                Profile profile = new Profile(sone).setFirstName(profileFirstName).setMiddleName(profileMiddleName).setLastName(profileLastName);
                profile.setBirthDay(profileBirthDay).setBirthMonth(profileBirthMonth).setBirthYear(profileBirthYear);
                /* avatar is processed after images are loaded. */
@@ -403,7 +406,7 @@ public class SoneDownloader extends AbstractService {
                                        return null;
                                }
                                try {
-                                       replies.add(core.getReply(replyId).setSone(sone).setPost(core.getPost(replyPostId)).setTime(Long.parseLong(replyTime)).setText(replyText));
+                                       replies.add(core.getPostReply(replyId, true).setSone(sone).setPost(core.getPost(replyPostId)).setTime(Long.parseLong(replyTime)).setText(replyText));
                                } catch (NumberFormatException nfe1) {
                                        /* TODO - mark Sone as bad. */
                                        logger.log(Level.WARNING, String.format("Downloaded reply for Sone %s with invalid time: %s", sone, replyTime));
@@ -480,9 +483,9 @@ public class SoneDownloader extends AbstractService {
                                                        logger.log(Level.WARNING, String.format("Downloaded Sone %s contains invalid images!", sone));
                                                        return null;
                                                }
-                                               long creationTime = Numbers.safeParseLong(imageCreationTimeString, 0L);
-                                               int imageWidth = Numbers.safeParseInteger(imageWidthString, 0);
-                                               int imageHeight = Numbers.safeParseInteger(imageHeightString, 0);
+                                               long creationTime = Optional.fromNullable(Longs.tryParse(imageCreationTimeString)).or(0L);
+                                               int imageWidth = Optional.fromNullable(Ints.tryParse(imageWidthString)).or(0);
+                                               int imageHeight = Optional.fromNullable(Ints.tryParse(imageHeightString)).or(0);
                                                if ((imageWidth < 1) || (imageHeight < 1)) {
                                                        logger.log(Level.WARNING, String.format("Downloaded Sone %s contains image %s with invalid dimensions (%s, %s)!", sone, imageId, imageWidthString, imageHeightString));
                                                        return null;