From: David ‘Bombe’ Roden Date: Fri, 18 Jan 2013 05:27:52 +0000 (+0100) Subject: Revert "Replace utils’ Numbers by Guava’s Optional and Ints/Longs.tryParse." X-Git-Tag: 0.8.5^2~3^2~62 X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=commitdiff_plain;h=9cb50c365e3bf7a74ffa40a45630a0e7be40b526 Revert "Replace utils’ Numbers by Guava’s Optional and Ints/Longs.tryParse." This reverts commit d5efb086bee8f103cbe90c7a953ffbb7ff27b689 because Ints/ Longs.tryParse() throws an NPE if the given String is null. --- diff --git a/src/main/java/net/pterodactylus/sone/core/Core.java b/src/main/java/net/pterodactylus/sone/core/Core.java index b603eff..5a54f76 100644 --- a/src/main/java/net/pterodactylus/sone/core/Core.java +++ b/src/main/java/net/pterodactylus/sone/core/Core.java @@ -79,17 +79,16 @@ import net.pterodactylus.sone.utils.IntegerRangePredicate; import net.pterodactylus.util.config.Configuration; import net.pterodactylus.util.config.ConfigurationException; import net.pterodactylus.util.logging.Logging; +import net.pterodactylus.util.number.Numbers; import net.pterodactylus.util.service.AbstractService; import net.pterodactylus.util.thread.NamedThreadFactory; import net.pterodactylus.util.thread.Ticker; -import com.google.common.base.Optional; import com.google.common.base.Predicate; import com.google.common.base.Predicates; import com.google.common.collect.Collections2; import com.google.common.eventbus.EventBus; import com.google.common.eventbus.Subscribe; -import com.google.common.primitives.Longs; import com.google.inject.Inject; import freenet.keys.FreenetURI; @@ -824,7 +823,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider logger.log(Level.SEVERE, String.format("Could not convert the Identity’s URIs to Freenet URIs: %s, %s", ownIdentity.getInsertUri(), ownIdentity.getRequestUri()), mue1); return null; } - sone.setLatestEdition(Optional.fromNullable(Longs.tryParse(ownIdentity.getProperty("Sone.LatestEdition"))).or(0L)); + sone.setLatestEdition(Numbers.safeParseLong(ownIdentity.getProperty("Sone.LatestEdition"), (long) 0)); sone.setClient(new Client("Sone", SonePlugin.VERSION.toString())); sone.setKnown(true); /* TODO - load posts ’n stuff */ @@ -879,7 +878,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider final Sone sone = getRemoteSone(identity.getId(), true).setIdentity(identity); boolean newSone = sone.getRequestUri() == null; sone.setRequestUri(getSoneUri(identity.getRequestUri())); - sone.setLatestEdition(Optional.fromNullable(Longs.tryParse(identity.getProperty("Sone.LatestEdition"))).or(0L)); + sone.setLatestEdition(Numbers.safeParseLong(identity.getProperty("Sone.LatestEdition"), (long) 0)); if (newSone) { synchronized (knownSones) { newSone = !knownSones.contains(sone.getId()); @@ -2394,7 +2393,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider public void run() { Sone sone = getRemoteSone(identity.getId(), false); sone.setIdentity(identity); - sone.setLatestEdition(Optional.fromNullable(Longs.tryParse(identity.getProperty("Sone.LatestEdition"))).or(sone.getLatestEdition())); + sone.setLatestEdition(Numbers.safeParseLong(identity.getProperty("Sone.LatestEdition"), sone.getLatestEdition())); soneDownloader.addSone(sone); soneDownloader.fetchSone(sone); } diff --git a/src/main/java/net/pterodactylus/sone/core/SoneDownloader.java b/src/main/java/net/pterodactylus/sone/core/SoneDownloader.java index 9bc14a8..70a193a 100644 --- a/src/main/java/net/pterodactylus/sone/core/SoneDownloader.java +++ b/src/main/java/net/pterodactylus/sone/core/SoneDownloader.java @@ -37,16 +37,13 @@ import net.pterodactylus.sone.data.Sone; import net.pterodactylus.sone.data.Sone.SoneStatus; 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; @@ -253,7 +250,7 @@ public class SoneDownloader extends AbstractService { Integer protocolVersion = null; String soneProtocolVersion = soneXml.getValue("protocol-version", null); if (soneProtocolVersion != null) { - protocolVersion = Optional.fromNullable(Ints.tryParse(soneProtocolVersion)).or(0); + protocolVersion = Numbers.safeParseInteger(soneProtocolVersion); } if (protocolVersion == null) { logger.log(Level.INFO, "No protocol version found, assuming 0."); @@ -330,9 +327,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 = 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)); + 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)); 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. */ @@ -483,9 +480,9 @@ public class SoneDownloader extends AbstractService { logger.log(Level.WARNING, String.format("Downloaded Sone %s contains invalid images!", sone)); return null; } - 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); + long creationTime = Numbers.safeParseLong(imageCreationTimeString, 0L); + int imageWidth = Numbers.safeParseInteger(imageWidthString, 0); + int imageHeight = Numbers.safeParseInteger(imageHeightString, 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; diff --git a/src/main/java/net/pterodactylus/sone/freenet/wot/WebOfTrustConnector.java b/src/main/java/net/pterodactylus/sone/freenet/wot/WebOfTrustConnector.java index d3e8747..07628e3 100644 --- a/src/main/java/net/pterodactylus/sone/freenet/wot/WebOfTrustConnector.java +++ b/src/main/java/net/pterodactylus/sone/freenet/wot/WebOfTrustConnector.java @@ -29,11 +29,10 @@ import net.pterodactylus.sone.freenet.plugin.PluginConnector; import net.pterodactylus.sone.freenet.plugin.PluginException; import net.pterodactylus.sone.freenet.plugin.event.ReceivedReplyEvent; import net.pterodactylus.util.logging.Logging; +import net.pterodactylus.util.number.Numbers; -import com.google.common.base.Optional; import com.google.common.collect.MapMaker; import com.google.common.eventbus.Subscribe; -import com.google.common.primitives.Ints; import com.google.inject.Inject; import freenet.support.SimpleFieldSet; @@ -153,9 +152,9 @@ public class WebOfTrustConnector { DefaultIdentity identity = new DefaultIdentity(id, nickname, requestUri); identity.setContexts(parseContexts("Contexts" + identityCounter + ".", fields)); identity.setProperties(parseProperties("Properties" + identityCounter + ".", fields)); - Integer trust = Ints.tryParse(fields.get("Trust" + identityCounter)); - int score = Optional.fromNullable(Ints.tryParse(fields.get("Score" + identityCounter))).or(0); - int rank = Optional.fromNullable(Ints.tryParse(fields.get("Rank" + identityCounter))).or(0); + Integer trust = Numbers.safeParseInteger(fields.get("Trust" + identityCounter), null); + int score = Numbers.safeParseInteger(fields.get("Score" + identityCounter), 0); + int rank = Numbers.safeParseInteger(fields.get("Rank" + identityCounter), 0); identity.setTrust(ownIdentity, new Trust(trust, score, rank)); identities.add(identity); } diff --git a/src/main/java/net/pterodactylus/sone/template/ImageLinkFilter.java b/src/main/java/net/pterodactylus/sone/template/ImageLinkFilter.java index eea9121..7e3723c 100644 --- a/src/main/java/net/pterodactylus/sone/template/ImageLinkFilter.java +++ b/src/main/java/net/pterodactylus/sone/template/ImageLinkFilter.java @@ -23,6 +23,7 @@ import java.util.Map; import net.pterodactylus.sone.core.Core; import net.pterodactylus.sone.data.Image; +import net.pterodactylus.util.number.Numbers; import net.pterodactylus.util.template.Filter; import net.pterodactylus.util.template.Template; import net.pterodactylus.util.template.TemplateContext; @@ -30,7 +31,6 @@ import net.pterodactylus.util.template.TemplateContextFactory; import net.pterodactylus.util.template.TemplateParser; import com.google.common.base.Optional; -import com.google.common.primitives.Ints; /** * Template filter that turns an {@link Image} into an HTML <img> tag, @@ -77,8 +77,8 @@ public class ImageLinkFilter implements Filter { return null; } String imageClass = String.valueOf(parameters.get("class")); - int maxWidth = Optional.fromNullable(Ints.tryParse(String.valueOf(parameters.get("max-width")))).or(Integer.MAX_VALUE); - int maxHeight = Optional.fromNullable(Ints.tryParse(String.valueOf(parameters.get("max-height")))).or(Integer.MAX_VALUE); + int maxWidth = Numbers.safeParseInteger(parameters.get("max-width"), Integer.MAX_VALUE); + int maxHeight = Numbers.safeParseInteger(parameters.get("max-height"), Integer.MAX_VALUE); String mode = String.valueOf(parameters.get("mode")); String title = String.valueOf(parameters.get("title")); diff --git a/src/main/java/net/pterodactylus/sone/template/ParserFilter.java b/src/main/java/net/pterodactylus/sone/template/ParserFilter.java index db41830..d2f2619 100644 --- a/src/main/java/net/pterodactylus/sone/template/ParserFilter.java +++ b/src/main/java/net/pterodactylus/sone/template/ParserFilter.java @@ -38,15 +38,13 @@ import net.pterodactylus.sone.text.SonePart; import net.pterodactylus.sone.text.SoneTextParser; import net.pterodactylus.sone.text.SoneTextParserContext; import net.pterodactylus.sone.web.page.FreenetRequest; +import net.pterodactylus.util.number.Numbers; import net.pterodactylus.util.template.Filter; import net.pterodactylus.util.template.Template; import net.pterodactylus.util.template.TemplateContext; import net.pterodactylus.util.template.TemplateContextFactory; import net.pterodactylus.util.template.TemplateParser; -import com.google.common.base.Optional; -import com.google.common.primitives.Ints; - /** * Filter that filters a given text through a {@link SoneTextParser}. * @@ -92,8 +90,8 @@ public class ParserFilter implements Filter { @Override public Object format(TemplateContext templateContext, Object data, Map parameters) { String text = String.valueOf(data); - int length = Optional.fromNullable(Ints.tryParse(String.valueOf(parameters.get("length")))).or(-1); - int cutOffLength = Optional.fromNullable(Ints.tryParse(String.valueOf(parameters.get("cut-off-length")))).or(length); + int length = Numbers.safeParseInteger(parameters.get("length"), Numbers.safeParseInteger(templateContext.get(String.valueOf(parameters.get("length"))), -1)); + int cutOffLength = Numbers.safeParseInteger(parameters.get("cut-off-length"), Numbers.safeParseInteger(templateContext.get(String.valueOf(parameters.get("cut-off-length"))), length)); Object sone = parameters.get("sone"); if (sone instanceof String) { sone = core.getSone((String) sone, false); diff --git a/src/main/java/net/pterodactylus/sone/web/BookmarksPage.java b/src/main/java/net/pterodactylus/sone/web/BookmarksPage.java index dda4cac..10b6dc3 100644 --- a/src/main/java/net/pterodactylus/sone/web/BookmarksPage.java +++ b/src/main/java/net/pterodactylus/sone/web/BookmarksPage.java @@ -26,13 +26,12 @@ import java.util.Set; import net.pterodactylus.sone.data.Post; import net.pterodactylus.sone.web.page.FreenetRequest; import net.pterodactylus.util.collection.Pagination; +import net.pterodactylus.util.number.Numbers; import net.pterodactylus.util.template.Template; import net.pterodactylus.util.template.TemplateContext; -import com.google.common.base.Optional; import com.google.common.base.Predicate; import com.google.common.collect.Collections2; -import com.google.common.primitives.Ints; /** * Page that lets the user browse all his bookmarked posts. @@ -73,7 +72,7 @@ public class BookmarksPage extends SoneTemplatePage { }); List sortedPosts = new ArrayList(loadedPosts); Collections.sort(sortedPosts, Post.TIME_COMPARATOR); - Pagination pagination = new Pagination(sortedPosts, webInterface.getCore().getPreferences().getPostsPerPage()).setPage(Optional.fromNullable(Ints.tryParse(request.getHttpRequest().getParam("page"))).or(0)); + Pagination pagination = new Pagination(sortedPosts, webInterface.getCore().getPreferences().getPostsPerPage()).setPage(Numbers.safeParseInteger(request.getHttpRequest().getParam("page"), 0)); templateContext.set("pagination", pagination); templateContext.set("posts", pagination.getItems()); templateContext.set("postsNotLoaded", allPosts.size() != loadedPosts.size()); diff --git a/src/main/java/net/pterodactylus/sone/web/EditProfilePage.java b/src/main/java/net/pterodactylus/sone/web/EditProfilePage.java index 62957c2..c79a1e9 100644 --- a/src/main/java/net/pterodactylus/sone/web/EditProfilePage.java +++ b/src/main/java/net/pterodactylus/sone/web/EditProfilePage.java @@ -23,12 +23,10 @@ import net.pterodactylus.sone.data.Profile; import net.pterodactylus.sone.data.Profile.Field; import net.pterodactylus.sone.data.Sone; import net.pterodactylus.sone.web.page.FreenetRequest; +import net.pterodactylus.util.number.Numbers; import net.pterodactylus.util.template.Template; import net.pterodactylus.util.template.TemplateContext; import net.pterodactylus.util.web.Method; - -import com.google.common.primitives.Ints; - import freenet.clients.http.ToadletContext; /** @@ -76,9 +74,9 @@ public class EditProfilePage extends SoneTemplatePage { firstName = request.getHttpRequest().getPartAsStringFailsafe("first-name", 256).trim(); middleName = request.getHttpRequest().getPartAsStringFailsafe("middle-name", 256).trim(); lastName = request.getHttpRequest().getPartAsStringFailsafe("last-name", 256).trim(); - birthDay = Ints.tryParse(request.getHttpRequest().getPartAsStringFailsafe("birth-day", 256).trim()); - birthMonth = Ints.tryParse(request.getHttpRequest().getPartAsStringFailsafe("birth-month", 256).trim()); - birthYear = Ints.tryParse(request.getHttpRequest().getPartAsStringFailsafe("birth-year", 256).trim()); + birthDay = Numbers.safeParseInteger(request.getHttpRequest().getPartAsStringFailsafe("birth-day", 256).trim()); + birthMonth = Numbers.safeParseInteger(request.getHttpRequest().getPartAsStringFailsafe("birth-month", 256).trim()); + birthYear = Numbers.safeParseInteger(request.getHttpRequest().getPartAsStringFailsafe("birth-year", 256).trim()); avatarId = request.getHttpRequest().getPartAsStringFailsafe("avatarId", 36); profile.setFirstName(firstName.length() > 0 ? firstName : null); profile.setMiddleName(middleName.length() > 0 ? middleName : null); diff --git a/src/main/java/net/pterodactylus/sone/web/ImageBrowserPage.java b/src/main/java/net/pterodactylus/sone/web/ImageBrowserPage.java index ea039ca..4a9548e 100644 --- a/src/main/java/net/pterodactylus/sone/web/ImageBrowserPage.java +++ b/src/main/java/net/pterodactylus/sone/web/ImageBrowserPage.java @@ -27,12 +27,10 @@ import net.pterodactylus.sone.data.Image; import net.pterodactylus.sone.data.Sone; import net.pterodactylus.sone.web.page.FreenetRequest; import net.pterodactylus.util.collection.Pagination; +import net.pterodactylus.util.number.Numbers; import net.pterodactylus.util.template.Template; import net.pterodactylus.util.template.TemplateContext; -import com.google.common.base.Optional; -import com.google.common.primitives.Ints; - /** * The image browser page is the entry page for the image management. * @@ -92,7 +90,7 @@ public class ImageBrowserPage extends SoneTemplatePage { albums.addAll(sone.getAllAlbums()); } Collections.sort(albums, Album.TITLE_COMPARATOR); - Pagination albumPagination = new Pagination(albums, 12).setPage(Optional.fromNullable(Ints.tryParse(request.getHttpRequest().getParam("page"))).or(0)); + Pagination albumPagination = new Pagination(albums, 12).setPage(Numbers.safeParseInteger(request.getHttpRequest().getParam("page"), 0)); templateContext.set("albumPagination", albumPagination); templateContext.set("albums", albumPagination.getItems()); return; diff --git a/src/main/java/net/pterodactylus/sone/web/IndexPage.java b/src/main/java/net/pterodactylus/sone/web/IndexPage.java index ff7395e..b4307e3 100644 --- a/src/main/java/net/pterodactylus/sone/web/IndexPage.java +++ b/src/main/java/net/pterodactylus/sone/web/IndexPage.java @@ -27,13 +27,12 @@ import net.pterodactylus.sone.data.Sone; import net.pterodactylus.sone.notify.ListNotificationFilters; import net.pterodactylus.sone.web.page.FreenetRequest; import net.pterodactylus.util.collection.Pagination; +import net.pterodactylus.util.number.Numbers; import net.pterodactylus.util.template.Template; import net.pterodactylus.util.template.TemplateContext; -import com.google.common.base.Optional; import com.google.common.base.Predicate; import com.google.common.collect.Collections2; -import com.google.common.primitives.Ints; /** * The index page shows the main page of Sone. This page will contain the posts @@ -89,7 +88,7 @@ public class IndexPage extends SoneTemplatePage { allPosts = Collections2.filter(allPosts, Post.FUTURE_POSTS_FILTER); List sortedPosts = new ArrayList(allPosts); Collections.sort(sortedPosts, Post.TIME_COMPARATOR); - Pagination pagination = new Pagination(sortedPosts, webInterface.getCore().getPreferences().getPostsPerPage()).setPage(Optional.fromNullable(Ints.tryParse(request.getHttpRequest().getParam("page"))).or(0)); + Pagination pagination = new Pagination(sortedPosts, webInterface.getCore().getPreferences().getPostsPerPage()).setPage(Numbers.safeParseInteger(request.getHttpRequest().getParam("page"), 0)); templateContext.set("pagination", pagination); templateContext.set("posts", pagination.getItems()); } diff --git a/src/main/java/net/pterodactylus/sone/web/KnownSonesPage.java b/src/main/java/net/pterodactylus/sone/web/KnownSonesPage.java index 65254e3..dd1b1fc 100644 --- a/src/main/java/net/pterodactylus/sone/web/KnownSonesPage.java +++ b/src/main/java/net/pterodactylus/sone/web/KnownSonesPage.java @@ -25,15 +25,14 @@ import java.util.List; import net.pterodactylus.sone.data.Sone; import net.pterodactylus.sone.web.page.FreenetRequest; import net.pterodactylus.util.collection.Pagination; +import net.pterodactylus.util.number.Numbers; import net.pterodactylus.util.template.Template; import net.pterodactylus.util.template.TemplateContext; -import com.google.common.base.Optional; import com.google.common.base.Predicate; import com.google.common.base.Predicates; import com.google.common.collect.Collections2; import com.google.common.collect.Ordering; -import com.google.common.primitives.Ints; /** * This page shows all known Sones. @@ -141,7 +140,7 @@ public class KnownSonesPage extends SoneTemplatePage { Collections.sort(sortedSones, Sone.NICE_NAME_COMPARATOR); } } - Pagination sonePagination = new Pagination(sortedSones, 25).setPage(Optional.fromNullable(Ints.tryParse(request.getHttpRequest().getParam("page"))).or(0)); + Pagination sonePagination = new Pagination(sortedSones, 25).setPage(Numbers.safeParseInteger(request.getHttpRequest().getParam("page"), 0)); templateContext.set("pagination", sonePagination); templateContext.set("knownSones", sonePagination.getItems()); } diff --git a/src/main/java/net/pterodactylus/sone/web/NewPage.java b/src/main/java/net/pterodactylus/sone/web/NewPage.java index af7c1ec..2d4ec55 100644 --- a/src/main/java/net/pterodactylus/sone/web/NewPage.java +++ b/src/main/java/net/pterodactylus/sone/web/NewPage.java @@ -28,12 +28,10 @@ import net.pterodactylus.sone.data.PostReply; import net.pterodactylus.sone.notify.ListNotificationFilters; import net.pterodactylus.sone.web.page.FreenetRequest; import net.pterodactylus.util.collection.Pagination; +import net.pterodactylus.util.number.Numbers; import net.pterodactylus.util.template.Template; import net.pterodactylus.util.template.TemplateContext; -import com.google.common.base.Optional; -import com.google.common.primitives.Ints; - /** * Page that displays all new posts and replies. The posts are filtered using * {@link ListNotificationFilters#filterPosts(java.util.Collection, net.pterodactylus.sone.data.Sone)} @@ -77,7 +75,7 @@ public class NewPage extends SoneTemplatePage { Collections.sort(sortedPosts, Post.TIME_COMPARATOR); /* paginate them. */ - Pagination pagination = new Pagination(sortedPosts, webInterface.getCore().getPreferences().getPostsPerPage()).setPage(Optional.fromNullable(Ints.tryParse(request.getHttpRequest().getParam("page"))).or(0)); + Pagination pagination = new Pagination(sortedPosts, webInterface.getCore().getPreferences().getPostsPerPage()).setPage(Numbers.safeParseInteger(request.getHttpRequest().getParam("page"), 0)); templateContext.set("pagination", pagination); templateContext.set("posts", pagination.getItems()); } diff --git a/src/main/java/net/pterodactylus/sone/web/OptionsPage.java b/src/main/java/net/pterodactylus/sone/web/OptionsPage.java index 41398c5..094a9cf 100644 --- a/src/main/java/net/pterodactylus/sone/web/OptionsPage.java +++ b/src/main/java/net/pterodactylus/sone/web/OptionsPage.java @@ -25,13 +25,11 @@ import net.pterodactylus.sone.data.Sone; import net.pterodactylus.sone.data.Sone.ShowCustomAvatars; import net.pterodactylus.sone.fcp.FcpInterface.FullAccessRequired; import net.pterodactylus.sone.web.page.FreenetRequest; +import net.pterodactylus.util.number.Numbers; import net.pterodactylus.util.template.Template; import net.pterodactylus.util.template.TemplateContext; import net.pterodactylus.util.web.Method; -import com.google.common.base.Optional; -import com.google.common.primitives.Ints; - /** * This page lets the user edit the options of the Sone plugin. * @@ -80,31 +78,31 @@ public class OptionsPage extends SoneTemplatePage { currentSone.getOptions(). getEnumOption("ShowCustomAvatars").set(ShowCustomAvatars.valueOf(showCustomAvatars)); webInterface.getCore().touchConfiguration(); } - Integer insertionDelay = Ints.tryParse(request.getHttpRequest().getPartAsStringFailsafe("insertion-delay", 16)); + Integer insertionDelay = Numbers.safeParseInteger(request.getHttpRequest().getPartAsStringFailsafe("insertion-delay", 16)); if (!preferences.validateInsertionDelay(insertionDelay)) { fieldErrors.add("insertion-delay"); } else { preferences.setInsertionDelay(insertionDelay); } - Integer postsPerPage = Ints.tryParse(request.getHttpRequest().getPartAsStringFailsafe("posts-per-page", 4)); + Integer postsPerPage = Numbers.safeParseInteger(request.getHttpRequest().getPartAsStringFailsafe("posts-per-page", 4), null); if (!preferences.validatePostsPerPage(postsPerPage)) { fieldErrors.add("posts-per-page"); } else { preferences.setPostsPerPage(postsPerPage); } - Integer imagesPerPage = Ints.tryParse(request.getHttpRequest().getPartAsStringFailsafe("images-per-page", 4)); + Integer imagesPerPage = Numbers.safeParseInteger(request.getHttpRequest().getPartAsStringFailsafe("images-per-page", 4), null); if (!preferences.validateImagesPerPage(imagesPerPage)) { fieldErrors.add("images-per-page"); } else { preferences.setImagesPerPage(imagesPerPage); } - Integer charactersPerPost = Ints.tryParse(request.getHttpRequest().getPartAsStringFailsafe("characters-per-post", 10)); + Integer charactersPerPost = Numbers.safeParseInteger(request.getHttpRequest().getPartAsStringFailsafe("characters-per-post", 10), null); if (!preferences.validateCharactersPerPost(charactersPerPost)) { fieldErrors.add("characters-per-post"); } else { preferences.setCharactersPerPost(charactersPerPost); } - Integer postCutOffLength = Ints.tryParse(request.getHttpRequest().getPartAsStringFailsafe("post-cut-off-length", 10)); + Integer postCutOffLength = Numbers.safeParseInteger(request.getHttpRequest().getPartAsStringFailsafe("post-cut-off-length", 10), null); if (!preferences.validatePostCutOffLength(postCutOffLength)) { fieldErrors.add("post-cut-off-length"); } else { @@ -112,13 +110,13 @@ public class OptionsPage extends SoneTemplatePage { } boolean requireFullAccess = request.getHttpRequest().isPartSet("require-full-access"); preferences.setRequireFullAccess(requireFullAccess); - Integer positiveTrust = Ints.tryParse(request.getHttpRequest().getPartAsStringFailsafe("positive-trust", 3)); + Integer positiveTrust = Numbers.safeParseInteger(request.getHttpRequest().getPartAsStringFailsafe("positive-trust", 3)); if (!preferences.validatePositiveTrust(positiveTrust)) { fieldErrors.add("positive-trust"); } else { preferences.setPositiveTrust(positiveTrust); } - Integer negativeTrust = Ints.tryParse(request.getHttpRequest().getPartAsStringFailsafe("negative-trust", 4)); + Integer negativeTrust = Numbers.safeParseInteger(request.getHttpRequest().getPartAsStringFailsafe("negative-trust", 4)); if (!preferences.validateNegativeTrust(negativeTrust)) { fieldErrors.add("negative-trust"); } else { @@ -131,7 +129,7 @@ public class OptionsPage extends SoneTemplatePage { preferences.setTrustComment(trustComment); boolean fcpInterfaceActive = request.getHttpRequest().isPartSet("fcp-interface-active"); preferences.setFcpInterfaceActive(fcpInterfaceActive); - Integer fcpFullAccessRequiredInteger = Optional.fromNullable(Ints.tryParse(request.getHttpRequest().getPartAsStringFailsafe("fcp-full-access-required", 1))).or(preferences.getFcpFullAccessRequired().ordinal()); + Integer fcpFullAccessRequiredInteger = Numbers.safeParseInteger(request.getHttpRequest().getPartAsStringFailsafe("fcp-full-access-required", 1), preferences.getFcpFullAccessRequired().ordinal()); FullAccessRequired fcpFullAccessRequired = FullAccessRequired.values()[fcpFullAccessRequiredInteger]; preferences.setFcpFullAccessRequired(fcpFullAccessRequired); webInterface.getCore().touchConfiguration(); diff --git a/src/main/java/net/pterodactylus/sone/web/RescuePage.java b/src/main/java/net/pterodactylus/sone/web/RescuePage.java index feeb0bf..6888277 100644 --- a/src/main/java/net/pterodactylus/sone/web/RescuePage.java +++ b/src/main/java/net/pterodactylus/sone/web/RescuePage.java @@ -20,13 +20,11 @@ package net.pterodactylus.sone.web; import net.pterodactylus.sone.core.SoneRescuer; import net.pterodactylus.sone.data.Sone; import net.pterodactylus.sone.web.page.FreenetRequest; +import net.pterodactylus.util.number.Numbers; import net.pterodactylus.util.template.Template; import net.pterodactylus.util.template.TemplateContext; import net.pterodactylus.util.web.Method; -import com.google.common.base.Optional; -import com.google.common.primitives.Longs; - /** * Page that lets the user control the rescue mode for a Sone. * @@ -61,7 +59,7 @@ public class RescuePage extends SoneTemplatePage { SoneRescuer soneRescuer = webInterface.getCore().getSoneRescuer(currentSone); if (request.getMethod() == Method.POST) { if ("true".equals(request.getHttpRequest().getPartAsStringFailsafe("fetch", 4))) { - long edition = Optional.fromNullable(Longs.tryParse(request.getHttpRequest().getPartAsStringFailsafe("edition", 8))).or(-1L); + long edition = Numbers.safeParseLong(request.getHttpRequest().getPartAsStringFailsafe("edition", 8), -1L); if (edition > -1) { soneRescuer.setEdition(edition); } diff --git a/src/main/java/net/pterodactylus/sone/web/SearchPage.java b/src/main/java/net/pterodactylus/sone/web/SearchPage.java index 3e68f51..1bca8cd 100644 --- a/src/main/java/net/pterodactylus/sone/web/SearchPage.java +++ b/src/main/java/net/pterodactylus/sone/web/SearchPage.java @@ -37,13 +37,13 @@ import net.pterodactylus.sone.data.Sone; import net.pterodactylus.sone.web.page.FreenetRequest; import net.pterodactylus.util.collection.Pagination; import net.pterodactylus.util.logging.Logging; +import net.pterodactylus.util.number.Numbers; import net.pterodactylus.util.template.Template; import net.pterodactylus.util.template.TemplateContext; import net.pterodactylus.util.text.StringEscaper; import net.pterodactylus.util.text.TextException; import com.google.common.base.Function; -import com.google.common.base.Optional; import com.google.common.base.Predicate; import com.google.common.cache.CacheBuilder; import com.google.common.cache.CacheLoader; @@ -51,7 +51,6 @@ import com.google.common.cache.LoadingCache; import com.google.common.collect.Collections2; import com.google.common.collect.FluentIterable; import com.google.common.collect.Ordering; -import com.google.common.primitives.Ints; /** * This page lets the user search for posts and replies that contain certain @@ -149,8 +148,8 @@ public class SearchPage extends SoneTemplatePage { List resultPosts = FluentIterable.from(sortedPostHits).transform(new HitMapper()).toList(); /* pagination. */ - Pagination sonePagination = new Pagination(resultSones, webInterface.getCore().getPreferences().getPostsPerPage()).setPage(Optional.fromNullable(Ints.tryParse(request.getHttpRequest().getParam("sonePage"))).or(0)); - Pagination postPagination = new Pagination(resultPosts, webInterface.getCore().getPreferences().getPostsPerPage()).setPage(Optional.fromNullable(Ints.tryParse(request.getHttpRequest().getParam("postPage"))).or(0)); + Pagination sonePagination = new Pagination(resultSones, webInterface.getCore().getPreferences().getPostsPerPage()).setPage(Numbers.safeParseInteger(request.getHttpRequest().getParam("sonePage"), 0)); + Pagination postPagination = new Pagination(resultPosts, webInterface.getCore().getPreferences().getPostsPerPage()).setPage(Numbers.safeParseInteger(request.getHttpRequest().getParam("postPage"), 0)); templateContext.set("sonePagination", sonePagination); templateContext.set("soneHits", sonePagination.getItems()); diff --git a/src/main/java/net/pterodactylus/sone/web/ViewSonePage.java b/src/main/java/net/pterodactylus/sone/web/ViewSonePage.java index 830a394..dec4fe2 100644 --- a/src/main/java/net/pterodactylus/sone/web/ViewSonePage.java +++ b/src/main/java/net/pterodactylus/sone/web/ViewSonePage.java @@ -32,12 +32,10 @@ import net.pterodactylus.sone.data.Sone; import net.pterodactylus.sone.template.SoneAccessor; import net.pterodactylus.sone.web.page.FreenetRequest; import net.pterodactylus.util.collection.Pagination; +import net.pterodactylus.util.number.Numbers; import net.pterodactylus.util.template.Template; import net.pterodactylus.util.template.TemplateContext; -import com.google.common.base.Optional; -import com.google.common.primitives.Ints; - /** * Lets the user browser another Sone. * @@ -91,7 +89,7 @@ public class ViewSonePage extends SoneTemplatePage { List sonePosts = sone.getPosts(); sonePosts.addAll(webInterface.getCore().getDirectedPosts(sone)); Collections.sort(sonePosts, Post.TIME_COMPARATOR); - Pagination postPagination = new Pagination(sonePosts, webInterface.getCore().getPreferences().getPostsPerPage()).setPage(Optional.fromNullable(Ints.tryParse(request.getHttpRequest().getParam("postPage"))).or(0)); + Pagination postPagination = new Pagination(sonePosts, webInterface.getCore().getPreferences().getPostsPerPage()).setPage(Numbers.safeParseInteger(request.getHttpRequest().getParam("postPage"), 0)); templateContext.set("postPagination", postPagination); templateContext.set("posts", postPagination.getItems()); Set replies = sone.getReplies(); @@ -113,7 +111,7 @@ public class ViewSonePage extends SoneTemplatePage { }); - Pagination repliedPostPagination = new Pagination(posts, webInterface.getCore().getPreferences().getPostsPerPage()).setPage(Optional.fromNullable(Ints.tryParse(request.getHttpRequest().getParam("repliedPostPage"))).or(0)); + Pagination repliedPostPagination = new Pagination(posts, webInterface.getCore().getPreferences().getPostsPerPage()).setPage(Numbers.safeParseInteger(request.getHttpRequest().getParam("repliedPostPage"), 0)); templateContext.set("repliedPostPagination", repliedPostPagination); templateContext.set("repliedPosts", repliedPostPagination.getItems()); }