X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fcore%2FCore.java;h=a0f0a5998ba44f46147e49066e644158aef46a6d;hb=850ac7a9ce6d9051d8f85919a1cd5a4beb57950f;hp=3164484a09369e2b795472dfdb86c0f4c4bb8ef9;hpb=48d8d875299dcbcaf24912c58960b7a1c070d22b;p=Sone.git diff --git a/src/main/java/net/pterodactylus/sone/core/Core.java b/src/main/java/net/pterodactylus/sone/core/Core.java index 3164484..a0f0a59 100644 --- a/src/main/java/net/pterodactylus/sone/core/Core.java +++ b/src/main/java/net/pterodactylus/sone/core/Core.java @@ -18,9 +18,12 @@ package net.pterodactylus.sone.core; import java.net.MalformedURLException; +import java.util.ArrayList; import java.util.Collections; +import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; +import java.util.List; import java.util.Map; import java.util.Set; import java.util.UUID; @@ -106,7 +109,8 @@ public class Core extends AbstractService { */ public Core freenetInterface(FreenetInterface freenetInterface) { this.freenetInterface = freenetInterface; - soneDownloader = new SoneDownloader(freenetInterface); + soneDownloader = new SoneDownloader(this, freenetInterface); + soneDownloader.start(); return this; } @@ -135,6 +139,34 @@ public class Core extends AbstractService { return soneCache.get(soneId); } + /** + * Creates a new post. + * + * @param sone + * The sone that creates the post + * @param text + * The text of the post + * @return The created post + */ + public Post createPost(Sone sone, String text) { + return createPost(sone, System.currentTimeMillis(), text); + } + + /** + * Creates a new post. + * + * @param sone + * The Sone that creates the post + * @param time + * The time of the post + * @param text + * The text of the post + * @return The created post + */ + public Post createPost(Sone sone, long time, String text) { + return getPost(UUID.randomUUID().toString()).setSone(sone).setTime(time).setText(text); + } + // // ACTIONS // @@ -145,7 +177,7 @@ public class Core extends AbstractService { * @param sone * The Sone to add */ - public void addSone(Sone sone) { + public void addLocalSone(Sone sone) { if (localSones.add(sone)) { soneCache.put(sone.getId(), sone); SoneInserter soneInserter = new SoneInserter(freenetInterface, sone); @@ -161,7 +193,7 @@ public class Core extends AbstractService { * @param sone * The sone to watch */ - public void addRemoteSone(Sone sone) { + public void addSone(Sone sone) { if (!soneCache.containsKey(sone.getId())) { soneCache.put(sone.getId(), sone); } @@ -219,7 +251,7 @@ public class Core extends AbstractService { sone.setProfile(new Profile()); /* set modification counter to 1 so it is inserted immediately. */ sone.setModificationCounter(1); - addSone(sone); + addLocalSone(sone); } catch (MalformedURLException mue1) { throw new SoneException(Type.INVALID_URI); } @@ -238,6 +270,63 @@ public class Core extends AbstractService { localSones.remove(sone); } + /** + * Returns the post with the given ID. If no post exists yet with the given + * ID, a new post is returned. + * + * @param postId + * The ID of the post + * @return The post + */ + public Post getPost(String postId) { + if (!postCache.containsKey(postId)) { + postCache.put(postId, new Post(postId)); + } + return postCache.get(postId); + } + + /** + * Returns the reply with the given ID. If no reply exists yet with the + * given ID, a new reply is returned. + * + * @param replyId + * The ID of the reply + * @return The reply + */ + public Reply getReply(String replyId) { + if (!replyCache.containsKey(replyId)) { + replyCache.put(replyId, new Reply(replyId)); + } + return replyCache.get(replyId); + } + + /** + * Gets all replies to the given post, sorted by date, oldest first. + * + * @param post + * The post the replies refer to + * @return The sorted list of replies for the post + */ + public List getReplies(Post post) { + List replies = new ArrayList(); + for (Reply reply : replyCache.values()) { + if (reply.getPost().equals(post)) { + replies.add(reply); + } + } + Collections.sort(replies, new Comparator() { + + /** + * {@inheritDoc} + */ + @Override + public int compare(Reply leftReply, Reply rightReply) { + return (int) Math.max(Integer.MIN_VALUE, Math.min(Integer.MAX_VALUE, leftReply.getTime() - rightReply.getTime())); + } + }); + return replies; + } + // // SERVICE METHODS // @@ -306,8 +395,7 @@ public class Core extends AbstractService { } long time = configuration.getLongValue(postPrefix + "/Time").getValue(null); String text = configuration.getStringValue(postPrefix + "/Text").getValue(null); - Post post = new Post(id, sone, time, text); - postCache.put(id, post); + Post post = getPost(id).setSone(sone).setTime(time).setText(text); sone.addPost(post); } while (true); int replyCounter = 0; @@ -324,7 +412,7 @@ public class Core extends AbstractService { Post replyPost = postCache.get(configuration.getStringValue(replyPrefix + "/Post").getValue(null)); long replyTime = configuration.getLongValue(replyPrefix + "/Time").getValue(null); String replyText = configuration.getStringValue(replyPrefix + "/Text").getValue(null); - Reply reply = new Reply(replyId, replySone, replyPost, replyTime, replyText); + Reply reply = getReply(replyId).setSone(replySone).setPost(replyPost).setTime(replyTime).setText(replyText); replyCache.put(replyId, reply); } while (true); @@ -336,16 +424,16 @@ public class Core extends AbstractService { if (friendId == null) { break; } - Sone friendSone = soneCache.get(friendId); + Sone friendSone = getSone(friendId); String friendKey = configuration.getStringValue(friendPrefix + "/Key").getValue(null); String friendName = configuration.getStringValue(friendPrefix + "/Name").getValue(null); friendSone.setRequestUri(new FreenetURI(friendKey)).setName(friendName); - addRemoteSone(friendSone); + addSone(friendSone); sone.addFriend(sone); } sone.setModificationCounter(modificationCounter); - addSone(sone); + addLocalSone(sone); } catch (MalformedURLException mue1) { logger.log(Level.WARNING, "Could not create Sone from requestUri (“" + requestUri + "”) and insertUri (“" + insertUri + "”)!", mue1); }