X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fweb%2FSearchPage.java;h=508d30b719e687a2b45615f28972860428d89c3c;hb=ec06ae64c86f0b06bb0cf9f8b289e7907e81dffa;hp=3024dfaed0dc71c3088c541d888de5ce52125e7b;hpb=1b078dab3e386026650fe795518db303d710cc00;p=Sone.git diff --git a/src/main/java/net/pterodactylus/sone/web/SearchPage.java b/src/main/java/net/pterodactylus/sone/web/SearchPage.java index 3024dfa..508d30b 100644 --- a/src/main/java/net/pterodactylus/sone/web/SearchPage.java +++ b/src/main/java/net/pterodactylus/sone/web/SearchPage.java @@ -1,5 +1,5 @@ /* - * Sone - SearchPage.java - Copyright © 2010–2012 David Roden + * Sone - SearchPage.java - Copyright © 2010–2013 David Roden * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -36,8 +36,6 @@ import net.pterodactylus.sone.data.Reply; import net.pterodactylus.sone.data.Sone; import net.pterodactylus.sone.web.page.FreenetRequest; import net.pterodactylus.util.collection.Pagination; -import net.pterodactylus.util.collection.mapper.Mapper; -import net.pterodactylus.util.collection.mapper.Mappers; import net.pterodactylus.util.logging.Logging; import net.pterodactylus.util.number.Numbers; import net.pterodactylus.util.template.Template; @@ -45,11 +43,15 @@ 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; import com.google.common.cache.LoadingCache; import com.google.common.collect.Collections2; +import com.google.common.collect.FluentIterable; +import com.google.common.collect.Ordering; /** * This page lets the user search for posts and replies that contain certain @@ -96,6 +98,7 @@ public class SearchPage extends SoneTemplatePage { * {@inheritDoc} */ @Override + @SuppressWarnings("synthetic-access") protected void processTemplate(FreenetRequest request, TemplateContext templateContext) throws RedirectException { super.processTemplate(request, templateContext); String query = request.getHttpRequest().getParam("query").trim(); @@ -128,7 +131,7 @@ public class SearchPage extends SoneTemplatePage { redirectIfNotNull(getImageId(phrase), "imageBrowser.html?image="); } - Set sones = webInterface.getCore().getSones(); + Collection sones = webInterface.getCore().getSones(); Collection> soneHits = getHits(sones, phrases, SoneStringGenerator.COMPLETE_GENERATOR); Collection> postHits = hitCache.getUnchecked(phrases); @@ -138,14 +141,12 @@ public class SearchPage extends SoneTemplatePage { postHits = Collections2.filter(postHits, Hit.POSITIVE_FILTER); /* now sort. */ - List> sortedSoneHits = new ArrayList>(soneHits); - Collections.sort(sortedSoneHits, Hit.DESCENDING_COMPARATOR); - List> sortedPostHits = new ArrayList>(postHits); - Collections.sort(sortedPostHits, Hit.DESCENDING_COMPARATOR); + List> sortedSoneHits = Ordering.from(Hit.DESCENDING_COMPARATOR).sortedCopy(soneHits); + List> sortedPostHits = Ordering.from(Hit.DESCENDING_COMPARATOR).sortedCopy(postHits); /* extract Sones and posts. */ - List resultSones = Mappers.mappedList(sortedSoneHits, new HitMapper()); - List resultPosts = Mappers.mappedList(sortedPostHits, new HitMapper()); + List resultSones = FluentIterable.from(sortedSoneHits).transform(new HitMapper()).toList(); + 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)); @@ -200,7 +201,7 @@ public class SearchPage extends SoneTemplatePage { * @return The parsed phrases */ private static List parseSearchPhrases(String query) { - List parsedPhrases = null; + List parsedPhrases; try { parsedPhrases = StringEscaper.parseLine(query); } catch (TextException te1) { @@ -309,7 +310,7 @@ public class SearchPage extends SoneTemplatePage { */ private String getSoneId(String phrase) { String soneId = phrase.startsWith("sone://") ? phrase.substring(7) : phrase; - return (webInterface.getCore().getSone(soneId, false) != null) ? soneId : null; + return (webInterface.getCore().getSone(soneId).isPresent()) ? soneId : null; } /** @@ -322,7 +323,7 @@ public class SearchPage extends SoneTemplatePage { */ private String getPostId(String phrase) { String postId = phrase.startsWith("post://") ? phrase.substring(7) : phrase; - return (webInterface.getCore().getPost(postId, false) != null) ? postId : null; + return (webInterface.getCore().getPost(postId).isPresent()) ? postId : null; } /** @@ -336,7 +337,11 @@ public class SearchPage extends SoneTemplatePage { */ private String getReplyPostId(String phrase) { String replyId = phrase.startsWith("reply://") ? phrase.substring(8) : phrase; - return (webInterface.getCore().getPostReply(replyId, false) != null) ? webInterface.getCore().getPostReply(replyId, false).getPost().getId() : null; + Optional postReply = webInterface.getCore().getPostReply(replyId); + if (!postReply.isPresent()) { + return null; + } + return postReply.get().getPostId(); } /** @@ -349,7 +354,7 @@ public class SearchPage extends SoneTemplatePage { */ private String getAlbumId(String phrase) { String albumId = phrase.startsWith("album://") ? phrase.substring(8) : phrase; - return (webInterface.getCore().getAlbum(albumId, false) != null) ? albumId : null; + return (webInterface.getCore().getAlbum(albumId) != null) ? albumId : null; } /** @@ -459,10 +464,10 @@ public class SearchPage extends SoneTemplatePage { public String generateString(Post post) { StringBuilder postString = new StringBuilder(); postString.append(post.getText()); - if (post.getRecipient() != null) { - postString.append(' ').append(SoneStringGenerator.NAME_GENERATOR.generateString(post.getRecipient())); + if (post.getRecipient().isPresent()) { + postString.append(' ').append(SoneStringGenerator.NAME_GENERATOR.generateString(post.getRecipient().get())); } - for (PostReply reply : Collections2.filter(webInterface.getCore().getReplies(post), Reply.FUTURE_REPLY_FILTER)) { + for (PostReply reply : Collections2.filter(webInterface.getCore().getReplies(post.getId()), Reply.FUTURE_REPLY_FILTER)) { postString.append(' ').append(SoneStringGenerator.NAME_GENERATOR.generateString(reply.getSone())); postString.append(' ').append(reply.getText()); } @@ -576,7 +581,7 @@ public class SearchPage extends SoneTemplatePage { @Override public boolean apply(Hit hit) { - return hit.getScore() > 0; + return (hit != null) && (hit.getScore() > 0); } }; @@ -637,13 +642,13 @@ public class SearchPage extends SoneTemplatePage { * The type of the object to extract * @author David ‘Bombe’ Roden */ - public static class HitMapper implements Mapper, T> { + private static class HitMapper implements Function, T> { /** * {@inheritDoc} */ @Override - public T map(Hit input) { + public T apply(Hit input) { return input.getObject(); }