Extract default Image implementation as base for all future image implementations.
[Sone.git] / src / main / java / net / pterodactylus / sone / data / AlbumImpl.java
index 5422812..5ee0c23 100644 (file)
@@ -29,9 +29,11 @@ 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.Preconditions;
 import com.google.common.base.Predicates;
 import com.google.common.collect.Collections2;
 import com.google.common.hash.Hasher;
@@ -173,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");
@@ -282,8 +266,24 @@ public class AlbumImpl implements Album {
        }
 
        @Override
+       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 Modifier modify() throws IllegalStateException {
-               Preconditions.checkState(getSone().isLocal(), "album must belong to a local Sone");
+               // TODO: reenable check for local Sones
                return new Modifier() {
                        private Optional<String> title = absent();
 
@@ -311,7 +311,6 @@ public class AlbumImpl implements Album {
 
                        @Override
                        public Album update() throws IllegalStateException {
-                               checkState(!albumImage.isPresent(), "album image must belong to this album");
                                if (title.isPresent()) {
                                        AlbumImpl.this.title = title.get();
                                }