From: David ‘Bombe’ Roden Date: Thu, 17 Jan 2013 16:00:12 +0000 (+0100) Subject: Replace utils’ Numbers by Guava’s Optional and Ints/Longs.tryParse. X-Git-Tag: 0.8.5^2~3^2~66 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=d5efb086bee8f103cbe90c7a953ffbb7ff27b689;p=Sone.git Replace utils’ Numbers by Guava’s Optional and Ints/Longs.tryParse. --- diff --git a/src/main/java/net/pterodactylus/sone/core/Core.java b/src/main/java/net/pterodactylus/sone/core/Core.java index 9713ddc..7075880 100644 --- a/src/main/java/net/pterodactylus/sone/core/Core.java +++ b/src/main/java/net/pterodactylus/sone/core/Core.java @@ -79,16 +79,17 @@ 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; @@ -823,7 +824,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(Numbers.safeParseLong(ownIdentity.getProperty("Sone.LatestEdition"), (long) 0)); + sone.setLatestEdition(Optional.fromNullable(Longs.tryParse(ownIdentity.getProperty("Sone.LatestEdition"))).or(0L)); sone.setClient(new Client("Sone", SonePlugin.VERSION.toString())); sone.setKnown(true); /* TODO - load posts ’n stuff */ @@ -878,7 +879,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(Numbers.safeParseLong(identity.getProperty("Sone.LatestEdition"), (long) 0)); + sone.setLatestEdition(Optional.fromNullable(Longs.tryParse(identity.getProperty("Sone.LatestEdition"))).or(0L)); if (newSone) { synchronized (knownSones) { newSone = !knownSones.contains(sone.getId()); @@ -2393,7 +2394,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider public void run() { Sone sone = getRemoteSone(identity.getId(), false); sone.setIdentity(identity); - sone.setLatestEdition(Numbers.safeParseLong(identity.getProperty("Sone.LatestEdition"), sone.getLatestEdition())); + sone.setLatestEdition(Optional.fromNullable(Longs.tryParse(identity.getProperty("Sone.LatestEdition"))).or(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 70a193a..9bc14a8 100644 --- a/src/main/java/net/pterodactylus/sone/core/SoneDownloader.java +++ b/src/main/java/net/pterodactylus/sone/core/SoneDownloader.java @@ -37,13 +37,16 @@ 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; @@ -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. */ @@ -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; 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 07628e3..d3e8747 100644 --- a/src/main/java/net/pterodactylus/sone/freenet/wot/WebOfTrustConnector.java +++ b/src/main/java/net/pterodactylus/sone/freenet/wot/WebOfTrustConnector.java @@ -29,10 +29,11 @@ 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; @@ -152,9 +153,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 = 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); + 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); 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 7e3723c..eea9121 100644 --- a/src/main/java/net/pterodactylus/sone/template/ImageLinkFilter.java +++ b/src/main/java/net/pterodactylus/sone/template/ImageLinkFilter.java @@ -23,7 +23,6 @@ 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; @@ -31,6 +30,7 @@ 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 = Numbers.safeParseInteger(parameters.get("max-width"), Integer.MAX_VALUE); - int maxHeight = Numbers.safeParseInteger(parameters.get("max-height"), Integer.MAX_VALUE); + 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); 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 d2f2619..db41830 100644 --- a/src/main/java/net/pterodactylus/sone/template/ParserFilter.java +++ b/src/main/java/net/pterodactylus/sone/template/ParserFilter.java @@ -38,13 +38,15 @@ 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}. * @@ -90,8 +92,8 @@ public class ParserFilter implements Filter { @Override public Object format(TemplateContext templateContext, Object data, Map parameters) { String text = String.valueOf(data); - 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)); + 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); 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 10b6dc3..dda4cac 100644 --- a/src/main/java/net/pterodactylus/sone/web/BookmarksPage.java +++ b/src/main/java/net/pterodactylus/sone/web/BookmarksPage.java @@ -26,12 +26,13 @@ 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. @@ -72,7 +73,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(Numbers.safeParseInteger(request.getHttpRequest().getParam("page"), 0)); + Pagination pagination = new Pagination(sortedPosts, webInterface.getCore().getPreferences().getPostsPerPage()).setPage(Optional.fromNullable(Ints.tryParse(request.getHttpRequest().getParam("page"))).or(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 c79a1e9..62957c2 100644 --- a/src/main/java/net/pterodactylus/sone/web/EditProfilePage.java +++ b/src/main/java/net/pterodactylus/sone/web/EditProfilePage.java @@ -23,10 +23,12 @@ 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; /** @@ -74,9 +76,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 = 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()); + 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()); 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 4a9548e..ea039ca 100644 --- a/src/main/java/net/pterodactylus/sone/web/ImageBrowserPage.java +++ b/src/main/java/net/pterodactylus/sone/web/ImageBrowserPage.java @@ -27,10 +27,12 @@ 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. * @@ -90,7 +92,7 @@ public class ImageBrowserPage extends SoneTemplatePage { albums.addAll(sone.getAllAlbums()); } Collections.sort(albums, Album.TITLE_COMPARATOR); - Pagination albumPagination = new Pagination(albums, 12).setPage(Numbers.safeParseInteger(request.getHttpRequest().getParam("page"), 0)); + Pagination albumPagination = new Pagination(albums, 12).setPage(Optional.fromNullable(Ints.tryParse(request.getHttpRequest().getParam("page"))).or(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 b4307e3..ff7395e 100644 --- a/src/main/java/net/pterodactylus/sone/web/IndexPage.java +++ b/src/main/java/net/pterodactylus/sone/web/IndexPage.java @@ -27,12 +27,13 @@ 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 @@ -88,7 +89,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(Numbers.safeParseInteger(request.getHttpRequest().getParam("page"), 0)); + Pagination pagination = new Pagination(sortedPosts, webInterface.getCore().getPreferences().getPostsPerPage()).setPage(Optional.fromNullable(Ints.tryParse(request.getHttpRequest().getParam("page"))).or(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 dd1b1fc..65254e3 100644 --- a/src/main/java/net/pterodactylus/sone/web/KnownSonesPage.java +++ b/src/main/java/net/pterodactylus/sone/web/KnownSonesPage.java @@ -25,14 +25,15 @@ 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. @@ -140,7 +141,7 @@ public class KnownSonesPage extends SoneTemplatePage { Collections.sort(sortedSones, Sone.NICE_NAME_COMPARATOR); } } - Pagination sonePagination = new Pagination(sortedSones, 25).setPage(Numbers.safeParseInteger(request.getHttpRequest().getParam("page"), 0)); + Pagination sonePagination = new Pagination(sortedSones, 25).setPage(Optional.fromNullable(Ints.tryParse(request.getHttpRequest().getParam("page"))).or(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 2d4ec55..af7c1ec 100644 --- a/src/main/java/net/pterodactylus/sone/web/NewPage.java +++ b/src/main/java/net/pterodactylus/sone/web/NewPage.java @@ -28,10 +28,12 @@ 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)} @@ -75,7 +77,7 @@ public class NewPage extends SoneTemplatePage { Collections.sort(sortedPosts, Post.TIME_COMPARATOR); /* paginate them. */ - Pagination pagination = new Pagination(sortedPosts, webInterface.getCore().getPreferences().getPostsPerPage()).setPage(Numbers.safeParseInteger(request.getHttpRequest().getParam("page"), 0)); + Pagination pagination = new Pagination(sortedPosts, webInterface.getCore().getPreferences().getPostsPerPage()).setPage(Optional.fromNullable(Ints.tryParse(request.getHttpRequest().getParam("page"))).or(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 094a9cf..41398c5 100644 --- a/src/main/java/net/pterodactylus/sone/web/OptionsPage.java +++ b/src/main/java/net/pterodactylus/sone/web/OptionsPage.java @@ -25,11 +25,13 @@ 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. * @@ -78,31 +80,31 @@ public class OptionsPage extends SoneTemplatePage { currentSone.getOptions(). getEnumOption("ShowCustomAvatars").set(ShowCustomAvatars.valueOf(showCustomAvatars)); webInterface.getCore().touchConfiguration(); } - Integer insertionDelay = Numbers.safeParseInteger(request.getHttpRequest().getPartAsStringFailsafe("insertion-delay", 16)); + Integer insertionDelay = Ints.tryParse(request.getHttpRequest().getPartAsStringFailsafe("insertion-delay", 16)); if (!preferences.validateInsertionDelay(insertionDelay)) { fieldErrors.add("insertion-delay"); } else { preferences.setInsertionDelay(insertionDelay); } - Integer postsPerPage = Numbers.safeParseInteger(request.getHttpRequest().getPartAsStringFailsafe("posts-per-page", 4), null); + Integer postsPerPage = Ints.tryParse(request.getHttpRequest().getPartAsStringFailsafe("posts-per-page", 4)); if (!preferences.validatePostsPerPage(postsPerPage)) { fieldErrors.add("posts-per-page"); } else { preferences.setPostsPerPage(postsPerPage); } - Integer imagesPerPage = Numbers.safeParseInteger(request.getHttpRequest().getPartAsStringFailsafe("images-per-page", 4), null); + Integer imagesPerPage = Ints.tryParse(request.getHttpRequest().getPartAsStringFailsafe("images-per-page", 4)); if (!preferences.validateImagesPerPage(imagesPerPage)) { fieldErrors.add("images-per-page"); } else { preferences.setImagesPerPage(imagesPerPage); } - Integer charactersPerPost = Numbers.safeParseInteger(request.getHttpRequest().getPartAsStringFailsafe("characters-per-post", 10), null); + Integer charactersPerPost = Ints.tryParse(request.getHttpRequest().getPartAsStringFailsafe("characters-per-post", 10)); if (!preferences.validateCharactersPerPost(charactersPerPost)) { fieldErrors.add("characters-per-post"); } else { preferences.setCharactersPerPost(charactersPerPost); } - Integer postCutOffLength = Numbers.safeParseInteger(request.getHttpRequest().getPartAsStringFailsafe("post-cut-off-length", 10), null); + Integer postCutOffLength = Ints.tryParse(request.getHttpRequest().getPartAsStringFailsafe("post-cut-off-length", 10)); if (!preferences.validatePostCutOffLength(postCutOffLength)) { fieldErrors.add("post-cut-off-length"); } else { @@ -110,13 +112,13 @@ public class OptionsPage extends SoneTemplatePage { } boolean requireFullAccess = request.getHttpRequest().isPartSet("require-full-access"); preferences.setRequireFullAccess(requireFullAccess); - Integer positiveTrust = Numbers.safeParseInteger(request.getHttpRequest().getPartAsStringFailsafe("positive-trust", 3)); + Integer positiveTrust = Ints.tryParse(request.getHttpRequest().getPartAsStringFailsafe("positive-trust", 3)); if (!preferences.validatePositiveTrust(positiveTrust)) { fieldErrors.add("positive-trust"); } else { preferences.setPositiveTrust(positiveTrust); } - Integer negativeTrust = Numbers.safeParseInteger(request.getHttpRequest().getPartAsStringFailsafe("negative-trust", 4)); + Integer negativeTrust = Ints.tryParse(request.getHttpRequest().getPartAsStringFailsafe("negative-trust", 4)); if (!preferences.validateNegativeTrust(negativeTrust)) { fieldErrors.add("negative-trust"); } else { @@ -129,7 +131,7 @@ public class OptionsPage extends SoneTemplatePage { preferences.setTrustComment(trustComment); boolean fcpInterfaceActive = request.getHttpRequest().isPartSet("fcp-interface-active"); preferences.setFcpInterfaceActive(fcpInterfaceActive); - Integer fcpFullAccessRequiredInteger = Numbers.safeParseInteger(request.getHttpRequest().getPartAsStringFailsafe("fcp-full-access-required", 1), preferences.getFcpFullAccessRequired().ordinal()); + Integer fcpFullAccessRequiredInteger = Optional.fromNullable(Ints.tryParse(request.getHttpRequest().getPartAsStringFailsafe("fcp-full-access-required", 1))).or(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 6888277..feeb0bf 100644 --- a/src/main/java/net/pterodactylus/sone/web/RescuePage.java +++ b/src/main/java/net/pterodactylus/sone/web/RescuePage.java @@ -20,11 +20,13 @@ 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. * @@ -59,7 +61,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 = Numbers.safeParseLong(request.getHttpRequest().getPartAsStringFailsafe("edition", 8), -1L); + long edition = Optional.fromNullable(Longs.tryParse(request.getHttpRequest().getPartAsStringFailsafe("edition", 8))).or(-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 4dbd7dc..ffde2a4 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,6 +51,7 @@ 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 @@ -147,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(Numbers.safeParseInteger(request.getHttpRequest().getParam("sonePage"), 0)); - Pagination postPagination = new Pagination(resultPosts, webInterface.getCore().getPreferences().getPostsPerPage()).setPage(Numbers.safeParseInteger(request.getHttpRequest().getParam("postPage"), 0)); + 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)); 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 dec4fe2..830a394 100644 --- a/src/main/java/net/pterodactylus/sone/web/ViewSonePage.java +++ b/src/main/java/net/pterodactylus/sone/web/ViewSonePage.java @@ -32,10 +32,12 @@ 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. * @@ -89,7 +91,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(Numbers.safeParseInteger(request.getHttpRequest().getParam("postPage"), 0)); + Pagination postPagination = new Pagination(sonePosts, webInterface.getCore().getPreferences().getPostsPerPage()).setPage(Optional.fromNullable(Ints.tryParse(request.getHttpRequest().getParam("postPage"))).or(0)); templateContext.set("postPagination", postPagination); templateContext.set("posts", postPagination.getItems()); Set replies = sone.getReplies(); @@ -111,7 +113,7 @@ public class ViewSonePage extends SoneTemplatePage { }); - Pagination repliedPostPagination = new Pagination(posts, webInterface.getCore().getPreferences().getPostsPerPage()).setPage(Numbers.safeParseInteger(request.getHttpRequest().getParam("repliedPostPage"), 0)); + Pagination repliedPostPagination = new Pagination(posts, webInterface.getCore().getPreferences().getPostsPerPage()).setPage(Optional.fromNullable(Ints.tryParse(request.getHttpRequest().getParam("repliedPostPage"))).or(0)); templateContext.set("repliedPostPagination", repliedPostPagination); templateContext.set("repliedPosts", repliedPostPagination.getItems()); }