/*
- * 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
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
* {@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();
redirectIfNotNull(getImageId(phrase), "imageBrowser.html?image=");
}
- Set<Sone> sones = webInterface.getCore().getSones();
+ Collection<Sone> sones = webInterface.getCore().getSones();
Collection<Hit<Sone>> soneHits = getHits(sones, phrases, SoneStringGenerator.COMPLETE_GENERATOR);
Collection<Hit<Post>> postHits = hitCache.getUnchecked(phrases);
postHits = Collections2.filter(postHits, Hit.POSITIVE_FILTER);
/* now sort. */
- List<Hit<Sone>> sortedSoneHits = new ArrayList<Hit<Sone>>(soneHits);
- Collections.sort(sortedSoneHits, Hit.DESCENDING_COMPARATOR);
- List<Hit<Post>> sortedPostHits = new ArrayList<Hit<Post>>(postHits);
- Collections.sort(sortedPostHits, Hit.DESCENDING_COMPARATOR);
+ List<Hit<Sone>> sortedSoneHits = Ordering.from(Hit.DESCENDING_COMPARATOR).sortedCopy(soneHits);
+ List<Hit<Post>> sortedPostHits = Ordering.from(Hit.DESCENDING_COMPARATOR).sortedCopy(postHits);
/* extract Sones and posts. */
List<Sone> resultSones = FluentIterable.from(sortedSoneHits).transform(new HitMapper<Sone>()).toList();
* @return The parsed phrases
*/
private static List<Phrase> parseSearchPhrases(String query) {
- List<String> parsedPhrases = null;
+ List<String> parsedPhrases;
try {
parsedPhrases = StringEscaper.parseLine(query);
} catch (TextException te1) {
*/
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;
}
/**
*/
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;
}
/**
*/
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> postReply = webInterface.getCore().getPostReply(replyId);
+ if (!postReply.isPresent()) {
+ return null;
+ }
+ return postReply.get().getPostId();
}
/**
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());
}
@Override
public boolean apply(Hit<?> hit) {
- return hit.getScore() > 0;
+ return (hit != null) && (hit.getScore() > 0);
}
};
* The type of the object to extract
* @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
*/
- public static class HitMapper<T> implements Function<Hit<T>, T> {
+ private static class HitMapper<T> implements Function<Hit<T>, T> {
/**
* {@inheritDoc}