📄 Update year in file headers
[Sone.git] / src / main / java / net / pterodactylus / sone / data / impl / AlbumImpl.java
index 3488577..04ab8e9 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Sone - Album.java - Copyright Â© 2011–2013 David Roden
+ * Sone - AlbumImpl.java - Copyright Â© 2011–2020 David Roden
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
 
 package net.pterodactylus.sone.data.impl;
 
-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 java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.UUID;
-
-import net.pterodactylus.sone.data.Album;
-import net.pterodactylus.sone.data.Image;
-import net.pterodactylus.sone.data.Sone;
-
-import com.google.common.base.Function;
-import com.google.common.base.Optional;
-import com.google.common.base.Predicates;
-import com.google.common.collect.Collections2;
-import com.google.common.hash.Hasher;
+import java.util.*;
+import javax.annotation.*;
+
+import com.google.common.base.*;
+import com.google.common.collect.*;
 import com.google.common.hash.Hashing;
+import com.google.common.hash.*;
+import net.pterodactylus.sone.data.*;
+
+import static com.google.common.base.Preconditions.*;
+import static java.nio.charset.StandardCharsets.*;
 
 /**
  * Container for images that can also contain nested {@link AlbumImpl}s.
- *
- * @author <a href="mailto:bombe@pterodactylus.net">David â€˜Bombe’ Roden</a>
  */
 public class AlbumImpl implements Album {
 
@@ -53,13 +41,13 @@ public class AlbumImpl implements Album {
        private final Sone sone;
 
        /** Nested albums. */
-       private final List<Album> albums = new ArrayList<Album>();
+       private final List<Album> albums = new ArrayList<>();
 
        /** The image IDs in order. */
-       private final List<String> imageIds = new ArrayList<String>();
+       private final List<String> imageIds = new ArrayList<>();
 
        /** The images in this album. */
-       private final Map<String, Image> images = new HashMap<String, Image>();
+       private final Map<String, Image> images = new HashMap<>();
 
        /** The parent album. */
        private Album parent;
@@ -70,9 +58,6 @@ public class AlbumImpl implements Album {
        /** The description of this album. */
        private String description;
 
-       /** The ID of the album picture. */
-       private String albumImage;
-
        /** Creates a new album with a random ID. */
        public AlbumImpl(Sone sone) {
                this(sone, UUID.randomUUID().toString());
@@ -105,7 +90,7 @@ public class AlbumImpl implements Album {
 
        @Override
        public List<Album> getAlbums() {
-               return new ArrayList<Album>(albums);
+               return new ArrayList<>(albums);
        }
 
        @Override
@@ -157,7 +142,7 @@ public class AlbumImpl implements Album {
 
        @Override
        public List<Image> getImages() {
-               return new ArrayList<Image>(Collections2.filter(Collections2.transform(imageIds, new Function<String, Image>() {
+               return new ArrayList<>(Collections2.filter(Collections2.transform(imageIds, new Function<String, Image>() {
 
                        @Override
                        @SuppressWarnings("synthetic-access")
@@ -176,9 +161,6 @@ public class AlbumImpl implements Album {
                        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);
@@ -192,13 +174,6 @@ public class AlbumImpl implements Album {
                checkArgument(image.getSone().equals(sone), "image must belong to the same Sone as this album");
                imageIds.remove(image.getId());
                images.remove(image.getId());
-               if (image.getId().equals(albumImage)) {
-                       if (images.isEmpty()) {
-                               albumImage = null;
-                       } else {
-                               albumImage = images.values().iterator().next().getId();
-                       }
-               }
        }
 
        @Override
@@ -232,14 +207,6 @@ public class AlbumImpl implements Album {
        }
 
        @Override
-       public Image getAlbumImage() {
-               if (albumImage == null) {
-                       return null;
-               }
-               return Optional.fromNullable(images.get(albumImage)).or(images.values().iterator().next());
-       }
-
-       @Override
        public boolean isEmpty() {
                return albums.isEmpty() && images.isEmpty();
        }
@@ -280,43 +247,33 @@ public class AlbumImpl implements Album {
        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();
+                       @Nullable
+                       private String title;
+                       @Nullable
+                       private String description;
 
                        @Override
                        public Modifier setTitle(String title) {
-                               this.title = fromNullable(title);
+                               this.title = 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);
+                               this.description = description;
                                return this;
                        }
 
                        @Override
                        public Album update() throws IllegalStateException {
-                               if (title.isPresent() && title.get().trim().isEmpty()) {
+                               if (title != null && title.trim().isEmpty()) {
                                        throw new AlbumTitleMustNotBeEmpty();
                                }
-                               if (title.isPresent()) {
-                                       AlbumImpl.this.title = title.get();
+                               if (title != null) {
+                                       AlbumImpl.this.title = title;
                                }
-                               if (description.isPresent()) {
-                                       AlbumImpl.this.description = description.get();
-                               }
-                               if (albumImage.isPresent()) {
-                                       AlbumImpl.this.albumImage = albumImage.get();
+                               if (description != null) {
+                                       AlbumImpl.this.description = description;
                                }
                                return AlbumImpl.this;
                        }
@@ -330,31 +287,28 @@ public class AlbumImpl implements Album {
        @Override
        public String getFingerprint() {
                Hasher hash = Hashing.sha256().newHasher();
-               hash.putString("Album(");
-               hash.putString("ID(").putString(id).putString(")");
-               hash.putString("Title(").putString(title).putString(")");
-               hash.putString("Description(").putString(description).putString(")");
-               if (albumImage != null) {
-                       hash.putString("AlbumImage(").putString(albumImage).putString(")");
-               }
+               hash.putString("Album(", UTF_8);
+               hash.putString("ID(", UTF_8).putString(id, UTF_8).putString(")", UTF_8);
+               hash.putString("Title(", UTF_8).putString(title, UTF_8).putString(")", UTF_8);
+               hash.putString("Description(", UTF_8).putString(description, UTF_8).putString(")", UTF_8);
 
                /* add nested albums. */
-               hash.putString("Albums(");
+               hash.putString("Albums(", UTF_8);
                for (Album album : albums) {
-                       hash.putString(album.getFingerprint());
+                       hash.putString(album.getFingerprint(), UTF_8);
                }
-               hash.putString(")");
+               hash.putString(")", UTF_8);
 
                /* add images. */
-               hash.putString("Images(");
+               hash.putString("Images(", UTF_8);
                for (Image image : getImages()) {
                        if (image.isInserted()) {
-                               hash.putString(image.getFingerprint());
+                               hash.putString(image.getFingerprint(), UTF_8);
                        }
                }
-               hash.putString(")");
+               hash.putString(")", UTF_8);
 
-               hash.putString(")");
+               hash.putString(")", UTF_8);
                return hash.hash().toString();
        }