Set client information in Sone builder.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sat, 19 Oct 2013 18:04:46 +0000 (20:04 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 28 Feb 2014 21:25:30 +0000 (22:25 +0100)
src/main/java/net/pterodactylus/sone/core/Core.java
src/main/java/net/pterodactylus/sone/core/SoneParser.java
src/main/java/net/pterodactylus/sone/data/Sone.java
src/main/java/net/pterodactylus/sone/data/impl/AbstractSoneBuilder.java
src/main/java/net/pterodactylus/sone/data/impl/DefaultSone.java
src/main/java/net/pterodactylus/sone/data/impl/DefaultSoneBuilder.java
src/main/java/net/pterodactylus/sone/database/SoneBuilder.java
src/main/java/net/pterodactylus/sone/text/SoneTextParser.java

index 1109537..0f22b81 100644 (file)
@@ -559,9 +559,8 @@ public class Core extends AbstractService implements SoneProvider {
                logger.info(String.format("Adding Sone from OwnIdentity: %s", ownIdentity));
                synchronized (sones) {
                        final Sone sone;
-                       sone = database.newSoneBuilder().by(ownIdentity.getId()).local().build(Optional.<SoneCreated>absent());
+                       sone = database.newSoneBuilder().by(ownIdentity.getId()).local().using(new Client("Sone", SonePlugin.VERSION.toString())).build(Optional.<SoneCreated>absent());
                        sone.modify().setLatestEdition(Numbers.safeParseLong(ownIdentity.getProperty("Sone.LatestEdition"), (long) 0)).update();
-                       sone.setClient(new Client("Sone", SonePlugin.VERSION.toString()));
                        sone.setKnown(true);
                        /* TODO - load posts ’n stuff */
                        sones.put(ownIdentity.getId(), sone);
index 79fc09a..7900a10 100644 (file)
@@ -90,17 +90,29 @@ public class SoneParser {
                        return null;
                }
 
-               Sone sone = new DefaultSone(new MemoryDatabase(null), originalSone.getId(), originalSone.isLocal());
-
                SimpleXML soneXml;
                try {
                        soneXml = SimpleXML.fromDocument(document);
                } catch (NullPointerException npe1) {
                        /* for some reason, invalid XML can cause NPEs. */
-                       logger.log(Level.WARNING, String.format("XML for Sone %s can not be parsed!", sone), npe1);
+                       logger.log(Level.WARNING, String.format("XML for Sone %s can not be parsed!", originalSone), npe1);
                        return null;
                }
 
+               SimpleXML clientXml = soneXml.getNode("client");
+               Client soneClient = originalSone.getClient();
+               if (clientXml != null) {
+                       String clientName = clientXml.getValue("name", null);
+                       String clientVersion = clientXml.getValue("version", null);
+                       if ((clientName == null) || (clientVersion == null)) {
+                               logger.log(Level.WARNING, String.format("Download Sone %s with client XML but missing name or version!", originalSone));
+                               return null;
+                       }
+                       soneClient = new Client(clientName, clientVersion);
+               }
+
+               Sone sone = new DefaultSone(new MemoryDatabase(null), originalSone.getId(), originalSone.isLocal(), soneClient);
+
                Integer protocolVersion = null;
                String soneProtocolVersion = soneXml.getValue("protocol-version", null);
                if (soneProtocolVersion != null) {
@@ -136,17 +148,6 @@ public class SoneParser {
                        return null;
                }
 
-               SimpleXML clientXml = soneXml.getNode("client");
-               if (clientXml != null) {
-                       String clientName = clientXml.getValue("name", null);
-                       String clientVersion = clientXml.getValue("version", null);
-                       if ((clientName == null) || (clientVersion == null)) {
-                               logger.log(Level.WARNING, String.format("Download Sone %s with client XML but missing name or version!", sone));
-                               return null;
-                       }
-                       sone.setClient(new Client(clientName, clientVersion));
-               }
-
                SimpleXML profileXml = soneXml.getNode("profile");
                if (profileXml == null) {
                        /* TODO - mark Sone as bad. */
index 083b3a5..989c6a1 100644 (file)
@@ -267,15 +267,6 @@ public interface Sone extends Identified, Fingerprintable, Comparable<Sone> {
        Client getClient();
 
        /**
-        * Sets the client used by this Sone.
-        *
-        * @param client
-        *              The client used by this Sone, or {@code null}
-        * @return This Sone (for method chaining)
-        */
-       Sone setClient(Client client);
-
-       /**
         * Returns whether this Sone is known.
         *
         * @return {@code true} if this Sone is known, {@code false} otherwise
index 140fbd5..878ba8d 100644 (file)
@@ -2,6 +2,7 @@ package net.pterodactylus.sone.data.impl;
 
 import static com.google.common.base.Preconditions.checkState;
 
+import net.pterodactylus.sone.data.Client;
 import net.pterodactylus.sone.database.SoneBuilder;
 import net.pterodactylus.sone.freenet.wot.Identity;
 
@@ -17,6 +18,7 @@ public abstract class AbstractSoneBuilder implements SoneBuilder {
 
        protected String id;
        protected boolean local;
+       protected Client client;
 
        @Override
        public SoneBuilder by(String id) {
@@ -30,8 +32,15 @@ public abstract class AbstractSoneBuilder implements SoneBuilder {
                return this;
        }
 
+       @Override
+       public SoneBuilder using(Client client) {
+               this.client = client;
+               return this;
+       }
+
        protected void validate() throws IllegalStateException {
                checkState(id != null, "id must not be null");
+               checkState(client != null, "client must not be null");
        }
 
 }
index 3abd1d4..92d7d5f 100644 (file)
@@ -88,7 +88,7 @@ public class DefaultSone implements Sone {
        private volatile Profile profile = new Profile(this);
 
        /** The client used by the Sone. */
-       private volatile Client client;
+       private final Client client;
 
        /** Whether this Sone is known. */
        private volatile boolean known;
@@ -122,10 +122,11 @@ public class DefaultSone implements Sone {
         * @param local
         *              {@code true} if the Sone is a local Sone, {@code false} otherwise
         */
-       public DefaultSone(Database database, String id, boolean local) {
+       public DefaultSone(Database database, String id, boolean local, Client client) {
                this.database = database;
                this.id = id;
                this.local = local;
+               this.client = client;
                rootAlbum = new DefaultAlbumBuilder(database, this, null).build();
        }
 
@@ -223,11 +224,6 @@ public class DefaultSone implements Sone {
                return client;
        }
 
-       public Sone setClient(Client client) {
-               this.client = client;
-               return this;
-       }
-
        public boolean isKnown() {
                return known;
        }
index 37a83fd..02a2d87 100644 (file)
@@ -22,7 +22,7 @@ public class DefaultSoneBuilder extends AbstractSoneBuilder {
        @Override
        public Sone build(Optional<SoneCreated> soneCreated) throws IllegalStateException {
                validate();
-               Sone sone = new DefaultSone(database, id, local);
+               Sone sone = new DefaultSone(database, id, local, client);
                if (soneCreated.isPresent()) {
                        soneCreated.get().soneCreated(sone);
                }
index d0b38af..ebac97e 100644 (file)
@@ -17,6 +17,7 @@
 
 package net.pterodactylus.sone.database;
 
+import net.pterodactylus.sone.data.Client;
 import net.pterodactylus.sone.data.Sone;
 import net.pterodactylus.sone.freenet.wot.Identity;
 
@@ -31,6 +32,7 @@ public interface SoneBuilder {
 
        SoneBuilder by(String id);
        SoneBuilder local();
+       SoneBuilder using(Client client);
 
        Sone build(Optional<SoneCreated> soneCreated) throws IllegalStateException;
 
index 0edd5f3..a9208dc 100644 (file)
@@ -237,7 +237,7 @@ public class SoneTextParser implements Parser<SoneTextParserContext> {
                                                                 * don’t use create=true above, we don’t want
                                                                 * the empty shell.
                                                                 */
-                                                               sone = Optional.<Sone>of(new DefaultSone(database, soneId, false));
+                                                               sone = Optional.<Sone>of(new DefaultSone(database, soneId, false, null));
                                                        }
                                                        parts.add(new SonePart(sone.get()));
                                                        line = line.substring(50);