Extract default Image implementation as base for all future image implementations.
[Sone.git] / src / main / java / net / pterodactylus / sone / data / AlbumImpl.java
index 56c44aa..5ee0c23 100644 (file)
@@ -17,6 +17,8 @@
 
 package net.pterodactylus.sone.data;
 
+import static com.google.common.base.Optional.absent;
+import static com.google.common.base.Optional.fromNullable;
 import static com.google.common.base.Preconditions.checkArgument;
 import static com.google.common.base.Preconditions.checkNotNull;
 import static com.google.common.base.Preconditions.checkState;
@@ -27,6 +29,9 @@ import java.util.List;
 import java.util.Map;
 import java.util.UUID;
 
+import net.pterodactylus.sone.data.impl.ImageBuilderImpl;
+import net.pterodactylus.sone.database.ImageBuilder;
+
 import com.google.common.base.Function;
 import com.google.common.base.Optional;
 import com.google.common.base.Predicates;
@@ -170,24 +175,6 @@ public class AlbumImpl implements Album {
        }
 
        @Override
-       public void addImage(Image image) {
-               checkNotNull(image, "image must not be null");
-               checkNotNull(image.getSone(), "image must have an owner");
-               checkArgument(image.getSone().equals(sone), "image must belong to the same Sone as this album");
-               if (image.getAlbum() != null) {
-                       image.getAlbum().removeImage(image);
-               }
-               image.setAlbum(this);
-               if (imageIds.isEmpty() && (albumImage == null)) {
-                       albumImage = image.getId();
-               }
-               if (!imageIds.contains(image.getId())) {
-                       imageIds.add(image.getId());
-                       images.put(image.getId(), image);
-               }
-       }
-
-       @Override
        public void removeImage(Image image) {
                checkNotNull(image, "image must not be null");
                checkNotNull(image.getSone(), "image must have an owner");
@@ -242,12 +229,6 @@ public class AlbumImpl implements Album {
        }
 
        @Override
-       public AlbumImpl setAlbumImage(String id) {
-               this.albumImage = id;
-               return this;
-       }
-
-       @Override
        public boolean isEmpty() {
                return albums.isEmpty() && images.isEmpty();
        }
@@ -280,20 +261,68 @@ public class AlbumImpl implements Album {
        }
 
        @Override
-       public Album setTitle(String title) {
-               this.title = checkNotNull(title, "title must not be null");
-               return this;
+       public String getDescription() {
+               return description;
        }
 
        @Override
-       public String getDescription() {
-               return description;
+       public ImageBuilder newImageBuilder() throws IllegalStateException {
+               return new ImageBuilderImpl(this) {
+                       @Override
+                       public Image build() throws IllegalStateException {
+                               Image image = super.build();
+                               if (images.isEmpty() && (albumImage == null)) {
+                                       albumImage = image.getId();
+                               }
+                               images.put(image.getId(), image);
+                               imageIds.add(image.getId());
+                               return image;
+                       }
+               };
        }
 
        @Override
-       public AlbumImpl setDescription(String description) {
-               this.description = checkNotNull(description, "description must not be null");
-               return this;
+       public Modifier modify() throws IllegalStateException {
+               // TODO: reenable check for local Sones
+               return new Modifier() {
+                       private Optional<String> title = absent();
+
+                       private Optional<String> description = absent();
+
+                       private Optional<String> albumImage = absent();
+
+                       @Override
+                       public Modifier setTitle(String title) {
+                               this.title = fromNullable(title);
+                               return this;
+                       }
+
+                       @Override
+                       public Modifier setDescription(String description) {
+                               this.description = fromNullable(description);
+                               return this;
+                       }
+
+                       @Override
+                       public Modifier setAlbumImage(String imageId) {
+                               this.albumImage = fromNullable(imageId);
+                               return this;
+                       }
+
+                       @Override
+                       public Album update() throws IllegalStateException {
+                               if (title.isPresent()) {
+                                       AlbumImpl.this.title = title.get();
+                               }
+                               if (description.isPresent()) {
+                                       AlbumImpl.this.description = description.get();
+                               }
+                               if (albumImage.isPresent()) {
+                                       AlbumImpl.this.albumImage = albumImage.get();
+                               }
+                               return AlbumImpl.this;
+                       }
+               };
        }
 
        //