Store ID as string, don’t force-convert to UUID.
[Sone.git] / src / main / java / net / pterodactylus / sone / data / SoneImpl.java
index f57bac9..725f32a 100644 (file)
 package net.pterodactylus.sone.data;
 
 import static com.google.common.base.Preconditions.checkNotNull;
+import static java.lang.String.format;
+import static java.util.logging.Logger.getLogger;
 
+import java.net.MalformedURLException;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
@@ -28,9 +31,9 @@ import java.util.concurrent.CopyOnWriteArraySet;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
-import net.pterodactylus.sone.core.Options;
+import net.pterodactylus.sone.data.SoneOptions.DefaultSoneOptions;
 import net.pterodactylus.sone.freenet.wot.Identity;
-import net.pterodactylus.util.logging.Logging;
+import net.pterodactylus.sone.freenet.wot.OwnIdentity;
 
 import freenet.keys.FreenetURI;
 
@@ -47,7 +50,7 @@ import com.google.common.hash.Hashing;
 public class SoneImpl implements Sone {
 
        /** The logger. */
-       private static final Logger logger = Logging.getLogger(SoneImpl.class);
+       private static final Logger logger = getLogger("Sone.Data");
 
        /** The ID of this Sone. */
        private final String id;
@@ -56,14 +59,7 @@ public class SoneImpl implements Sone {
        private final boolean local;
 
        /** The identity of this Sone. */
-       private Identity identity;
-
-       /** The URI under which the Sone is stored in Freenet. */
-       private volatile FreenetURI requestUri;
-
-       /** The URI used to insert a new version of this Sone. */
-       /* This will be null for remote Sones! */
-       private volatile FreenetURI insertUri;
+       private final Identity identity;
 
        /** The latest edition of the Sone. */
        private volatile long latestEdition;
@@ -99,21 +95,22 @@ public class SoneImpl implements Sone {
        private final Set<String> likedReplyIds = new CopyOnWriteArraySet<String>();
 
        /** The root album containing all albums. */
-       private final Album rootAlbum = new AlbumImpl().setSone(this);
+       private final Album rootAlbum = new AlbumImpl(this);
 
        /** Sone-specific options. */
-       private Options options = new Options();
+       private SoneOptions options = new DefaultSoneOptions();
 
        /**
         * Creates a new Sone.
         *
-        * @param id
-        *              The ID of the Sone
+        * @param identity
+        *              The identity of the Sone
         * @param local
         *              {@code true} if the Sone is a local Sone, {@code false} otherwise
         */
-       public SoneImpl(String id, boolean local) {
-               this.id = id;
+       public SoneImpl(Identity identity, boolean local) {
+               this.id = identity.getId();
+               this.identity = identity;
                this.local = local;
        }
 
@@ -140,24 +137,6 @@ public class SoneImpl implements Sone {
        }
 
        /**
-        * Sets the identity of this Sone. The {@link Identity#getId() ID} of the
-        * identity has to match this Sone’s {@link #getId()}.
-        *
-        * @param identity
-        *              The identity of this Sone
-        * @return This Sone (for method chaining)
-        * @throws IllegalArgumentException
-        *              if the ID of the identity does not match this Sone’s ID
-        */
-       public SoneImpl setIdentity(Identity identity) throws IllegalArgumentException {
-               if (!identity.getId().equals(id)) {
-                       throw new IllegalArgumentException("Identity’s ID does not match Sone’s ID!");
-               }
-               this.identity = identity;
-               return this;
-       }
-
-       /**
         * Returns the name of this Sone.
         *
         * @return The name of this Sone
@@ -181,26 +160,17 @@ public class SoneImpl implements Sone {
         * @return The request URI of this Sone
         */
        public FreenetURI getRequestUri() {
-               return (requestUri != null) ? requestUri.setSuggestedEdition(latestEdition) : null;
-       }
-
-       /**
-        * Sets the request URI of this Sone.
-        *
-        * @param requestUri
-        *              The request URI of this Sone
-        * @return This Sone (for method chaining)
-        */
-       public Sone setRequestUri(FreenetURI requestUri) {
-               if (this.requestUri == null) {
-                       this.requestUri = requestUri.setKeyType("USK").setDocName("Sone").setMetaString(new String[0]);
-                       return this;
+               try {
+                       return new FreenetURI(getIdentity().getRequestUri())
+                                       .setKeyType("USK")
+                                       .setDocName("Sone")
+                                       .setMetaString(new String[0])
+                                       .setSuggestedEdition(latestEdition);
+               } catch (MalformedURLException e) {
+                       throw new IllegalStateException(
+                                       format("Identity %s's request URI is incorrect.",
+                                                       getIdentity()), e);
                }
-               if (!this.requestUri.equalsKeypair(requestUri)) {
-                       logger.log(Level.WARNING, String.format("Request URI %s tried to overwrite %s!", requestUri, this.requestUri));
-                       return this;
-               }
-               return this;
        }
 
        /**
@@ -209,26 +179,17 @@ public class SoneImpl implements Sone {
         * @return The insert URI of this Sone
         */
        public FreenetURI getInsertUri() {
-               return (insertUri != null) ? insertUri.setSuggestedEdition(latestEdition) : null;
-       }
-
-       /**
-        * Sets the insert URI of this Sone.
-        *
-        * @param insertUri
-        *              The insert URI of this Sone
-        * @return This Sone (for method chaining)
-        */
-       public Sone setInsertUri(FreenetURI insertUri) {
-               if (this.insertUri == null) {
-                       this.insertUri = insertUri.setKeyType("USK").setDocName("Sone").setMetaString(new String[0]);
-                       return this;
+               if (!isLocal()) {
+                       return null;
                }
-               if (!this.insertUri.equalsKeypair(insertUri)) {
-                       logger.log(Level.WARNING, String.format("Request URI %s tried to overwrite %s!", insertUri, this.insertUri));
-                       return this;
+               try {
+                       return new FreenetURI(((OwnIdentity) getIdentity()).getInsertUri())
+                                       .setDocName("Sone")
+                                       .setMetaString(new String[0])
+                                       .setSuggestedEdition(latestEdition);
+               } catch (MalformedURLException e) {
+                       throw new IllegalStateException(format("Own identity %s's insert URI is incorrect.", getIdentity()), e);
                }
-               return this;
        }
 
        /**
@@ -643,7 +604,7 @@ public class SoneImpl implements Sone {
         *
         * @return The options of this Sone
         */
-       public Options getOptions() {
+       public SoneOptions getOptions() {
                return options;
        }
 
@@ -654,7 +615,7 @@ public class SoneImpl implements Sone {
         *              The options of this Sone
         */
        /* TODO - remove this method again, maybe add an option provider */
-       public void setOptions(Options options) {
+       public void setOptions(SoneOptions options) {
                this.options = options;
        }
 
@@ -742,7 +703,7 @@ public class SoneImpl implements Sone {
        /** {@inheritDoc} */
        @Override
        public String toString() {
-               return getClass().getName() + "[identity=" + identity + ",requestUri=" + requestUri + ",insertUri(" + String.valueOf(insertUri).length() + "),friends(" + friendSones.size() + "),posts(" + posts.size() + "),replies(" + replies.size() + "),albums(" + getRootAlbum().getAlbums().size() + ")]";
+               return getClass().getName() + "[identity=" + identity + ",friends(" + friendSones.size() + "),posts(" + posts.size() + "),replies(" + replies.size() + "),albums(" + getRootAlbum().getAlbums().size() + ")]";
        }
 
 }