From: David ‘Bombe’ Roden Date: Fri, 10 Oct 2014 20:24:01 +0000 (+0200) Subject: Don’t use number parsing from the utils package anymore. X-Git-Tag: 0.9-rc1^2~3^2~69 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=224be7a31fee673fa438cb02c4bb2105d01a52cc;p=Sone.git Don’t use number parsing from the utils package anymore. --- diff --git a/src/main/java/net/pterodactylus/sone/core/SoneParser.java b/src/main/java/net/pterodactylus/sone/core/SoneParser.java index 213be03..5851b18 100644 --- a/src/main/java/net/pterodactylus/sone/core/SoneParser.java +++ b/src/main/java/net/pterodactylus/sone/core/SoneParser.java @@ -1,6 +1,8 @@ package net.pterodactylus.sone.core; import static java.util.logging.Logger.getLogger; +import static net.pterodactylus.sone.utils.NumberParsers.parseInt; +import static net.pterodactylus.sone.utils.NumberParsers.parseLong; import java.io.InputStream; import java.util.ArrayList; @@ -22,7 +24,6 @@ import net.pterodactylus.sone.data.Sone; import net.pterodactylus.sone.database.PostBuilder; import net.pterodactylus.sone.database.PostReplyBuilder; import net.pterodactylus.sone.database.SoneBuilder; -import net.pterodactylus.util.number.Numbers; import net.pterodactylus.util.xml.SimpleXML; import net.pterodactylus.util.xml.XML; @@ -75,7 +76,7 @@ public class SoneParser { Integer protocolVersion = null; String soneProtocolVersion = soneXml.getValue("protocol-version", null); if (soneProtocolVersion != null) { - protocolVersion = Numbers.safeParseInteger(soneProtocolVersion); + protocolVersion = parseInt(soneProtocolVersion, null); } if (protocolVersion == null) { logger.log(Level.INFO, "No protocol version found, assuming 0."); @@ -129,9 +130,9 @@ public class SoneParser { 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 = parseInt(profileXml.getValue("birth-day", ""), null); + Integer profileBirthMonth = parseInt(profileXml.getValue("birth-month", ""), null); + Integer profileBirthYear = parseInt(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. */ @@ -295,9 +296,9 @@ public class SoneParser { 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 = parseLong(imageCreationTimeString, 0L); + int imageWidth = parseInt(imageWidthString, 0); + int imageHeight = parseInt(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 b0b6d42..3b30a4e 100644 --- a/src/main/java/net/pterodactylus/sone/freenet/wot/WebOfTrustConnector.java +++ b/src/main/java/net/pterodactylus/sone/freenet/wot/WebOfTrustConnector.java @@ -18,6 +18,7 @@ package net.pterodactylus.sone.freenet.wot; import static java.util.logging.Logger.getLogger; +import static net.pterodactylus.sone.utils.NumberParsers.parseInt; import java.util.HashMap; import java.util.HashSet; @@ -30,7 +31,6 @@ import java.util.logging.Logger; 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.number.Numbers; import com.google.common.base.Optional; import com.google.common.collect.MapMaker; @@ -156,9 +156,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 = parseInt(fields.get("Trust" + identityCounter), null); + int score = parseInt(fields.get("Score" + identityCounter), 0); + int rank = parseInt(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 7e3723c..15881db 100644 --- a/src/main/java/net/pterodactylus/sone/template/ImageLinkFilter.java +++ b/src/main/java/net/pterodactylus/sone/template/ImageLinkFilter.java @@ -17,13 +17,16 @@ package net.pterodactylus.sone.template; +import static java.lang.Integer.MAX_VALUE; +import static java.lang.String.valueOf; +import static net.pterodactylus.sone.utils.NumberParsers.parseInt; + import java.io.StringReader; import java.io.StringWriter; 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; @@ -77,8 +80,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 = parseInt(valueOf(parameters.get("max-width")), MAX_VALUE); + int maxHeight = parseInt(valueOf(parameters.get("max-height")), 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 bd6903b..f402a41 100644 --- a/src/main/java/net/pterodactylus/sone/template/ParserFilter.java +++ b/src/main/java/net/pterodactylus/sone/template/ParserFilter.java @@ -17,6 +17,9 @@ package net.pterodactylus.sone.template; +import static java.lang.String.valueOf; +import static net.pterodactylus.sone.utils.NumberParsers.parseInt; + import java.io.IOException; import java.io.StringReader; import java.io.StringWriter; @@ -38,7 +41,6 @@ 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; @@ -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 = parseInt(valueOf(parameters.get("length")), -1); + int cutOffLength = parseInt(valueOf(parameters.get("cut-off-length")), length); Object sone = parameters.get("sone"); if (sone instanceof String) { sone = core.getSone((String) sone).orNull(); diff --git a/src/main/java/net/pterodactylus/sone/utils/NumberParsers.java b/src/main/java/net/pterodactylus/sone/utils/NumberParsers.java new file mode 100644 index 0000000..9e8afb4 --- /dev/null +++ b/src/main/java/net/pterodactylus/sone/utils/NumberParsers.java @@ -0,0 +1,36 @@ +package net.pterodactylus.sone.utils; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; + +import com.google.common.primitives.Ints; +import com.google.common.primitives.Longs; + +/** + * Parses numbers from strings. + * + * @author David ‘Bombe’ Roden + */ +public class NumberParsers { + + @Nonnull + public static Integer parseInt(@Nullable String text, + @Nullable Integer defaultValue) { + if (text == null) { + return defaultValue; + } + Integer value = Ints.tryParse(text); + return (value == null) ? defaultValue : value; + } + + @Nonnull + public static Long parseLong(@Nullable String text, + @Nullable Long defaultValue) { + if (text == null) { + return defaultValue; + } + Long value = Longs.tryParse(text); + return (value == null) ? defaultValue : value; + } + +} diff --git a/src/main/java/net/pterodactylus/sone/web/BookmarksPage.java b/src/main/java/net/pterodactylus/sone/web/BookmarksPage.java index 10b6dc3..373ff18 100644 --- a/src/main/java/net/pterodactylus/sone/web/BookmarksPage.java +++ b/src/main/java/net/pterodactylus/sone/web/BookmarksPage.java @@ -17,6 +17,8 @@ package net.pterodactylus.sone.web; +import static net.pterodactylus.sone.utils.NumberParsers.parseInt; + import java.util.ArrayList; import java.util.Collection; import java.util.Collections; @@ -26,7 +28,6 @@ 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; @@ -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(parseInt(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 d0ddb26..38da1d4 100644 --- a/src/main/java/net/pterodactylus/sone/web/EditProfilePage.java +++ b/src/main/java/net/pterodactylus/sone/web/EditProfilePage.java @@ -18,6 +18,7 @@ package net.pterodactylus.sone.web; import static net.pterodactylus.sone.text.TextFilter.filter; +import static net.pterodactylus.sone.utils.NumberParsers.parseInt; import java.util.List; @@ -26,7 +27,6 @@ import net.pterodactylus.sone.data.Profile.Field; import net.pterodactylus.sone.data.Sone; import net.pterodactylus.sone.text.TextFilter; 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; @@ -77,9 +77,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 = parseInt(request.getHttpRequest().getPartAsStringFailsafe("birth-day", 256).trim(), null); + birthMonth = parseInt(request.getHttpRequest().getPartAsStringFailsafe("birth-month", 256).trim(), null); + birthYear = parseInt(request.getHttpRequest().getPartAsStringFailsafe("birth-year", 256).trim(), null); 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 0dbe4e7..60b22d5 100644 --- a/src/main/java/net/pterodactylus/sone/web/ImageBrowserPage.java +++ b/src/main/java/net/pterodactylus/sone/web/ImageBrowserPage.java @@ -21,6 +21,7 @@ import static com.google.common.collect.FluentIterable.from; import static net.pterodactylus.sone.data.Album.FLATTENER; import static net.pterodactylus.sone.data.Album.NOT_EMPTY; import static net.pterodactylus.sone.data.Album.TITLE_COMPARATOR; +import static net.pterodactylus.sone.utils.NumberParsers.parseInt; import java.net.URI; import java.util.ArrayList; @@ -34,7 +35,6 @@ 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; @@ -97,7 +97,7 @@ public class ImageBrowserPage extends SoneTemplatePage { albums.addAll(from(sone.getRootAlbum().getAlbums()).transformAndConcat(FLATTENER).filter(NOT_EMPTY).toList()); } Collections.sort(albums, TITLE_COMPARATOR); - Pagination albumPagination = new Pagination(albums, 12).setPage(Numbers.safeParseInteger(request.getHttpRequest().getParam("page"), 0)); + Pagination albumPagination = new Pagination(albums, 12).setPage(parseInt(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 e1a52b9..e242f52 100644 --- a/src/main/java/net/pterodactylus/sone/web/IndexPage.java +++ b/src/main/java/net/pterodactylus/sone/web/IndexPage.java @@ -17,6 +17,8 @@ package net.pterodactylus.sone.web; +import static net.pterodactylus.sone.utils.NumberParsers.parseInt; + import java.util.ArrayList; import java.util.Collection; import java.util.Collections; @@ -27,7 +29,6 @@ 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; @@ -90,7 +91,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(parseInt(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 beda638..d2e7abe 100644 --- a/src/main/java/net/pterodactylus/sone/web/KnownSonesPage.java +++ b/src/main/java/net/pterodactylus/sone/web/KnownSonesPage.java @@ -17,6 +17,8 @@ package net.pterodactylus.sone.web; +import static net.pterodactylus.sone.utils.NumberParsers.parseInt; + import java.util.ArrayList; import java.util.Collection; import java.util.Collections; @@ -25,7 +27,6 @@ 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; @@ -143,7 +144,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(parseInt(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 71200d8..5977349 100644 --- a/src/main/java/net/pterodactylus/sone/web/NewPage.java +++ b/src/main/java/net/pterodactylus/sone/web/NewPage.java @@ -17,6 +17,8 @@ package net.pterodactylus.sone.web; +import static net.pterodactylus.sone.utils.NumberParsers.parseInt; + import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; @@ -30,7 +32,6 @@ 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; @@ -77,7 +78,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(parseInt(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 5001a73..c38f97b 100644 --- a/src/main/java/net/pterodactylus/sone/web/OptionsPage.java +++ b/src/main/java/net/pterodactylus/sone/web/OptionsPage.java @@ -17,6 +17,8 @@ package net.pterodactylus.sone.web; +import static net.pterodactylus.sone.utils.NumberParsers.parseInt; + import java.util.ArrayList; import java.util.List; @@ -25,7 +27,6 @@ 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; @@ -78,31 +79,31 @@ public class OptionsPage extends SoneTemplatePage { currentSone.getOptions().setShowCustomAvatars(ShowCustomAvatars.valueOf(showCustomAvatars)); webInterface.getCore().touchConfiguration(); } - Integer insertionDelay = Numbers.safeParseInteger(request.getHttpRequest().getPartAsStringFailsafe("insertion-delay", 16)); + Integer insertionDelay = parseInt(request.getHttpRequest().getPartAsStringFailsafe("insertion-delay", 16), null); 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 = parseInt(request.getHttpRequest().getPartAsStringFailsafe("posts-per-page", 4), null); 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 = parseInt(request.getHttpRequest().getPartAsStringFailsafe("images-per-page", 4), null); 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 = parseInt(request.getHttpRequest().getPartAsStringFailsafe("characters-per-post", 10), null); 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 = parseInt(request.getHttpRequest().getPartAsStringFailsafe("post-cut-off-length", 10), null); if (!preferences.validatePostCutOffLength(postCutOffLength)) { fieldErrors.add("post-cut-off-length"); } else { @@ -110,13 +111,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 = parseInt(request.getHttpRequest().getPartAsStringFailsafe("positive-trust", 3), null); if (!preferences.validatePositiveTrust(positiveTrust)) { fieldErrors.add("positive-trust"); } else { preferences.setPositiveTrust(positiveTrust); } - Integer negativeTrust = Numbers.safeParseInteger(request.getHttpRequest().getPartAsStringFailsafe("negative-trust", 4)); + Integer negativeTrust = parseInt(request.getHttpRequest().getPartAsStringFailsafe("negative-trust", 4), null); if (!preferences.validateNegativeTrust(negativeTrust)) { fieldErrors.add("negative-trust"); } else { @@ -129,7 +130,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 = parseInt(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 6888277..8c325d2 100644 --- a/src/main/java/net/pterodactylus/sone/web/RescuePage.java +++ b/src/main/java/net/pterodactylus/sone/web/RescuePage.java @@ -17,10 +17,11 @@ package net.pterodactylus.sone.web; +import static net.pterodactylus.sone.utils.NumberParsers.parseLong; + 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; @@ -59,7 +60,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 = parseLong(request.getHttpRequest().getPartAsStringFailsafe("edition", 8), -1L); if (edition > -1) { soneRescuer.setEdition(edition); } diff --git a/src/main/java/net/pterodactylus/sone/web/ViewSonePage.java b/src/main/java/net/pterodactylus/sone/web/ViewSonePage.java index a94b4af..74461e3 100644 --- a/src/main/java/net/pterodactylus/sone/web/ViewSonePage.java +++ b/src/main/java/net/pterodactylus/sone/web/ViewSonePage.java @@ -17,6 +17,8 @@ package net.pterodactylus.sone.web; +import static net.pterodactylus.sone.utils.NumberParsers.parseInt; + import java.net.URI; import java.util.ArrayList; import java.util.Collections; @@ -32,7 +34,6 @@ 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; @@ -91,7 +92,7 @@ public class ViewSonePage extends SoneTemplatePage { List sonePosts = sone.get().getPosts(); sonePosts.addAll(webInterface.getCore().getDirectedPosts(sone.get().getId())); 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(parseInt(request.getHttpRequest().getParam("postPage"), 0)); templateContext.set("postPagination", postPagination); templateContext.set("posts", postPagination.getItems()); Set replies = sone.get().getReplies(); @@ -113,7 +114,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(parseInt(request.getHttpRequest().getParam("repliedPostPage"), 0)); templateContext.set("repliedPostPagination", repliedPostPagination); templateContext.set("repliedPosts", repliedPostPagination.getItems()); } diff --git a/src/test/java/net/pterodactylus/sone/utils/NumberParsersTest.java b/src/test/java/net/pterodactylus/sone/utils/NumberParsersTest.java new file mode 100644 index 0000000..00c2263 --- /dev/null +++ b/src/test/java/net/pterodactylus/sone/utils/NumberParsersTest.java @@ -0,0 +1,84 @@ +package net.pterodactylus.sone.utils; + +import static net.pterodactylus.sone.utils.NumberParsers.parseInt; +import static net.pterodactylus.sone.utils.NumberParsers.parseLong; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.nullValue; + +import org.junit.Test; + +/** + * Unit test for {@link NumberParsers}. + * + * @author David ‘Bombe’ Roden + */ +public class NumberParsersTest { + + @Test + // yes, this test is for coverage only. + public void constructorCanBeCalled() { + new NumberParsers(); + } + + @Test + public void nullIsParsedToDefaultInt() { + assertThat(parseInt(null, 17), is(17)); + } + + @Test + public void notANumberIsParsedToDefaultInt() { + assertThat(parseInt("not a number", 18), is(18)); + } + + @Test + public void intIsCorrectlyParsed() { + assertThat(parseInt("19", 0), is(19)); + } + + @Test + public void valueTooLargeForIntIsParsedToDefault() { + assertThat(parseInt("2147483648", 20), is(20)); + } + + @Test + public void valueTooSmallForIntIsParsedToDefault() { + assertThat(parseInt("-2147483649", 20), is(20)); + } + + @Test + public void nullCanBeDefaultIntValue() { + assertThat(parseInt("not a number", null), nullValue()); + } + + @Test + public void nullIsParsedToDefaultLong() { + assertThat(parseLong(null, 17L), is(17L)); + } + + @Test + public void notANumberIsParsedToDefaultLong() { + assertThat(parseLong("not a number", 18L), is(18L)); + } + + @Test + public void LongIsCorrectlyParsed() { + assertThat(parseLong("19", 0L), is(19L)); + } + + @Test + public void valueTooLargeForLongIsParsedToDefault() { + assertThat(parseLong("9223372036854775808", 20L), is(20L)); + } + + @Test + public void valueTooSmallForLongIsParsedToDefault() { + assertThat(parseLong("-9223372036854775809", 20L), is(20L)); + } + + @Test + public void nullCanBeDefaultLongValue() { + assertThat(parseLong("not a number", null), nullValue()); + } + +}