X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;ds=inline;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fweb%2FSearchPage.java;h=2a4b4994a0b31b807a7f3d2f88b6ce754b5acfe2;hb=2e03e9dddbea4b81aacaf1aa316f5c3ccffd4bf9;hp=a604cc67c182960213bb68d66873477059a8afb4;hpb=e5c81f9240fcfd2cc983cf3011a1f1dd11e08456;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 a604cc6..2a4b499 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 @@ -17,6 +17,8 @@ package net.pterodactylus.sone.web; +import static com.google.common.collect.FluentIterable.from; + import java.util.ArrayList; import java.util.Collection; import java.util.Collections; @@ -44,12 +46,12 @@ 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; /** @@ -93,10 +95,8 @@ public class SearchPage extends SoneTemplatePage { // SONETEMPLATEPAGE METHODS // - /** - * {@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(); @@ -129,7 +129,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); @@ -143,8 +143,8 @@ public class SearchPage extends SoneTemplatePage { List> sortedPostHits = Ordering.from(Hit.DESCENDING_COMPARATOR).sortedCopy(postHits); /* extract Sones and posts. */ - List resultSones = FluentIterable.from(sortedSoneHits).transform(new HitMapper()).toList(); - List resultPosts = FluentIterable.from(sortedPostHits).transform(new HitMapper()).toList(); + List resultSones = from(sortedSoneHits).transform(new HitMapper()).toList(); + List resultPosts = from(sortedPostHits).transform(new HitMapper()).toList(); /* pagination. */ Pagination sonePagination = new Pagination(resultSones, webInterface.getCore().getPreferences().getPostsPerPage()).setPage(Numbers.safeParseInteger(request.getHttpRequest().getParam("sonePage"), 0)); @@ -308,7 +308,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; } /** @@ -321,7 +321,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().getDatabase().getPost(postId).isPresent()) ? postId : null; } /** @@ -335,7 +335,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().getDatabase().getPostReply(replyId); + if (!postReply.isPresent()) { + return null; + } + return postReply.get().getPostId(); } /** @@ -348,7 +352,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).isPresent() ? albumId : null; } /** @@ -361,7 +365,7 @@ public class SearchPage extends SoneTemplatePage { */ private String getImageId(String phrase) { String imageId = phrase.startsWith("image://") ? phrase.substring(8) : phrase; - return (webInterface.getCore().getImage(imageId, false) != null) ? imageId : null; + return webInterface.getCore().getImage(imageId).isPresent() ? imageId : null; } /** @@ -415,9 +419,6 @@ public class SearchPage extends SoneTemplatePage { this.complete = complete; } - /** - * {@inheritDoc} - */ @Override public String generateString(Sone sone) { StringBuilder soneString = new StringBuilder(); @@ -451,17 +452,14 @@ public class SearchPage extends SoneTemplatePage { */ private class PostStringGenerator implements StringGenerator { - /** - * {@inheritDoc} - */ @Override 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 : from(post.getReplies()).filter(Reply.FUTURE_REPLY_FILTER)) { postString.append(' ').append(SoneStringGenerator.NAME_GENERATOR.generateString(reply.getSone())); postString.append(' ').append(reply.getText()); } @@ -537,17 +535,11 @@ public class SearchPage extends SoneTemplatePage { // OBJECT METHODS // - /** - * {@inheritDoc} - */ @Override public int hashCode() { return phrase.hashCode() ^ ((optionality == Optionality.FORBIDDEN) ? (0xaaaaaaaa) : ((optionality == Optionality.REQUIRED) ? 0x55555555 : 0)); } - /** - * {@inheritDoc} - */ @Override public boolean equals(Object object) { if (!(object instanceof Phrase)) { @@ -575,7 +567,7 @@ public class SearchPage extends SoneTemplatePage { @Override public boolean apply(Hit hit) { - return hit.getScore() > 0; + return (hit == null) ? false : hit.getScore() > 0; } }; @@ -638,9 +630,6 @@ public class SearchPage extends SoneTemplatePage { */ private static class HitMapper implements Function, T> { - /** - * {@inheritDoc} - */ @Override public T apply(Hit input) { return input.getObject();