From: David ‘Bombe’ Roden Date: Sun, 7 Dec 2014 09:52:03 +0000 (+0100) Subject: Use Sone builder to set the posts of a Sone. X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=90d193f69c4aa12c232f937a9d2b7d5afdda81dd;p=Sone.git Use Sone builder to set the posts of a Sone. --- diff --git a/src/main/java/net/pterodactylus/sone/core/SoneParser.java b/src/main/java/net/pterodactylus/sone/core/SoneParser.java index 0323796..d2914da 100644 --- a/src/main/java/net/pterodactylus/sone/core/SoneParser.java +++ b/src/main/java/net/pterodactylus/sone/core/SoneParser.java @@ -119,12 +119,11 @@ public class SoneParser { } else { soneBuilder.using(new Client("Unknown Client", "0.0")); } - Sone sone = soneBuilder.build(); SimpleXML profileXml = soneXml.getNode("profile"); if (profileXml == null) { /* TODO - mark Sone as bad. */ - logger.log(Level.WARNING, String.format("Downloaded Sone %s has no profile!", sone)); + logger.log(Level.WARNING, String.format("Downloaded Sone %s has no profile!", originalSone)); return null; } @@ -135,7 +134,7 @@ public class SoneParser { 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 profile = new Profile(originalSone).setFirstName(profileFirstName).setMiddleName(profileMiddleName).setLastName(profileLastName); profile.setBirthDay(profileBirthDay).setBirthMonth(profileBirthMonth).setBirthYear(profileBirthYear); /* avatar is processed after images are loaded. */ String avatarId = profileXml.getValue("avatar", null); @@ -147,7 +146,7 @@ public class SoneParser { String fieldName = fieldXml.getValue("field-name", null); String fieldValue = fieldXml.getValue("field-value", ""); if (fieldName == null) { - logger.log(Level.WARNING, String.format("Downloaded profile field for Sone %s with missing data! Name: %s, Value: %s", sone, fieldName, fieldValue)); + logger.log(Level.WARNING, String.format("Downloaded profile field for Sone %s with missing data! Name: %s, Value: %s", originalSone, fieldName, fieldValue)); return null; } try { @@ -161,11 +160,11 @@ public class SoneParser { /* parse posts. */ SimpleXML postsXml = soneXml.getNode("posts"); - Set posts = new HashSet(); if (postsXml == null) { /* TODO - mark Sone as bad. */ - logger.log(Level.WARNING, String.format("Downloaded Sone %s has no posts!", sone)); + logger.log(Level.WARNING, String.format("Downloaded Sone %s has no posts!", originalSone)); } else { + Set posts = new HashSet(); for (SimpleXML postXml : postsXml.getNodes("post")) { String postId = postXml.getValue("id", null); String postRecipientId = postXml.getValue("recipient", null); @@ -173,24 +172,26 @@ public class SoneParser { String postText = postXml.getValue("text", null); if ((postId == null) || (postTime == null) || (postText == null)) { /* TODO - mark Sone as bad. */ - logger.log(Level.WARNING, String.format("Downloaded post for Sone %s with missing data! ID: %s, Time: %s, Text: %s", sone, postId, postTime, postText)); + logger.log(Level.WARNING, String.format("Downloaded post for Sone %s with missing data! ID: %s, Time: %s, Text: %s", originalSone, postId, postTime, postText)); return null; } try { PostBuilder postBuilder = core.postBuilder(); /* TODO - parse time correctly. */ - postBuilder.withId(postId).from(sone.getId()).withTime(Long.parseLong(postTime)).withText(postText); + postBuilder.withId(postId).from(originalSone.getId()).withTime(Long.parseLong(postTime)).withText(postText); if ((postRecipientId != null) && (postRecipientId.length() == 43)) { postBuilder.to(postRecipientId); } posts.add(postBuilder.build()); } catch (NumberFormatException nfe1) { /* TODO - mark Sone as bad. */ - logger.log(Level.WARNING, String.format("Downloaded post for Sone %s with invalid time: %s", sone, postTime)); + logger.log(Level.WARNING, String.format("Downloaded post for Sone %s with invalid time: %s", originalSone, postTime)); return null; } } + soneBuilder.withPosts(posts); } + Sone sone = soneBuilder.build(); /* parse replies. */ SimpleXML repliesXml = soneXml.getNode("replies"); @@ -325,7 +326,6 @@ public class SoneParser { /* atomic setter operation on the Sone. */ synchronized (sone) { sone.setProfile(profile); - sone.setPosts(posts); sone.setReplies(replies); sone.setLikePostIds(likedPostIds); sone.setLikeReplyIds(likedReplyIds); diff --git a/src/main/java/net/pterodactylus/sone/data/impl/AbstractSoneBuilder.java b/src/main/java/net/pterodactylus/sone/data/impl/AbstractSoneBuilder.java index 1ce38e4..bc19d4d 100644 --- a/src/main/java/net/pterodactylus/sone/data/impl/AbstractSoneBuilder.java +++ b/src/main/java/net/pterodactylus/sone/data/impl/AbstractSoneBuilder.java @@ -2,7 +2,11 @@ package net.pterodactylus.sone.data.impl; import static com.google.common.base.Preconditions.checkState; +import java.util.Collection; +import java.util.HashSet; + import net.pterodactylus.sone.data.Client; +import net.pterodactylus.sone.data.Post; import net.pterodactylus.sone.database.SoneBuilder; import net.pterodactylus.sone.freenet.wot.Identity; import net.pterodactylus.sone.freenet.wot.OwnIdentity; @@ -18,6 +22,7 @@ public abstract class AbstractSoneBuilder implements SoneBuilder { protected boolean local; protected long lastUpdated; protected Client client; + protected final Collection posts = new HashSet(); @Override public SoneBuilder from(Identity identity) { @@ -43,6 +48,13 @@ public abstract class AbstractSoneBuilder implements SoneBuilder { return this; } + @Override + public SoneBuilder withPosts(Collection posts) { + this.posts.clear(); + this.posts.addAll(posts); + return this; + } + protected void validate() throws IllegalStateException { checkState(identity != null, "identity must not be null"); checkState(!local || (identity instanceof OwnIdentity), diff --git a/src/main/java/net/pterodactylus/sone/data/impl/SoneImpl.java b/src/main/java/net/pterodactylus/sone/data/impl/SoneImpl.java index 44111aa..2479276 100644 --- a/src/main/java/net/pterodactylus/sone/data/impl/SoneImpl.java +++ b/src/main/java/net/pterodactylus/sone/data/impl/SoneImpl.java @@ -25,6 +25,7 @@ import java.net.MalformedURLException; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; +import java.util.HashSet; import java.util.List; import java.util.Set; import java.util.concurrent.CopyOnWriteArraySet; @@ -94,7 +95,7 @@ public class SoneImpl implements LocalSone { private volatile boolean known; /** All posts. */ - private final Set posts = new CopyOnWriteArraySet(); + private final Collection posts = new HashSet(); /** All replies. */ private final Set replies = new CopyOnWriteArraySet(); @@ -120,13 +121,14 @@ public class SoneImpl implements LocalSone { * @param local * {@code true} if the Sone is a local Sone, {@code false} otherwise */ - public SoneImpl(Database database, Identity identity, boolean local, long time, Client client) { + public SoneImpl(Database database, Identity identity, boolean local, long time, Client client, Collection posts) { this.database = database; this.id = identity.getId(); this.identity = identity; this.local = local; this.time = time; this.client = client; + this.posts.addAll(posts); } // @@ -368,10 +370,6 @@ public class SoneImpl implements LocalSone { * @return This Sone (for method chaining) */ public Sone setPosts(Collection posts) { - synchronized (this) { - this.posts.clear(); - this.posts.addAll(posts); - } return this; } diff --git a/src/main/java/net/pterodactylus/sone/database/SoneBuilder.java b/src/main/java/net/pterodactylus/sone/database/SoneBuilder.java index 8fc895b..0fdeacf 100644 --- a/src/main/java/net/pterodactylus/sone/database/SoneBuilder.java +++ b/src/main/java/net/pterodactylus/sone/database/SoneBuilder.java @@ -1,6 +1,9 @@ package net.pterodactylus.sone.database; +import java.util.Collection; + import net.pterodactylus.sone.data.Client; +import net.pterodactylus.sone.data.Post; import net.pterodactylus.sone.data.Sone; import net.pterodactylus.sone.freenet.wot.Identity; @@ -17,6 +20,8 @@ public interface SoneBuilder { SoneBuilder lastUpdated(long lastUpdated); SoneBuilder using(Client client); + SoneBuilder withPosts(Collection posts); + Sone build() throws IllegalStateException; } diff --git a/src/main/java/net/pterodactylus/sone/database/memory/MemorySoneBuilder.java b/src/main/java/net/pterodactylus/sone/database/memory/MemorySoneBuilder.java index 565c7c4..355d2ad 100644 --- a/src/main/java/net/pterodactylus/sone/database/memory/MemorySoneBuilder.java +++ b/src/main/java/net/pterodactylus/sone/database/memory/MemorySoneBuilder.java @@ -21,7 +21,7 @@ public class MemorySoneBuilder extends AbstractSoneBuilder { @Override public Sone build() throws IllegalStateException { validate(); - return new SoneImpl(database, identity, local, lastUpdated, client); + return new SoneImpl(database, identity, local, lastUpdated, client, posts); } }