package net.pterodactylus.sone.data;
import java.util.ArrayList;
+import java.util.LinkedHashMap;
import java.util.List;
+import java.util.Map;
import java.util.UUID;
import net.pterodactylus.util.validation.Validation;
private final List<Album> albums = new ArrayList<Album>();
/** The images in this album. */
- private final List<Image> images = new ArrayList<Image>();
+ private final Map<String, Image> images = new LinkedHashMap<String, Image>();
/** The parent album. */
private Album parent;
/** The description of this album. */
private String description;
- /** The index of the album picture. */
- private int albumImage = -1;
+ /** The ID of the album picture. */
+ private String albumImage;
/**
* Creates a new album with a random ID.
* @return The images in this album
*/
public List<Image> getImages() {
- return new ArrayList<Image>(images);
+ return new ArrayList<Image>(images.values());
}
/**
*/
public void addImage(Image image) {
Validation.begin().isNotNull("Image", image).check().isNotNull("Image Owner", image.getSone()).check().isEqual("Image Owner", image.getSone(), sone).check();
+ if (image.getAlbum() != null) {
+ image.getAlbum().removeImage(image);
+ }
image.setAlbum(this);
- if (!images.contains(image)) {
- images.add(image);
+ if (images.isEmpty()) {
+ albumImage = image.getId();
+ }
+ if (!images.containsKey(image.getId())) {
+ images.put(image.getId(), image);
}
}
public void removeImage(Image image) {
Validation.begin().isNotNull("Image", image).check().isEqual("Image Owner", image.getSone(), sone).check();
images.remove(image);
+ if (image.getId().equals(albumImage)) {
+ if (images.isEmpty()) {
+ albumImage = null;
+ } else {
+ albumImage = images.values().iterator().next().getId();
+ }
+ }
}
/**
* @return The image to show when this album is listed
*/
public Image getAlbumImage() {
- if (albumImage == -1) {
+ if (albumImage == null) {
return null;
}
return images.get(albumImage);
}
/**
+ * Sets the ID of the album image.
+ *
+ * @param id
+ * The ID of the album image
+ * @return This album
+ */
+ public Album setAlbumImage(String id) {
+ this.albumImage = id;
+ return this;
+ }
+
+ /**
* Returns whether this album contains any other albums or images.
*
* @return {@code true} if this album is empty, {@code false} otherwise
/* add images. */
fingerprint.append("Images(");
- for (Image image : images) {
+ for (Image image : images.values()) {
if (image.isInserted()) {
fingerprint.append(image.getFingerprint());
}