Remove request and insert URI from default Sone implementation.
[Sone.git] / src / main / java / net / pterodactylus / sone / data / impl / DefaultSone.java
index 59964f9..41f9c6f 100644 (file)
@@ -68,16 +68,6 @@ public class DefaultSone implements Sone {
        /** Whether the Sone is local. */
        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;
-
        /** The latest edition of the Sone. */
        private volatile long latestEdition;
 
@@ -91,7 +81,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;
@@ -125,10 +115,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();
        }
 
@@ -141,57 +132,17 @@ public class DefaultSone implements Sone {
        }
 
        public Identity getIdentity() {
-               return identity;
-       }
-
-       public DefaultSone 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;
+               return database.getIdentity(id).get();
        }
 
        public String getName() {
-               return (identity != null) ? identity.getNickname() : null;
+               return getIdentity().getNickname();
        }
 
        public boolean isLocal() {
                return local;
        }
 
-       public FreenetURI getRequestUri() {
-               return (requestUri != null) ? requestUri.setSuggestedEdition(latestEdition) : null;
-       }
-
-       public Sone setRequestUri(FreenetURI requestUri) {
-               if (this.requestUri == null) {
-                       this.requestUri = requestUri.setKeyType("USK").setDocName("Sone").setMetaString(new String[0]);
-                       return this;
-               }
-               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;
-       }
-
-       public FreenetURI getInsertUri() {
-               return (insertUri != null) ? insertUri.setSuggestedEdition(latestEdition) : null;
-       }
-
-       public Sone setInsertUri(FreenetURI insertUri) {
-               if (this.insertUri == null) {
-                       this.insertUri = insertUri.setKeyType("USK").setDocName("Sone").setMetaString(new String[0]);
-                       return this;
-               }
-               if (!this.insertUri.equalsKeypair(insertUri)) {
-                       logger.log(Level.WARNING, String.format("Request URI %s tried to overwrite %s!", insertUri, this.insertUri));
-                       return this;
-               }
-               return this;
-       }
-
        public long getLatestEdition() {
                return latestEdition;
        }
@@ -234,11 +185,6 @@ public class DefaultSone implements Sone {
                return client;
        }
 
-       public Sone setClient(Client client) {
-               this.client = client;
-               return this;
-       }
-
        public boolean isKnown() {
                return known;
        }
@@ -334,11 +280,6 @@ public class DefaultSone implements Sone {
                return likedPostIds.contains(postId);
        }
 
-       public Sone addLikedPostId(String postId) {
-               likedPostIds.add(postId);
-               return this;
-       }
-
        public Sone removeLikedPostId(String postId) {
                likedPostIds.remove(postId);
                return this;
@@ -409,6 +350,23 @@ public class DefaultSone implements Sone {
                };
        }
 
+       public Modifier modify() {
+               return new Modifier() {
+                       private long latestEdition = DefaultSone.this.latestEdition;
+                       @Override
+                       public Modifier setLatestEdition(long latestEdition) {
+                               this.latestEdition = latestEdition;
+                               return this;
+                       }
+
+                       @Override
+                       public Sone update() {
+                               DefaultSone.this.latestEdition = latestEdition;
+                               return DefaultSone.this;
+                       }
+               };
+       }
+
        //
        // FINGERPRINTABLE METHODS
        //
@@ -488,7 +446,7 @@ public class DefaultSone implements Sone {
 
        @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() + "[id=" + id + ",friends(" + friendSones.size() + "),posts(" + posts.size() + "),replies(" + replies.size() + "),albums(" + getRootAlbum().getAlbums().size() + ")]";
        }
 
 }